The Open Source DocuSign Alternative.
TypeScript digital-signaturedocument-signingdocusign-alternativee-signatureesignesignaturenext-authnextjsopen-sourcepades-standardpdfpdf-signpdf-signaturepostgresqlprismaself-hostedsigningtypescript
chore: add cancelled webhook event (#1608)
Catalin Pitb0f8c8313440
diff --git a/apps/documentation/pages/developers/webhooks.mdx b/apps/documentation/pages/developers/webhooks.mdxindex 3ce2e7ee9..1155e32c8 100644--- a/apps/documentation/pages/developers/webhooks.mdx+++ b/apps/documentation/pages/developers/webhooks.mdx@@ -21,6 +21,7 @@ Documenso supports Webhooks and allows you to subscribe to the following events:- `document.signed`- `document.completed`- `document.rejected`+- `document.cancelled`## Create a webhook subscription@@ -37,7 +38,7 @@ Clicking on the "**Create Webhook**" button opens a modal to create a new webhooTo create a new webhook subscription, you need to provide the following information:- Enter the webhook URL that will receive the event payload.-- Select the event(s) you want to subscribe to: `document.created`, `document.sent`, `document.opened`, `document.signed`, `document.completed`, `document.rejected`.+- Select the event(s) you want to subscribe to: `document.created`, `document.sent`, `document.opened`, `document.signed`, `document.completed`, `document.rejected`, `document.cancelled`.- Optionally, you can provide a secret key that will be used to sign the payload. This key will be included in the `X-Documenso-Secret` header of the request.@@ -528,6 +529,96 @@ Example payload for the `document.rejected` event:}```+Example payload for the `document.rejected` event:++```json+{+ "event": "DOCUMENT_CANCELLED",+ "payload": {+ "id": 7,+ "externalId": null,+ "userId": 3,+ "authOptions": null,+ "formValues": null,+ "visibility": "EVERYONE",+ "title": "documenso.pdf",+ "status": "PENDING",+ "documentDataId": "cm6exvn93006hi02ru90a265a",+ "createdAt": "2025-01-27T11:02:14.393Z",+ "updatedAt": "2025-01-27T11:03:16.387Z",+ "completedAt": null,+ "deletedAt": null,+ "teamId": null,+ "templateId": null,+ "source": "DOCUMENT",+ "documentMeta": {+ "id": "cm6exvn96006ji02rqvzjvwoy",+ "subject": "",+ "message": "",+ "timezone": "Etc/UTC",+ "password": null,+ "dateFormat": "yyyy-MM-dd hh:mm a",+ "redirectUrl": "",+ "signingOrder": "PARALLEL",+ "typedSignatureEnabled": true,+ "language": "en",+ "distributionMethod": "EMAIL",+ "emailSettings": {+ "documentDeleted": true,+ "documentPending": true,+ "recipientSigned": true,+ "recipientRemoved": true,+ "documentCompleted": true,+ "ownerDocumentCompleted": true,+ "recipientSigningRequest": true+ }+ },+ "recipients": [+ {+ "id": 7,+ "documentId": 7,+ "templateId": null,+ "email": "[email protected]",+ "name": "Zorita Baird",+ "token": "XkKx1HCs6Znm2UBJA2j6o",+ "documentDeletedAt": null,+ "expired": null,+ "signedAt": null,+ "authOptions": { "accessAuth": null, "actionAuth": null },+ "signingOrder": 1,+ "rejectionReason": null,+ "role": "SIGNER",+ "readStatus": "NOT_OPENED",+ "signingStatus": "NOT_SIGNED",+ "sendStatus": "SENT"+ }+ ],+ "Recipient": [+ {+ "id": 7,+ "documentId": 7,+ "templateId": null,+ "email": "[email protected]",+ "name": "Signer",+ "token": "XkKx1HCs6Znm2UBJA2j6o",+ "documentDeletedAt": null,+ "expired": null,+ "signedAt": null,+ "authOptions": { "accessAuth": null, "actionAuth": null },+ "signingOrder": 1,+ "rejectionReason": null,+ "role": "SIGNER",+ "readStatus": "NOT_OPENED",+ "signingStatus": "NOT_SIGNED",+ "sendStatus": "SENT"+ }+ ]+ },+ "createdAt": "2025-01-27T11:03:27.730Z",+ "webhookEndpoint": "https://mywebhooksite.com/mywebhook"+}+```+## AvailabilityWebhooks are available to individual users and teams.diff --git a/packages/lib/server-only/document/delete-document.ts b/packages/lib/server-only/document/delete-document.tsindex b1fb58c4c..886588214 100644--- a/packages/lib/server-only/document/delete-document.ts+++ b/packages/lib/server-only/document/delete-document.ts@@ -9,7 +9,7 @@ import type {TeamGlobalSettings,User,} from '@prisma/client';-import { DocumentStatus, SendStatus } from '@prisma/client';+import { DocumentStatus, SendStatus, WebhookTriggerEvents } from '@prisma/client';import { mailer } from '@documenso/email/mailer';import DocumentCancelTemplate from '@documenso/email/templates/document-cancel';@@ -21,10 +21,15 @@ import { FROM_ADDRESS, FROM_NAME } from '../../constants/email';import { AppError, AppErrorCode } from '../../errors/app-error';import { DOCUMENT_AUDIT_LOG_TYPE } from '../../types/document-audit-logs';import { extractDerivedDocumentEmailSettings } from '../../types/document-email';+import {+ ZWebhookDocumentSchema,+ mapDocumentToWebhookDocumentPayload,+} from '../../types/webhook-payload';import type { ApiRequestMetadata } from '../../universal/extract-request-metadata';import { createDocumentAuditLogData } from '../../utils/document-audit-logs';import { renderEmailWithI18N } from '../../utils/render-email-with-i18n';import { teamGlobalSettingsToBranding } from '../../utils/team-global-settings-to-branding';+import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';export type DeleteDocumentOptions = {id: number;@@ -110,6 +115,13 @@ export const deleteDocument = async ({});}+ await triggerWebhook({+ event: WebhookTriggerEvents.DOCUMENT_CANCELLED,+ data: ZWebhookDocumentSchema.parse(mapDocumentToWebhookDocumentPayload(document)),+ userId,+ teamId,+ });+// Return partial document for API v1 response.return {id: document.id,diff --git a/packages/prisma/migrations/20250124135853_add_cancelled_event_webhooks/migration.sql b/packages/prisma/migrations/20250124135853_add_cancelled_event_webhooks/migration.sqlnew file mode 100644index 000000000..51b9aaa08--- /dev/null+++ b/packages/prisma/migrations/20250124135853_add_cancelled_event_webhooks/migration.sql@@ -0,0 +1,2 @@+-- AlterEnum+ALTER TYPE "WebhookTriggerEvents" ADD VALUE 'DOCUMENT_CANCELLED';diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prismaindex 98926e3ad..c80c0ad3e 100644--- a/packages/prisma/schema.prisma+++ b/packages/prisma/schema.prisma@@ -176,6 +176,7 @@ enum WebhookTriggerEvents {DOCUMENT_SIGNEDDOCUMENT_COMPLETEDDOCUMENT_REJECTED+ DOCUMENT_CANCELLED}model Webhook {