Skip to content

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

TypeScript

Clear a bulk-send row's failure reason when its envelope is deleted

Matt Van Horne62cfc94a42f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99b71c7..3a498ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,9 +8,11 @@ release says when it does.
## [Unreleased]
-Send the same template to many people at once from a CSV. This needs a
-migration step (`20261118090000_bulk_send_batches.sql`): a confirmed batch
-stores its results so a retry does not send the same envelopes again.
+Send the same template to many people at once from a CSV. This needs two
+migration steps (`20261118090000_bulk_send_batches.sql`,
+`20261118120000_bulk_send_row_reason_cleared_with_envelope.sql`): a confirmed
+batch stores its results so a retry does not send the same envelopes again,
+and a stored failure reason is cleared when its envelope is deleted.
### Templates
diff --git a/db/migrations/20261118120000_bulk_send_row_reason_cleared_with_envelope.sql b/db/migrations/20261118120000_bulk_send_row_reason_cleared_with_envelope.sql
new file mode 100644
index 0000000..a08baa9
--- /dev/null
+++ b/db/migrations/20261118120000_bulk_send_row_reason_cleared_with_envelope.sql
@@ -0,0 +1,27 @@
+-- A send-to-many row that failed after its draft was made keeps a reason
+-- that can name the signer (a Part 11 account problem does). When that
+-- envelope is deleted, by the stale draft cleanup or any other path, the
+-- row's envelope link is set to null and erasing the signer's data can no
+-- longer find the row. So clear the reason as its envelope goes. The row
+-- and its status stay, so a retry of the batch still does not send it
+-- again.
+
+create or replace function public.clear_bulk_send_row_reason()
+returns trigger
+language plpgsql
+as $$
+begin
+ update public.bulk_send_batch_rows
+ set reason = null
+ where envelope_id = old.id and reason is not null;
+
+ return old;
+end;
+$$;
+
+drop trigger if exists envelopes_clear_bulk_send_row_reason
+ on public.envelopes;
+
+create trigger envelopes_clear_bulk_send_row_reason
+ before delete on public.envelopes
+ for each row execute function public.clear_bulk_send_row_reason();
diff --git a/src/lib/bulk-send.dbtest.ts b/src/lib/bulk-send.dbtest.ts
index 86e88af..d7546a3 100644
--- a/src/lib/bulk-send.dbtest.ts
+++ b/src/lib/bulk-send.dbtest.ts
@@ -1125,6 +1125,55 @@ test("erasing a signer clears a saved failure reason that names them", async ()
assert.equal(stored.rows[0]?.reason, null);
});
+test("deleting a failed row's draft clears its reason and keeps its status", async () => {
+ const userId = await createUser();
+ const template = await createOneRoleTemplate(userId);
+ const email = `ann-${randomUUID()}@example.test`;
+ const body = confirmBody(`name,email\nAnn Example,${email}\n`);
+ const envelopeId = await leaveProcessingDraft(userId, template.id, body);
+
+ await database.query(
+ `
+ update bulk_send_batch_rows
+ set status = 'failed', reason = $2
+ where envelope_id = $1
+ `,
+ [envelopeId, `Ann Example (${email}) needs an account on this site.`],
+ );
+
+ const batch = await database.query<{ id: string }>(
+ `select batch_id as id from bulk_send_batch_rows where envelope_id = $1`,
+ [envelopeId],
+ );
+ const batchId = batch.rows[0]!.id;
+
+ // The stale draft cleanup deletes the draft row the same way.
+ const deleted = await database.query(
+ `delete from envelopes where id = $1 and status = 'draft'`,
+ [envelopeId],
+ );
+
+ assert.equal(deleted.rowCount, 1);
+
+ const stored = await database.query<{
+ envelopeId: string | null;
+ reason: string | null;
+ status: string;
+ }>(
+ `
+ select envelope_id as "envelopeId", reason, status
+ from bulk_send_batch_rows
+ where batch_id = $1
+ `,
+ [batchId],
+ );
+
+ assert.equal(stored.rowCount, 1);
+ assert.equal(stored.rows[0]?.status, "failed");
+ assert.equal(stored.rows[0]?.envelopeId, null);
+ assert.equal(stored.rows[0]?.reason, null);
+});
+
async function takeOverBatch(userId: string, batchKey: unknown) {
await database.query(
`