Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

fix(embedded): fix expiry notice and deadline copy for embedded envelopes (#247)

Kevin Rosef94e13bb46a5
diff --git a/src/components/envelopes/envelope-detail-shell.tsx b/src/components/envelopes/envelope-detail-shell.tsx
index ee8cbda..418ba48 100644
--- a/src/components/envelopes/envelope-detail-shell.tsx
+++ b/src/components/envelopes/envelope-detail-shell.tsx
@@ -33,7 +33,10 @@ import {
canEditAutomaticReminders,
describeAutomaticReminders,
describeCopyRecipientDelivery,
+ describeDeadlineDialogNote,
+ describeEnvelopeDeadline,
} from "@/lib/envelope-detail-copy";
+import type { EnvelopeDelivery } from "@/lib/schemas/envelope";
import { formatReminderInterval } from "@/lib/services/reminders";
import {
DEFAULT_ENVELOPE_DEADLINE_DAYS,
@@ -250,11 +253,11 @@ export function EnvelopeDetailShell({
: null;
const deadlineText = !deadline
? null
- : isExpired
- ? `Expired ${deadline.expiresAtLabel}. Nobody can sign until you set a new deadline.`
- : deadline.expiresAtLabel
- ? `Expires ${deadline.expiresAtLabel}.`
- : "No deadline. Each signing link lasts seven days.";
+ : describeEnvelopeDeadline({
+ delivery: record.delivery,
+ expired: isExpired,
+ expiresAtLabel: deadline.expiresAtLabel,
+ });
const isEmbedded = record.delivery === "embedded";
const reminderIntervalDays = isEmbedded
? null
@@ -1056,6 +1059,7 @@ export function EnvelopeDetailShell({
{activeModal === "change-deadline" ? (
<DeadlineDialog
+ delivery={record.delivery}
errorMessage={actionError}
expired={isExpired}
onCancel={() => setActiveModal(null)}
@@ -1290,6 +1294,7 @@ function CopyDraftDialog({
}
type DeadlineDialogProps = {
+ delivery?: EnvelopeDelivery;
errorMessage?: string | null;
expired: boolean;
onCancel: () => void;
@@ -1298,6 +1303,7 @@ type DeadlineDialogProps = {
};
function DeadlineDialog({
+ delivery,
errorMessage,
expired,
onCancel,
@@ -1323,12 +1329,7 @@ function DeadlineDialog({
</DialogTitle>
<DialogDescription asChild>
<div className="space-y-2 text-sm leading-6 text-(--secondary)">
- <p>
- The new deadline counts from now.{" "}
- {expired
- ? "The signing link that was already sent will work again."
- : "The signing link that was already sent keeps working until then."}
- </p>
+ <p>{describeDeadlineDialogNote({ delivery, expired })}</p>
</div>
</DialogDescription>
</DialogHeader>
diff --git a/src/lib/envelope-deadline.dbtest.ts b/src/lib/envelope-deadline.dbtest.ts
index 2922797..04272e6 100644
--- a/src/lib/envelope-deadline.dbtest.ts
+++ b/src/lib/envelope-deadline.dbtest.ts
@@ -65,9 +65,11 @@ function unixNow() {
/** An envelope out for signature, waiting on its one signer. */
async function createSentEnvelope({
+ delivery = "email",
expiresAt,
status = "sent",
}: {
+ delivery?: "email" | "embedded";
expiresAt: number | null;
status?: "draft" | "sent";
}) {
@@ -88,14 +90,15 @@ async function createSentEnvelope({
await database.query(
`
insert into envelopes (
- id, user_id, title, status, source_pdf_key, source_pdf_filename,
+ id, user_id, title, status, delivery, source_pdf_key, source_pdf_filename,
source_pdf_size_bytes, page_count, source_pdf_hash, sent_at, expires_at
- ) values ($1, $2, 'Lease', $3, $4, 'lease.pdf', 10, 1, $5, $6, $7)
+ ) values ($1, $2, 'Lease', $3, $4, $5, 'lease.pdf', 10, 1, $6, $7, $8)
`,
[
envelopeId,
userId,
status,
+ delivery,
`test/${envelopeId}.pdf`,
"a".repeat(64),
status === "sent" ? sentAt : null,
@@ -347,6 +350,46 @@ test("the daily job tells the sender once that a deadline passed", async () => {
assert.equal((await verifyAuditChain(expired.envelopeId, database)).ok, true);
});
+test("expiry notices for embedded envelopes explain requesting a fresh link instead of reusing an expired one (issue #247)", async () => {
+ const now = unixNow();
+ const embedded = await createSentEnvelope({
+ delivery: "embedded",
+ expiresAt: now - 60,
+ });
+ const emailed = await createSentEnvelope({
+ delivery: "email",
+ expiresAt: now - 60,
+ });
+ const { sent, service } = recordingEmailService();
+
+ const summary = await runEnvelopeSchedule({
+ database,
+ emailService: service,
+ now,
+ });
+
+ assert.equal(summary.expiryNoticeFailures.length, 0);
+
+ const embeddedNotice = sent.find((message) =>
+ message.to.startsWith(embedded.userId),
+ );
+ const emailedNotice = sent.find((message) =>
+ message.to.startsWith(emailed.userId),
+ );
+
+ assert.ok(embeddedNotice);
+ assert.ok(emailedNotice);
+ assert.doesNotMatch(
+ embeddedNotice.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+ assert.match(embeddedNotice.textBody, /request a fresh signing link/);
+ assert.match(
+ emailedNotice.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+});
+
test("a notice whose email fails is tried again on the next run", async () => {
const now = unixNow();
const { envelopeId, userId } = await createSentEnvelope({
diff --git a/src/lib/envelope-detail-copy.test.ts b/src/lib/envelope-detail-copy.test.ts
index bf77baf..3380846 100644
--- a/src/lib/envelope-detail-copy.test.ts
+++ b/src/lib/envelope-detail-copy.test.ts
@@ -5,6 +5,8 @@ import {
canEditAutomaticReminders,
describeAutomaticReminders,
describeCopyRecipientDelivery,
+ describeDeadlineDialogNote,
+ describeEnvelopeDeadline,
} from "./envelope-detail-copy.ts";
test("copy recipient status copy distinguishes terminal outcomes from pending delivery (issue #238)", () => {
@@ -89,3 +91,73 @@ test("automatic reminder copy and editability reflect embedded vs email delivery
);
assert.equal(canEditAutomaticReminders({ isOwnEnvelope: true }), true);
});
+
+test("deadline banner and dialog copy match embedded vs email link recovery (issue #247)", () => {
+ const label = "November 24, 2026 at 6:34 AM UTC";
+
+ const embeddedExpiredBanner = describeEnvelopeDeadline({
+ delivery: "embedded",
+ expired: true,
+ expiresAtLabel: label,
+ });
+
+ assert.equal(
+ embeddedExpiredBanner,
+ `Expired ${label}. Nobody can sign until you set a new deadline and your app requests a fresh signing link.`,
+ );
+ assert.equal(
+ describeEnvelopeDeadline({
+ delivery: "email",
+ expired: true,
+ expiresAtLabel: label,
+ }),
+ `Expired ${label}. Nobody can sign until you set a new deadline.`,
+ );
+ assert.equal(
+ describeEnvelopeDeadline({
+ delivery: "embedded",
+ expired: false,
+ expiresAtLabel: label,
+ }),
+ `Expires ${label}.`,
+ );
+ assert.equal(
+ describeEnvelopeDeadline({
+ delivery: "email",
+ expired: false,
+ expiresAtLabel: null,
+ }),
+ "No deadline. Each signing link lasts seven days.",
+ );
+
+ const embeddedExpiredNote = describeDeadlineDialogNote({
+ delivery: "embedded",
+ expired: true,
+ });
+
+ assert.doesNotMatch(embeddedExpiredNote, /will work again/i);
+ assert.match(embeddedExpiredNote, /stay expired/i);
+ assert.match(embeddedExpiredNote, /request a fresh signing link/i);
+
+ const embeddedActiveNote = describeDeadlineDialogNote({
+ delivery: "embedded",
+ expired: false,
+ });
+
+ assert.doesNotMatch(embeddedActiveNote, /already sent keeps working/i);
+ assert.match(embeddedActiveNote, /keep their own expiry/i);
+ assert.match(embeddedActiveNote, /request a fresh signing link/i);
+
+ assert.equal(
+ describeDeadlineDialogNote({ delivery: "email", expired: true }),
+ "The new deadline counts from now. The signing link that was already sent will work again.",
+ );
+ assert.equal(
+ describeDeadlineDialogNote({ delivery: "email", expired: false }),
+ "The new deadline counts from now. The signing link that was already sent keeps working until then.",
+ );
+ assert.equal(
+ describeDeadlineDialogNote({ expired: true }),
+ "The new deadline counts from now. The signing link that was already sent will work again.",
+ );
+});
diff --git a/src/lib/envelope-detail-copy.ts b/src/lib/envelope-detail-copy.ts
index c4aa017..4638f3d 100644
--- a/src/lib/envelope-detail-copy.ts
+++ b/src/lib/envelope-detail-copy.ts
@@ -45,3 +45,42 @@ export function canEditAutomaticReminders(input: {
}): boolean {
return input.isOwnEnvelope && input.delivery !== "embedded";
}
+
+/**
+ * Deadline banner text on the sender's envelope detail page while the
+ * envelope is out for signature.
+ */
+export function describeEnvelopeDeadline(input: {
+ delivery?: EnvelopeDelivery;
+ expired: boolean;
+ expiresAtLabel: string | null;
+}): string {
+ if (input.expired) {
+ return input.delivery === "embedded"
+ ? `Expired ${input.expiresAtLabel}. Nobody can sign until you set a new deadline and your app requests a fresh signing link.`
+ : `Expired ${input.expiresAtLabel}. Nobody can sign until you set a new deadline.`;
+ }
+
+ return input.expiresAtLabel
+ ? `Expires ${input.expiresAtLabel}.`
+ : "No deadline. Each signing link lasts seven days.";
+}
+
+/**
+ * Recovery explanation inside the Set/Change deadline dialog. Embedded links
+ * keep their own short lifetime and never revive after expiry.
+ */
+export function describeDeadlineDialogNote(input: {
+ delivery?: EnvelopeDelivery;
+ expired: boolean;
+}): string {
+ if (input.delivery === "embedded") {
+ return input.expired
+ ? "The new deadline counts from now. Previous embedded signing links stay expired, so your app will need to request a fresh signing link."
+ : "The new deadline counts from now. Existing embedded signing links keep their own expiry; your app can request a fresh signing link until the new deadline.";
+ }
+
+ return input.expired
+ ? "The new deadline counts from now. The signing link that was already sent will work again."
+ : "The new deadline counts from now. The signing link that was already sent keeps working until then.";
+}
diff --git a/src/lib/envelope-schedule.test.ts b/src/lib/envelope-schedule.test.ts
new file mode 100644
index 0000000..ee2955b
--- /dev/null
+++ b/src/lib/envelope-schedule.test.ts
@@ -0,0 +1,223 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+
+import type { Pool, PoolClient, QueryResult } from "pg";
+
+import { buildExpiryNotice, runEnvelopeSchedule } from "./envelope-schedule.ts";
+import type {
+ EmailService,
+ TransactionalEmailMessage,
+} from "./services/email.ts";
+
+const EXPIRED_AT = Date.parse("2026-11-24T06:34:00.000Z") / 1000;
+
+test("buildExpiryNotice tells embedded senders to request a fresh signing link instead of promising reuse (issue #247)", () => {
+ const previousAppUrl = process.env.APP_URL;
+ process.env.APP_URL = "https://sign.example.test";
+
+ try {
+ const embeddedNotice = buildExpiryNotice({
+ delivery: "embedded",
+ expiresAt: EXPIRED_AT,
+ id: "env-embedded",
+ ownerEmail: "[email protected]",
+ ownerName: "Example Sender",
+ title: "Portal agreement",
+ waitingOn: "Robin Example ([email protected])",
+ });
+
+ assert.equal(embeddedNotice.template, "expiry_notice");
+ assert.equal(
+ embeddedNotice.subject,
+ '"Portal agreement" expired before everyone signed',
+ );
+ assert.equal(embeddedNotice.to, "[email protected]");
+ assert.doesNotMatch(
+ embeddedNotice.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+ assert.match(
+ embeddedNotice.textBody,
+ /open the envelope and set a new deadline/,
+ );
+ assert.match(
+ embeddedNotice.textBody,
+ /Previous embedded signing links stay expired/,
+ );
+ assert.match(embeddedNotice.textBody, /request a fresh signing link/);
+ assert.match(
+ embeddedNotice.textBody,
+ /https:\/\/sign\.example\.test\/envelopes\/env-embedded$/,
+ );
+
+ const emailNotice = buildExpiryNotice({
+ delivery: "email",
+ expiresAt: EXPIRED_AT,
+ id: "env-email",
+ ownerEmail: "[email protected]",
+ ownerName: "Example Sender",
+ title: "Email agreement",
+ waitingOn: "Robin Example ([email protected])",
+ });
+
+ assert.equal(emailNotice.template, "expiry_notice");
+ assert.match(
+ emailNotice.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+ assert.match(
+ emailNotice.textBody,
+ /https:\/\/sign\.example\.test\/envelopes\/env-email$/,
+ );
+ } finally {
+ if (previousAppUrl === undefined) {
+ delete process.env.APP_URL;
+ } else {
+ process.env.APP_URL = previousAppUrl;
+ }
+ }
+});
+
+test("runEnvelopeSchedule selects delivery and sends delivery-accurate expiry notices (issue #247)", async () => {
+ const previousAppUrl = process.env.APP_URL;
+ process.env.APP_URL = "https://sign.example.test";
+
+ try {
+ const queryResult = <T>(rows: T[], rowCount = rows.length): QueryResult =>
+ ({
+ command: "",
+ fields: [],
+ oid: 0,
+ rowCount,
+ rows,
+ }) as unknown as QueryResult;
+
+ const expiredRows = [
+ {
+ delivery: "embedded" as const,
+ expiresAt: EXPIRED_AT,
+ id: "env-embedded",
+ ownerEmail: "[email protected]",
+ ownerName: "Example Sender",
+ title: "Portal agreement",
+ waitingOn: "Robin Example ([email protected])",
+ },
+ {
+ delivery: "email" as const,
+ expiresAt: EXPIRED_AT,
+ id: "env-email",
+ ownerEmail: "[email protected]",
+ ownerName: "Example Sender",
+ title: "Email agreement",
+ waitingOn: "Robin Example ([email protected])",
+ },
+ ];
+ let selectedDeliveryColumn = false;
+ const sent: TransactionalEmailMessage[] = [];
+
+ const execute = async (
+ text: string,
+ values: unknown[] = [],
+ ): Promise<QueryResult> => {
+ const sql = text.replace(/\s+/g, " ").trim();
+
+ if (sql === "BEGIN" || sql === "COMMIT" || sql === "ROLLBACK") {
+ return queryResult([], 0);
+ }
+
+ if (sql.startsWith("SELECT pg_advisory_xact_lock")) {
+ return queryResult([{ pg_advisory_xact_lock: "" }], 1);
+ }
+
+ if (
+ sql.includes("FROM envelopes e") &&
+ sql.includes("e.expiry_notified_at IS NULL")
+ ) {
+ selectedDeliveryColumn = sql.includes("e.delivery");
+ return queryResult(expiredRows);
+ }
+
+ if (
+ sql.startsWith("SELECT id FROM envelopes") &&
+ sql.includes("expiry_notified_at IS NULL")
+ ) {
+ return queryResult([{ id: String(values[0]) }]);
+ }
+
+ if (sql.startsWith("UPDATE envelopes SET expiry_notified_at = $1")) {
+ return queryResult([], 1);
+ }
+
+ if (sql.includes("SELECT sequence, row_hash")) {
+ return queryResult([]);
+ }
+
+ if (sql.startsWith("INSERT INTO audit_events")) {
+ return queryResult([], 1);
+ }
+
+ if (sql.startsWith("INSERT INTO webhook_deliveries")) {
+ return queryResult([], 0);
+ }
+
+ if (sql.includes("FROM envelopes e JOIN signers s")) {
+ return queryResult([]);
+ }
+
+ throw new Error(`Unexpected SQL in envelope-schedule.test.ts: ${sql}`);
+ };
+
+ const client = {
+ query: (text: string, values?: unknown[]) => execute(text, values),
+ release: () => undefined,
+ } as unknown as PoolClient;
+ const database = {
+ connect: async () => client,
+ query: (text: string, values?: unknown[]) => execute(text, values),
+ } as unknown as Pool;
+ const emailService: EmailService = {
+ async send(message) {
+ sent.push(message);
+ return { accepted: [message.to], messageId: "m-1", rejected: [] };
+ },
+ async sendBatch() {
+ throw new Error("not used");
+ },
+ };
+
+ const summary = await runEnvelopeSchedule({
+ database,
+ emailService,
+ now: EXPIRED_AT + 60,
+ });
+
+ assert.equal(selectedDeliveryColumn, true);
+ assert.equal(summary.expiryNoticesSent, 2);
+ assert.deepEqual(summary.expiryNoticeFailures, []);
+
+ const embeddedMail = sent.find((message) =>
+ message.subject.includes("Portal agreement"),
+ );
+ const emailedMail = sent.find((message) =>
+ message.subject.includes("Email agreement"),
+ );
+
+ assert.ok(embeddedMail);
+ assert.ok(emailedMail);
+ assert.doesNotMatch(
+ embeddedMail.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+ assert.match(embeddedMail.textBody, /request a fresh signing link/);
+ assert.match(
+ emailedMail.textBody,
+ /The signing link that was already sent will work again\./,
+ );
+ } finally {
+ if (previousAppUrl === undefined) {
+ delete process.env.APP_URL;
+ } else {
+ process.env.APP_URL = previousAppUrl;
+ }
+ }
+});
diff --git a/src/lib/envelope-schedule.ts b/src/lib/envelope-schedule.ts
index 1c97acd..b0105b2 100644
--- a/src/lib/envelope-schedule.ts
+++ b/src/lib/envelope-schedule.ts
@@ -54,7 +54,8 @@ type ScheduleRun = {
summary: EnvelopeScheduleSummary;
};
-type ExpiredEnvelopeRow = {
+export type ExpiredEnvelopeRow = {
+ delivery: "email" | "embedded";
expiresAt: number;
id: string;
ownerEmail: string;
@@ -74,6 +75,7 @@ async function listExpiredEnvelopesToNotify(
SELECT
e.id,
e.title,
+ e.delivery,
e.expires_at AS "expiresAt",
u.email AS "ownerEmail",
u.name AS "ownerName",
@@ -168,12 +170,16 @@ async function noteExpiryNoticeSent(
}, database);
}
-function buildExpiryNotice(envelope: ExpiredEnvelopeRow) {
+export function buildExpiryNotice(envelope: ExpiredEnvelopeRow) {
const appUrl = resolveAppUrl();
const link = appUrl ? `${appUrl}/envelopes/${envelope.id}` : null;
const waitingOn = envelope.waitingOn
? ` It was still waiting on ${envelope.waitingOn}.`
: "";
+ const recoveryLine =
+ envelope.delivery === "embedded"
+ ? "To give signers more time, open the envelope and set a new deadline. Previous embedded signing links stay expired, so your app will need to request a fresh signing link before signing can resume. If the document is no longer needed, void it."
+ : "To give signers more time, open the envelope and set a new deadline. The signing link that was already sent will work again. If the document is no longer needed, void it.";
return {
subject: `"${envelope.title}" expired before everyone signed`,
@@ -183,7 +189,7 @@ function buildExpiryNotice(envelope: ExpiredEnvelopeRow) {
"",
`The deadline for "${envelope.title}" passed on ${formatDeadline(envelope.expiresAt)}.${waitingOn} Nobody can sign it now.`,
"",
- "To give signers more time, open the envelope and set a new deadline. The signing link that was already sent will work again. If the document is no longer needed, void it.",
+ recoveryLine,
...(link ? ["", link] : []),
].join("\n"),
to: envelope.ownerEmail,