mirror of
https://github.com/documenso/documenso.git
synced 2025-11-24 21:51:40 +10:00
Compare commits
2 Commits
feat/unlin
...
fix/optimi
| Author | SHA1 | Date | |
|---|---|---|---|
| 8083e54625 | |||
| 7a55abadaf |
@ -1,3 +1,4 @@
|
|||||||
|
// Todo: [Webhooks] delete file after deployment.
|
||||||
import { handlerTriggerWebhooks } from '@documenso/lib/server-only/webhooks/trigger/handler';
|
import { handlerTriggerWebhooks } from '@documenso/lib/server-only/webhooks/trigger/handler';
|
||||||
|
|
||||||
import type { Route } from './+types/webhook.trigger';
|
import type { Route } from './+types/webhook.trigger';
|
||||||
|
|||||||
@ -22,53 +22,51 @@ export const run = async ({
|
|||||||
|
|
||||||
const { webhookUrl: url, secret } = webhook;
|
const { webhookUrl: url, secret } = webhook;
|
||||||
|
|
||||||
await io.runTask('execute-webhook', async () => {
|
const payloadData = {
|
||||||
const payloadData = {
|
event,
|
||||||
event,
|
payload: data,
|
||||||
payload: data,
|
createdAt: new Date().toISOString(),
|
||||||
createdAt: new Date().toISOString(),
|
webhookEndpoint: url,
|
||||||
webhookEndpoint: url,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(payloadData),
|
body: JSON.stringify(payloadData),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Documenso-Secret': secret ?? '',
|
'X-Documenso-Secret': secret ?? '',
|
||||||
},
|
},
|
||||||
});
|
|
||||||
|
|
||||||
const body = await response.text();
|
|
||||||
|
|
||||||
let responseBody: Prisma.InputJsonValue | Prisma.JsonNullValueInput = Prisma.JsonNull;
|
|
||||||
|
|
||||||
try {
|
|
||||||
responseBody = JSON.parse(body);
|
|
||||||
} catch (err) {
|
|
||||||
responseBody = body;
|
|
||||||
}
|
|
||||||
|
|
||||||
await prisma.webhookCall.create({
|
|
||||||
data: {
|
|
||||||
url,
|
|
||||||
event,
|
|
||||||
status: response.ok ? WebhookCallStatus.SUCCESS : WebhookCallStatus.FAILED,
|
|
||||||
requestBody: payloadData as Prisma.InputJsonValue,
|
|
||||||
responseCode: response.status,
|
|
||||||
responseBody,
|
|
||||||
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
||||||
webhookId: webhook.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Webhook execution failed with status ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: response.ok,
|
|
||||||
status: response.status,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const body = await response.text();
|
||||||
|
|
||||||
|
let responseBody: Prisma.InputJsonValue | Prisma.JsonNullValueInput = Prisma.JsonNull;
|
||||||
|
|
||||||
|
try {
|
||||||
|
responseBody = JSON.parse(body);
|
||||||
|
} catch (err) {
|
||||||
|
responseBody = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.webhookCall.create({
|
||||||
|
data: {
|
||||||
|
url,
|
||||||
|
event,
|
||||||
|
status: response.ok ? WebhookCallStatus.SUCCESS : WebhookCallStatus.FAILED,
|
||||||
|
requestBody: payloadData as Prisma.InputJsonValue,
|
||||||
|
responseCode: response.status,
|
||||||
|
responseBody,
|
||||||
|
responseHeaders: Object.fromEntries(response.headers.entries()),
|
||||||
|
webhookId: webhook.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Webhook execution failed with status ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: response.ok,
|
||||||
|
status: response.status,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,84 +0,0 @@
|
|||||||
import { deletedAccountServiceAccount } from '@documenso/lib/server-only/user/service-accounts/deleted-account';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
import { DocumentStatus } from '@documenso/prisma/client';
|
|
||||||
|
|
||||||
type HandleDocumentOwnershipOnDeletionOptions = {
|
|
||||||
documentIds: number[];
|
|
||||||
organisationOwnerId: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const handleDocumentOwnershipOnDeletion = async ({
|
|
||||||
documentIds,
|
|
||||||
organisationOwnerId,
|
|
||||||
}: HandleDocumentOwnershipOnDeletionOptions) => {
|
|
||||||
if (documentIds.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serviceAccount = await deletedAccountServiceAccount();
|
|
||||||
const serviceAccountTeam = serviceAccount.ownedOrganisations[0].teams[0];
|
|
||||||
|
|
||||||
await prisma.document.deleteMany({
|
|
||||||
where: {
|
|
||||||
id: {
|
|
||||||
in: documentIds,
|
|
||||||
},
|
|
||||||
status: DocumentStatus.DRAFT,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const organisationOwner = await prisma.user.findUnique({
|
|
||||||
where: {
|
|
||||||
id: organisationOwnerId,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
ownedOrganisations: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
teams: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (organisationOwner && organisationOwner.ownedOrganisations.length > 0) {
|
|
||||||
const ownerPersonalTeam = organisationOwner.ownedOrganisations[0].teams[0];
|
|
||||||
|
|
||||||
await prisma.document.updateMany({
|
|
||||||
where: {
|
|
||||||
id: {
|
|
||||||
in: documentIds,
|
|
||||||
},
|
|
||||||
status: {
|
|
||||||
in: [DocumentStatus.PENDING, DocumentStatus.REJECTED, DocumentStatus.COMPLETED],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
userId: organisationOwner.id,
|
|
||||||
teamId: ownerPersonalTeam.id,
|
|
||||||
deletedAt: new Date(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await prisma.document.updateMany({
|
|
||||||
where: {
|
|
||||||
id: {
|
|
||||||
in: documentIds,
|
|
||||||
},
|
|
||||||
status: {
|
|
||||||
in: [DocumentStatus.PENDING, DocumentStatus.REJECTED, DocumentStatus.COMPLETED],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
userId: serviceAccount.id,
|
|
||||||
teamId: serviceAccountTeam.id,
|
|
||||||
deletedAt: new Date(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -5,20 +5,6 @@ export const deletedAccountServiceAccount = async () => {
|
|||||||
where: {
|
where: {
|
||||||
email: 'deleted-account@documenso.com',
|
email: 'deleted-account@documenso.com',
|
||||||
},
|
},
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
email: true,
|
|
||||||
ownedOrganisations: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
teams: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!serviceAccount) {
|
if (!serviceAccount) {
|
||||||
|
|||||||
@ -13,6 +13,7 @@ export type HandlerTriggerWebhooksResponse =
|
|||||||
error: string;
|
error: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Todo: [Webhooks] delete after deployment.
|
||||||
export const handlerTriggerWebhooks = async (req: Request) => {
|
export const handlerTriggerWebhooks = async (req: Request) => {
|
||||||
const signature = req.headers.get('x-webhook-signature');
|
const signature = req.headers.get('x-webhook-signature');
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { WebhookTriggerEvents } from '@prisma/client';
|
import type { WebhookTriggerEvents } from '@prisma/client';
|
||||||
|
|
||||||
import { NEXT_PRIVATE_INTERNAL_WEBAPP_URL } from '../../../constants/app';
|
import { jobs } from '../../../jobs/client';
|
||||||
import { sign } from '../../crypto/sign';
|
|
||||||
import { getAllWebhooksByEventTrigger } from '../get-all-webhooks-by-event-trigger';
|
import { getAllWebhooksByEventTrigger } from '../get-all-webhooks-by-event-trigger';
|
||||||
|
|
||||||
export type TriggerWebhookOptions = {
|
export type TriggerWebhookOptions = {
|
||||||
@ -13,35 +12,26 @@ export type TriggerWebhookOptions = {
|
|||||||
|
|
||||||
export const triggerWebhook = async ({ event, data, userId, teamId }: TriggerWebhookOptions) => {
|
export const triggerWebhook = async ({ event, data, userId, teamId }: TriggerWebhookOptions) => {
|
||||||
try {
|
try {
|
||||||
const body = {
|
|
||||||
event,
|
|
||||||
data,
|
|
||||||
userId,
|
|
||||||
teamId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const registeredWebhooks = await getAllWebhooksByEventTrigger({ event, userId, teamId });
|
const registeredWebhooks = await getAllWebhooksByEventTrigger({ event, userId, teamId });
|
||||||
|
|
||||||
if (registeredWebhooks.length === 0) {
|
if (registeredWebhooks.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const signature = sign(body);
|
await Promise.allSettled(
|
||||||
|
registeredWebhooks.map(async (webhook) => {
|
||||||
await Promise.race([
|
await jobs.triggerJob({
|
||||||
fetch(`${NEXT_PRIVATE_INTERNAL_WEBAPP_URL()}/api/webhook/trigger`, {
|
name: 'internal.execute-webhook',
|
||||||
method: 'POST',
|
payload: {
|
||||||
headers: {
|
event,
|
||||||
'content-type': 'application/json',
|
webhookId: webhook.id,
|
||||||
'x-webhook-signature': signature,
|
data,
|
||||||
},
|
},
|
||||||
body: JSON.stringify(body),
|
});
|
||||||
}),
|
}),
|
||||||
new Promise((_, reject) => {
|
);
|
||||||
setTimeout(() => reject(new Error('Request timeout')), 500);
|
|
||||||
}),
|
|
||||||
]).catch(() => null);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
throw new Error(`Failed to trigger webhook`);
|
throw new Error(`Failed to trigger webhook`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import {
|
|||||||
ORGANISATION_USER_ACCOUNT_TYPE,
|
ORGANISATION_USER_ACCOUNT_TYPE,
|
||||||
} from '@documenso/lib/constants/organisations';
|
} from '@documenso/lib/constants/organisations';
|
||||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
import { handleDocumentOwnershipOnDeletion } from '@documenso/lib/server-only/document/handle-document-ownership-on-deletion';
|
|
||||||
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
@ -33,24 +32,6 @@ export const deleteOrganisationRoute = authenticatedProcedure
|
|||||||
userId: user.id,
|
userId: user.id,
|
||||||
roles: ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP['DELETE_ORGANISATION'],
|
roles: ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP['DELETE_ORGANISATION'],
|
||||||
}),
|
}),
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
owner: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
teams: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
documents: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!organisation) {
|
if (!organisation) {
|
||||||
@ -59,15 +40,6 @@ export const deleteOrganisationRoute = authenticatedProcedure
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const documentIds = organisation.teams.flatMap((team) => team.documents.map((doc) => doc.id));
|
|
||||||
|
|
||||||
if (documentIds && documentIds.length > 0) {
|
|
||||||
await handleDocumentOwnershipOnDeletion({
|
|
||||||
documentIds,
|
|
||||||
organisationOwnerId: organisation.owner.id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
await tx.account.deleteMany({
|
await tx.account.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
import { ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP } from '@documenso/lib/constants/organisations';
|
|
||||||
import { handleDocumentOwnershipOnDeletion } from '@documenso/lib/server-only/document/handle-document-ownership-on-deletion';
|
|
||||||
import { deleteTeam } from '@documenso/lib/server-only/team/delete-team';
|
import { deleteTeam } from '@documenso/lib/server-only/team/delete-team';
|
||||||
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
|
||||||
import { prisma } from '@documenso/prisma';
|
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
import { authenticatedProcedure } from '../trpc';
|
||||||
import { ZDeleteTeamRequestSchema, ZDeleteTeamResponseSchema } from './delete-team.types';
|
import { ZDeleteTeamRequestSchema, ZDeleteTeamResponseSchema } from './delete-team.types';
|
||||||
@ -15,53 +11,12 @@ export const deleteTeamRoute = authenticatedProcedure
|
|||||||
const { teamId } = input;
|
const { teamId } = input;
|
||||||
const { user } = ctx;
|
const { user } = ctx;
|
||||||
|
|
||||||
const team = await prisma.team.findUnique({
|
|
||||||
where: {
|
|
||||||
id: teamId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const organisation = await prisma.organisation.findFirst({
|
|
||||||
where: buildOrganisationWhereQuery({
|
|
||||||
organisationId: team?.organisationId,
|
|
||||||
userId: user.id,
|
|
||||||
roles: ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP['DELETE_ORGANISATION'],
|
|
||||||
}),
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
owner: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
teams: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
documents: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.logger.info({
|
ctx.logger.info({
|
||||||
input: {
|
input: {
|
||||||
teamId,
|
teamId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const documentIds = organisation?.teams.flatMap((team) => team.documents.map((doc) => doc.id));
|
|
||||||
|
|
||||||
if (documentIds && documentIds.length > 0 && organisation) {
|
|
||||||
await handleDocumentOwnershipOnDeletion({
|
|
||||||
documentIds,
|
|
||||||
organisationOwnerId: organisation.owner.id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await deleteTeam({
|
await deleteTeam({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
teamId,
|
teamId,
|
||||||
|
|||||||
Reference in New Issue
Block a user