Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Email signers who already signed when an envelope is voided
Claudef40baec4f463
diff --git a/src/components/envelopes/envelope-detail-shell.tsx b/src/components/envelopes/envelope-detail-shell.tsxindex fbc74a6..eb13138 100644--- a/src/components/envelopes/envelope-detail-shell.tsx+++ b/src/components/envelopes/envelope-detail-shell.tsx@@ -1118,7 +1118,7 @@ export function EnvelopeDetailShell({confirmLabel="Void envelope"description={["This will cancel the signing process.",- "Notified signers will be informed that the envelope has been voided.",+ "Signers who were sent the envelope, including those who already signed, will be told it was voided.","This action cannot be undone.",]}confirmDisabled={record.part11 && voidReason.trim().length === 0}diff --git a/src/lib/envelopes.ts b/src/lib/envelopes.tsindex 010638a..75520b8 100644--- a/src/lib/envelopes.ts+++ b/src/lib/envelopes.ts@@ -1561,18 +1561,23 @@ function buildVoidNotificationEmail(input: {senderName: string;signerEmail: string;signerName: string;+ /** The signer had already signed, so the document they signed won't be completed. */+ signerSigned?: boolean;}) {const subject = `"${input.envelopeTitle}" was voided by ${input.senderName}`;+ const outcome = input.signerSigned+ ? "The signing process has been canceled, so the document you signed will not be completed. No further action is needed."+ : "The signing process has been canceled. No further action is needed.";const textBody = [`Hi ${input.signerName},`,"",`${input.senderName} voided "${input.envelopeTitle}" in ${input.branding.productName}.`,- "The signing process has been canceled. No further action is needed.",+ outcome,].join("\n");const htmlBody = [`<p>Hi ${escapeHtml(input.signerName)},</p>`,`<p>${escapeHtml(input.senderName)} voided “${escapeHtml(input.envelopeTitle)}” in ${escapeHtml(input.branding.productName)}.</p>`,- "<p>The signing process has been canceled. No further action is needed.</p>",+ `<p>${escapeHtml(outcome)}</p>`,].join("");const legalFooter = withUserEmailLegalFooter({@@ -3349,6 +3354,12 @@ export async function voidSentEnvelope((signer) => signer.status === "notified",);const currentSigner = notifiedSigners[0];+ // Everyone who was sent the envelope hears that it was voided: the signer+ // whose turn it is, and those who already signed it. Signers still waiting+ // for their turn were never invited, so they get nothing.+ const invitedSigners = signers.filter(+ (signer) => signer.status === "notified" || signer.status === "signed",+ );const voidedAt = Math.floor(Date.now() / 1000);const emailService = createEmailService();const storageService = createStorageService();@@ -3565,7 +3576,7 @@ export async function voidSentEnvelope(const branding = await getBranding().catch(() => null);await Promise.all(- notifiedSigners.map(async (signer) => {+ invitedSigners.map(async (signer) => {try {if (!branding) {throw new Error(@@ -3581,6 +3592,7 @@ export async function voidSentEnvelope(senderName: input.senderName,signerEmail: signer.email,signerName: signer.name,+ signerSigned: signer.status === "signed",}),);diff --git a/src/lib/void-notification.dbtest.ts b/src/lib/void-notification.dbtest.tsnew file mode 100644index 0000000..4dd154b--- /dev/null+++ b/src/lib/void-notification.dbtest.ts@@ -0,0 +1,242 @@+import assert from "node:assert/strict";+import { createHash, randomUUID } from "node:crypto";+import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import path from "node:path";+import { after, before, test } from "node:test";++import { PDFDocument } from "pdf-lib";+import type pg from "pg";++import { openTestDatabase } from "./db/test-database.ts";+import { voidSentEnvelope } from "./envelopes.ts";+import { listAuditEventsForEnvelope } from "./services/audit.ts";+import { issueSigningLink } from "./services/signing-links.ts";+import {+ createStorageService,+ type StorageService,+} from "./services/storage.ts";++// Who hears that an envelope was voided, against a real Postgres. Run with+// `npm run test:db`.++process.env.APP_URL ??= "https://sign.example.test";++const DAY = 24 * 60 * 60;++let database: pg.Pool;+let storageDir: string;+let outboxDir: string;+let storageService: StorageService;+const createdUsers: string[] = [];++before(async () => {+ database = await openTestDatabase();+ globalThis.__signudocPgPool = database;+ storageDir = await mkdtemp(path.join(tmpdir(), "signudoc-void-"));+ outboxDir = path.join(storageDir, "outbox");+ Object.assign(process.env, {+ EMAIL_CAPTURE_DIR: outboxDir,+ EMAIL_DRIVER: "capture",+ STORAGE_DRIVER: "local",+ STORAGE_LOCAL_DIR: storageDir,+ });+ storageService = createStorageService();+});++after(async () => {+ await database.query(`delete from public."user" where id = any($1)`, [+ createdUsers,+ ]);+ await database.end();+ await rm(storageDir, { force: true, recursive: true });+});++type SignerSetup = {+ status: "pending" | "notified" | "signed";+};++/** A sent envelope with signers in order, each at the given status. */+async function createSentEnvelope(signerSetups: readonly SignerSetup[]) {+ const userId = randomUUID();+ const envelopeId = randomUUID();+ const now = Math.floor(Date.now() / 1000);+ const sourcePdf = await PDFDocument.create();++ sourcePdf.addPage([612, 792]);++ const sourcePdfBytes = await sourcePdf.save();+ const sourcePdfKey = `test/${envelopeId}.pdf`;++ await storageService.putObject({+ body: sourcePdfBytes,+ contentType: "application/pdf",+ key: sourcePdfKey,+ });++ createdUsers.push(userId);+ await database.query(+ `insert into public."user" (id, name, email, role) values ($1, 'Sam Sender', $2, 'user')`,+ [userId, `${userId}@example.test`],+ );+ await database.query(+ `+ insert into envelopes (+ id, user_id, title, status, source_pdf_key, source_pdf_filename,+ source_pdf_size_bytes, page_count, source_pdf_hash, sent_at, expires_at+ ) values ($1, $2, 'Lease', 'sent', $3, 'lease.pdf', $4, 1, $5, $6, $7)+ `,+ [+ envelopeId,+ userId,+ sourcePdfKey,+ sourcePdfBytes.length,+ createHash("sha256").update(sourcePdfBytes).digest("hex"),+ now,+ now + DAY,+ ],+ );+ await database.query(+ `+ insert into envelope_pages (id, envelope_id, page_number, width_pt, height_pt)+ values ($1, $2, 1, 612, 792)+ `,+ [randomUUID(), envelopeId],+ );++ const signers: { email: string; id: string; status: string }[] = [];++ for (const [index, setup] of signerSetups.entries()) {+ const signerId = randomUUID();+ const email = `${signerId}@example.test`;+ const link =+ setup.status === "notified"+ ? await issueSigningLink({ expiresInSeconds: DAY, issuedAt: now })+ : null;++ await database.query(+ `+ insert into signers (+ id, envelope_id, name, email, sign_order, status,+ signing_token_hash, signing_token_expires_at, last_notified_at,+ signed_at+ ) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)+ `,+ [+ signerId,+ envelopeId,+ `Signer ${String(index + 1)}`,+ email,+ index + 1,+ setup.status,+ link?.tokenHash ?? null,+ link?.exp ?? null,+ setup.status === "pending" ? null : now,+ setup.status === "signed" ? now : null,+ ],+ );+ await database.query(+ `+ insert into fields (+ id, envelope_id, signer_id, type, page, x, y, width, height+ ) values ($1, $2, $3, 'signature', 1, 0.1, 0.1, 0.2, 0.1)+ `,+ [randomUUID(), envelopeId, signerId],+ );+ signers.push({ email, id: signerId, status: setup.status });+ }++ return { envelopeId, signers, userId };+}++async function capturedVoidNotices() {+ const files = await readdir(outboxDir).catch(() => [] as string[]);+ const messages = await Promise.all(+ files+ .filter((file) => file.endsWith(".json"))+ .map(+ async (file) =>+ JSON.parse(await readFile(path.join(outboxDir, file), "utf8")) as {+ template: string;+ text: string;+ to: string | string[];+ },+ ),+ );++ return messages.filter((message) => message.template === "void_notification");+}++function noticesTo(+ notices: Awaited<ReturnType<typeof capturedVoidNotices>>,+ email: string,+) {+ return notices.filter((notice) =>+ (Array.isArray(notice.to) ? notice.to : [notice.to]).includes(email),+ );+}++test("voiding emails the current signer and those who already signed, not those still waiting", async () => {+ const envelope = await createSentEnvelope([+ { status: "signed" },+ { status: "notified" },+ { status: "pending" },+ ]);+ const [signedSigner, currentSigner, waitingSigner] = envelope.signers;++ assert.ok(signedSigner && currentSigner && waitingSigner);++ await voidSentEnvelope(+ envelope.userId,+ envelope.envelopeId,+ { senderEmail: "[email protected]", senderName: "Sam Sender" },+ { database },+ );++ const notices = await capturedVoidNotices();++ assert.equal(noticesTo(notices, signedSigner.email).length, 1);+ assert.equal(noticesTo(notices, currentSigner.email).length, 1);+ assert.equal(noticesTo(notices, waitingSigner.email).length, 0);+ assert.match(+ noticesTo(notices, signedSigner.email)[0]?.text ?? "",+ /the document you signed will not be completed/,+ );+ assert.doesNotMatch(+ noticesTo(notices, currentSigner.email)[0]?.text ?? "",+ /the document you signed/,+ );++ const auditEvents = await listAuditEventsForEnvelope(+ envelope.envelopeId,+ database,+ );+ const voidEmailRecipients = auditEvents+ .filter(+ (event) =>+ event.event === "email_sent" &&+ event.metadata?.template === "void_notification",+ )+ .map((event) => event.metadata?.signerId)+ .sort();++ assert.deepEqual(+ voidEmailRecipients,+ [signedSigner.id, currentSigner.id].sort(),+ );++ // The signer who already signed keeps that status; only the open link of+ // the current signer is cleared.+ const { rows } = await database.query<{+ id: string;+ signing_token_hash: string | null;+ status: string;+ }>(+ `select id, status, signing_token_hash from signers where envelope_id = $1`,+ [envelope.envelopeId],+ );+ const byId = new Map(rows.map((row) => [row.id, row]));++ assert.equal(byId.get(signedSigner.id)?.status, "signed");+ assert.equal(byId.get(currentSigner.id)?.signing_token_hash, null);+});