Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Envelope deletion: a cancellation email can arrive after the notice of a deletion scheduled again
rayfernando13370b8113e9642b
diff --git a/src/lib/envelope-deletion.dbtest.ts b/src/lib/envelope-deletion.dbtest.tsindex f2937af..2d8e0a1 100644--- a/src/lib/envelope-deletion.dbtest.ts+++ b/src/lib/envelope-deletion.dbtest.ts@@ -806,6 +806,55 @@ test("a deletion scheduled again while its cancellation email is going out waits);});+test("a deletion scheduled again while the cancellation email is going out at once waits, so its own notice comes after", async () => {+ sent = [];+ const admin = await createUser("admin");+ const otherAdmin = await createUser("admin");+ const owner = await createUser("user");+ const envelope = await createEnvelope(owner.userId, { status: "completed" });+ const start = 1_100_000_000;++ await scheduleAs(admin, envelope.envelopeId, undefined, { now: start });++ // As the cancellation email goes out, another administrator schedules it+ // again; that waits for the email, then sends its own.+ const outbox = createOutbox();+ let rescheduling: Promise<unknown> | undefined;+ const reschedulingOutbox: EmailService = {+ ...outbox,+ async send(message, options) {+ if (!rescheduling && message.to === owner.email) {+ rescheduling = scheduleAs(otherAdmin, envelope.envelopeId, "Again.", {+ now: start + 2,+ });+ await delay(300);+ }++ return outbox.send(message, options);+ },+ };++ await cancelAs(admin, envelope.envelopeId, {+ emailService: reschedulingOutbox,+ now: start + 1,+ });+ await rescheduling;++ assert.deepEqual(+ emailsTo(owner.email)+ .map((message) => message.subject)+ .slice(1),+ [+ '"Lease" will not be deleted',+ `"Lease" will be deleted on ${new Date((start + 2 + WAIT) * 1000).toISOString().slice(0, 10)}`,+ ],+ );+ assert.equal(+ (await getScheduledEnvelopeDeletion(envelope.envelopeId, database))?.reason,+ "Again.",+ );+});+test("notices that keep failing go behind the others, so later senders are still told", async () => {sent = [];const admin = await createUser("admin");diff --git a/src/lib/envelope-deletion.ts b/src/lib/envelope-deletion.tsindex 6e2b2b2..2a90867 100644--- a/src/lib/envelope-deletion.ts+++ b/src/lib/envelope-deletion.ts@@ -583,21 +583,29 @@ export async function cancelEnvelopeDeletion(envelopeId: envelope.id,ownerEmail: owner.email,ownerName: owner.name,+ requestedAt: schedule.requestedAt,title: envelope.title,}: null,};}, database);+ const { notify } = cancelled;- // A failure here is tried again by the daily run.- if (cancelled.notify) {+ // A failure here is tried again by the daily run. As there, the request+ // stays locked while the email goes out, so a deletion scheduled again+ // meanwhile waits and its own notice comes after.+ if (notify) {try {- await sendCancellationNotice(- cancelled.notify,- now,- database,- emailService ?? createEmailService(),- );+ await withTransaction(async (tx) => {+ if (await lockOwedNotice(notify, tx)) {+ await sendCancellationNotice(+ notify,+ now,+ tx,+ emailService ?? createEmailService(),+ );+ }+ }, database);} catch (error) {console.error(`Telling the sender about the cancelled deletion of envelope ${envelopeId} failed:`,@@ -729,7 +737,7 @@ const CANCELLED_GIVE_UP_SECONDS = 30 * DAY_IN_SECONDS;* email comes after.*/async function lockOwedNotice(- row: PendingNoticeRow,+ row: Pick<PendingNoticeRow, "cancelledAt" | "envelopeId" | "requestedAt">,tx: DbExecutor,): Promise<boolean> {const result =