Skip to content

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

TypeScript

Lock the audit chain before rolling back a failed send or reminder

Claudefdc474678c57
diff --git a/src/lib/envelopes.ts b/src/lib/envelopes.ts
index 1a0f464..89e0d00 100644
--- a/src/lib/envelopes.ts
+++ b/src/lib/envelopes.ts
@@ -43,6 +43,7 @@ import { getBranding } from "./settings.ts";
import { importPdfDocument } from "./services/document-import.ts";
import {
listAuditEventsForEnvelope,
+ lockEnvelopeAuditChain,
recordAuditEvent,
recordEmailSent,
} from "./services/audit.ts";
@@ -1603,6 +1604,7 @@ export async function sendDraftEnvelope(
[envelope.updatedAt, envelopeId],
);
+ await lockEnvelopeAuditChain(envelopeId, tx);
await tx.query(
`
DELETE FROM audit_events
diff --git a/src/lib/reminders.ts b/src/lib/reminders.ts
index 04a640f..94dd562 100644
--- a/src/lib/reminders.ts
+++ b/src/lib/reminders.ts
@@ -12,7 +12,11 @@ import {
} from "./schemas/envelope.ts";
import { type SignerRecord } from "./schemas/signer.ts";
import { getBranding } from "./settings.ts";
-import { recordAuditEvent, recordEmailSent } from "./services/audit.ts";
+import {
+ lockEnvelopeAuditChain,
+ recordAuditEvent,
+ recordEmailSent,
+} from "./services/audit.ts";
import { createEmailService } from "./services/email.ts";
import { withUserEmailLegalFooter } from "./services/email-legal-footer.ts";
import {
@@ -431,6 +435,7 @@ export async function sendManualEnvelopeNudge(
],
);
+ await lockEnvelopeAuditChain(persisted.envelopeId, tx);
await tx.query(
`
DELETE FROM audit_events
diff --git a/src/lib/services/audit.ts b/src/lib/services/audit.ts
index 95f9240..835865a 100644
--- a/src/lib/services/audit.ts
+++ b/src/lib/services/audit.ts
@@ -114,6 +114,22 @@ function isPool(database: DbExecutor): database is ReturnType<typeof getDb> {
return "connect" in database && "totalCount" in database;
}
+/**
+ * Serializes writers of one envelope's audit chain until the surrounding
+ * transaction ends, so two events cannot take the same place in the chain and
+ * a rollback cannot delete the head while another event is chaining to it.
+ * Call it inside a transaction.
+ */
+export async function lockEnvelopeAuditChain(
+ envelopeId: string,
+ database: DbExecutor,
+): Promise<void> {
+ await database.query(
+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 7))",
+ [envelopeId],
+ );
+}
+
async function insertChainedAuditEvent(
input: RecordAuditEventInput,
database: DbExecutor,
@@ -121,12 +137,7 @@ async function insertChainedAuditEvent(
const id = input.id ?? randomUUID();
const createdAt = input.createdAt ?? Math.floor(Date.now() / 1000);
- // Serializes writers for one envelope until the surrounding transaction
- // ends, so two events cannot take the same place in the chain.
- await database.query(
- "SELECT pg_advisory_xact_lock(hashtextextended($1, 7))",
- [input.envelopeId],
- );
+ await lockEnvelopeAuditChain(input.envelopeId, database);
const previous = await database.query<{
rowHash: string | null;