Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Print certificate audit rows in recorded order (#97)
Claude4ccca267a065
diff --git a/src/lib/services/pdf-finalize.test.ts b/src/lib/services/pdf-finalize.test.tsindex cf3adf2..46e946e 100644--- a/src/lib/services/pdf-finalize.test.ts+++ b/src/lib/services/pdf-finalize.test.ts@@ -673,3 +673,140 @@ test("describeAuditActor keeps the signer's name on the signer's own rows", () ="system",);});++// Audit rows print "time | event | actor". Returns the events in the order+// the certificate lists them.+function certificateEventOrder(text: string): string[] {+ return [...text.matchAll(/UTC \| ([a-z_]+) \|/g)].map((match) => match[1]);+}++async function renderCertificateText(+ envelope: EnvelopeRecord,+ signers: SignerRecord[],+ auditEvents: AuditEventRecord[],+): Promise<string> {+ return (+ await extractText(+ await renderEnvelopeCertificatePdf(+ { auditEvents, envelope, signers },+ { seal: saveUnsealed },+ ),+ )+ ).replace(/\s+/g, " ");+}++test("the last signature is listed before completed in the same second (issue #97)", async () => {+ const fixture = await makeFixture();+ const now = fixture.envelope.completedAt ?? 0;+ // The ids sort after any generated id, so a sort by time then id would+ // put the completed row first.+ const auditEvents: AuditEventRecord[] = [+ {+ actorEmail: "[email protected]",+ actorType: "signer",+ createdAt: now,+ envelopeId: fixture.envelope.id,+ event: "signed",+ id: "zz-event-signed",+ metadata: { signerId: "signer-2", signerName: SECOND_SIGNER_NAME },+ },+ ];+ const text = await renderCertificateText(+ fixture.envelope,+ fixture.signers,+ auditEvents,+ );++ assert.deepEqual(certificateEventOrder(text), ["signed", "completed"], text);+});++test("a recorded completed row keeps its place in the recorded order (issue #97)", async () => {+ const fixture = await makeFixture();+ const now = fixture.envelope.completedAt ?? 0;+ const auditEvents: AuditEventRecord[] = [+ {+ actorEmail: "[email protected]",+ actorType: "signer",+ createdAt: now,+ envelopeId: fixture.envelope.id,+ event: "signed",+ id: "zz-event-signed",+ metadata: { signerId: "signer-2", signerName: SECOND_SIGNER_NAME },+ },+ {+ actorType: "system",+ createdAt: now,+ envelopeId: fixture.envelope.id,+ event: "completed",+ id: "aa-event-completed",+ metadata: null,+ },+ {+ actorType: "system",+ createdAt: now,+ envelopeId: fixture.envelope.id,+ event: "email_sent",+ id: "00-event-email",+ metadata: null,+ },+ ];+ const text = await renderCertificateText(+ fixture.envelope,+ fixture.signers,+ auditEvents,+ );++ assert.deepEqual(+ certificateEventOrder(text),+ ["signed", "completed", "email_sent"],+ text,+ );+});++test("declined and voided certificates keep same-second events in recorded order (issue #97)", async () => {+ const fixture = await makeFixture();+ const at = 1_799_999_000;+ const sameSecond = (+ event: AuditEventRecord["event"],+ id: string,+ ): AuditEventRecord => ({+ actorType: "system",+ createdAt: at,+ envelopeId: fixture.envelope.id,+ event,+ id,+ metadata: null,+ });++ for (const [status, terminalEvent] of [+ ["declined", "declined"],+ ["voided", "voided"],+ ] as const) {+ const envelope: EnvelopeRecord = {+ ...fixture.envelope,+ completedAt: null,+ declinedAt: status === "declined" ? at : null,+ status,+ voidedAt: status === "voided" ? at : null,+ };+ // Recorded order: sent, signed, the next invitation, then the end. The+ // ids are in reverse, so a sort by id would turn them around.+ const auditEvents = [+ sameSecond("sent", "d-sent"),+ sameSecond("signed", "c-signed"),+ sameSecond("email_sent", "b-email"),+ sameSecond(terminalEvent, "a-end"),+ ];+ const text = await renderCertificateText(+ envelope,+ fixture.signers,+ auditEvents,+ );++ assert.deepEqual(+ certificateEventOrder(text),+ ["sent", "signed", "email_sent", terminalEvent],+ text,+ );+ }+});diff --git a/src/lib/services/pdf-finalize.ts b/src/lib/services/pdf-finalize.tsindex e199ceb..bee35c5 100644--- a/src/lib/services/pdf-finalize.ts+++ b/src/lib/services/pdf-finalize.ts@@ -405,19 +405,32 @@ function buildCertificateAuditEvents(input: {envelope: EnvelopeRecord;terminalTimestamp: number;}): AuditEventRecord[] {- const baseAuditEvents =- input.envelope.status === "completed"- ? stripCompletedAuditEvents(input.auditEvents)- : input.auditEvents;- const terminalAuditEvents =- input.envelope.status === "completed"- ? [buildCompletedAuditEvent(input.envelope.id, input.terminalTimestamp)]- : [];-- return [...baseAuditEvents, ...terminalAuditEvents].sort(- (left, right) =>- left.createdAt - right.createdAt || left.id.localeCompare(right.id),+ // The rows come in the order they were recorded: the audit query reads+ // them by the chain's sequence, and a caller adds the event it is about to+ // record at the end. They are printed in that order and never re-sorted.+ // created_at is in whole seconds and ids are random, so sorting by time and+ // then id would shuffle events from the same second (issue #97).+ if (input.envelope.status !== "completed") {+ return [...input.auditEvents];+ }++ // A completed envelope shows one completed row. A recorded one keeps its+ // place, so events recorded after it stay after it. Before it is recorded,+ // as when the last signature seals the envelope, it goes last.+ const recordedCompletion = input.auditEvents.find(+ (event) => event.event === "completed",);++ if (recordedCompletion) {+ return input.auditEvents.filter(+ (event) => event.event !== "completed" || event === recordedCompletion,+ );+ }++ return [+ ...input.auditEvents,+ buildCompletedAuditEvent(input.envelope.id, input.terminalTimestamp),+ ];}export async function stampFieldValue(