Phase 2b of the berth-recommender refactor (plan §3.4). Every caller of the legacy `interests.berth_id` column now reads / writes through the `interest_berths` junction via the helper service introduced in Phase 2a; the column itself is dropped in a final migration. Service-layer changes - interests.service: filter `?berthId=X` becomes EXISTS-against-junction; list enrichment uses `getPrimaryBerthsForInterests`; create/update/ linkBerth/unlinkBerth all dispatch through the junction helpers, with createInterest's row insert + junction write sharing a single transaction. - clients / dashboard / report-generators / search: leftJoin chains pivot through `interest_berths` filtered by `is_primary=true`. - eoi-context / document-templates / berth-rules-engine / portal / record-export / queue worker: read primary via `getPrimaryBerth(...)`. - interest-scoring: berthLinked is now derived from any junction row count. - dedup/migration-apply + public interest route: write a primary junction row alongside the interest insert when a berth is provided. API contract preserved: list/detail responses still emit `berthId` and `berthMooringNumber`, derived from the primary junction row, so frontend consumers (interest-form, interest-detail-header) need no changes. Schema + migration - Drop `interestsRelations.berth` and `idx_interests_berth`. - Replace `berthsRelations.interests` with `interestBerths`. - Migration 0029_puzzling_romulus drops `interests.berth_id` + the index. - Tests that previously inserted `interests.berthId` now seed a primary junction row alongside the interest. Verified: vitest 995 passing (1 unrelated pre-existing flake in maintenance-cleanup.test.ts), tsc clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
98 lines
3.1 KiB
TypeScript
98 lines
3.1 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();
|
|
});
|
|
});
|