Files
pn-new-crm/tests/integration/documents-expired-webhook.test.ts
Matt 221ae5784e chore(autonomous-session): consolidate uncommitted work from prior session
Bundles the prior autonomous-session output that was sitting unstaged:

- Em-dash sweep across src/ + tests/ (en-dash/em-dash to hyphen, ~2280 instances)
- country-flag-icons rollout (CountryFlag component, replaces emoji glyphs that
  never rendered on Windows; lazy-loads the 3x2 SVG index as a single chunk
  after the per-subpath dynamic-import approach silently failed in webpack)
- Admin IA Phase 1+2: 7-domain regroup, 41 to 38 pages, /admin/berths index,
  redirects (ocr to ai, reports to dashboard, invitations to users),
  docs/admin-ia-proposal.md
- Per-template email tester (registry + endpoint + UI on Email admin page)
- Cancel-document mode picker (delete-from-Documenso vs keep-for-audit)
- Dashboard PDF report: 25 widgets, SVG charts, date-range picker, 11 resolvers
- Customize-widgets per-region sortables at xl+ (charts/rails/feed); single
  flat sortable below xl when the layout stacks; per-viewport saved orders
- Audit doc updates capturing each shipped item
- Lint fixes: react-compiler immutability in DonutChart (reduce instead of
  let-reassign), set-state-in-effect disables in CountryFlag and
  UploadForSigning preview-bytes effect, unused 'confirm' destructures in
  interest contract + reservation tabs, unescaped apostrophe in test-template
  card copy
2026-05-23 00:52:59 +02:00

151 lines
5.2 KiB
TypeScript

/**
* DOCUMENT_EXPIRED webhook handling - locks in fix(documenso). The handler
* was previously defined but never wired to the route's event switch, so
* expired EOIs stayed in `sent` / `partially_signed` forever.
*/
import { describe, expect, it } from 'vitest';
import { eq } from 'drizzle-orm';
import { db } from '@/lib/db';
import { documents, documentEvents } from '@/lib/db/schema/documents';
import { interests, interestBerths } from '@/lib/db/schema/interests';
import { handleDocumentExpired } from '@/lib/services/documents.service';
import { makeBerth, makeClient, makePort } from '../helpers/factories';
describe('handleDocumentExpired', () => {
it('flips a sent EOI to expired and writes a documentEvents row', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
const documensoId = `documenso-test-${Date.now()}`;
const [doc] = await db
.insert(documents)
.values({
portId: port.id,
clientId: client.id,
documentType: 'eoi',
title: 'Expiring EOI',
status: 'sent',
documensoId,
createdBy: 'seed',
})
.returning();
await handleDocumentExpired({ documentId: documensoId });
const after = await db.query.documents.findFirst({
where: eq(documents.id, doc!.id),
});
expect(after?.status).toBe('expired');
const events = await db
.select()
.from(documentEvents)
.where(eq(documentEvents.documentId, doc!.id));
expect(events.map((e) => e.eventType)).toContain('expired');
});
it('also flips the linked interest eoiStatus to expired', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
const berth = await makeBerth({ portId: port.id });
const [interest] = await db
.insert(interests)
.values({
portId: port.id,
clientId: client.id,
pipelineStage: 'eoi_sent',
leadCategory: 'hot_lead',
eoiStatus: 'sent',
})
.returning();
await db.insert(interestBerths).values({
interestId: interest!.id,
berthId: berth.id,
isPrimary: true,
isSpecificInterest: true,
});
const documensoId = `documenso-test-${Date.now()}-i`;
await db.insert(documents).values({
portId: port.id,
clientId: client.id,
interestId: interest!.id,
documentType: 'eoi',
title: 'Expiring EOI for interest',
status: 'sent',
documensoId,
createdBy: 'seed',
});
await handleDocumentExpired({ documentId: documensoId });
const updatedInterest = await db.query.interests.findFirst({
where: eq(interests.id, interest!.id),
});
expect(updatedInterest?.eoiStatus).toBe('expired');
});
it('is a no-op when the documensoId does not match any document', async () => {
// Should NOT throw - the handler logs a warning and returns. Verify no
// exception propagates up to the webhook route.
await expect(
handleDocumentExpired({ documentId: 'definitely-not-a-real-doc' }),
).resolves.toBeUndefined();
});
it('does not flip a document in port B when port A receives the expired event', async () => {
// Two ports holding the same documenso_id (legacy data, or a future
// Documenso-instance migration that reuses ids). The handler currently
// mutates whichever document `findFirst` returns - locking in the
// intended behaviour now means a future port_id-aware handler can
// be added without regressing this guard.
//
// Per the audit, this assertion is expected to FAIL until the
// deferred port_id-on-handler fix lands. Once it lands, the test
// protects against re-introducing the cross-tenant flip.
const portA = await makePort();
const portB = await makePort();
const clientA = await makeClient({ portId: portA.id });
const clientB = await makeClient({ portId: portB.id });
const sharedDocumensoId = `documenso-shared-${Date.now()}`;
const [docA] = await db
.insert(documents)
.values({
portId: portA.id,
clientId: clientA.id,
documentType: 'eoi',
title: 'Port A EOI',
status: 'sent',
documensoId: sharedDocumensoId,
createdBy: 'seed',
})
.returning();
const [docB] = await db
.insert(documents)
.values({
portId: portB.id,
clientId: clientB.id,
documentType: 'eoi',
title: 'Port B EOI',
status: 'sent',
documensoId: sharedDocumensoId,
createdBy: 'seed',
})
.returning();
// Simulate port A receiving the expired event (the route caller would
// resolve this from the secret → port mapping in the deferred fix).
await handleDocumentExpired({ documentId: sharedDocumensoId, portId: portA.id });
// Port-A doc flipped, port-B unchanged. Pre-fix, both flip - this
// assertion locks the boundary in once the handler scope lands.
const afterA = await db.query.documents.findFirst({ where: eq(documents.id, docA!.id) });
const afterB = await db.query.documents.findFirst({ where: eq(documents.id, docB!.id) });
expect(afterA?.status).toBe('expired');
expect(afterB?.status).toBe('sent');
});
});