Bundles the prior session's 50-task fix sweep (Documenso v2 + EOI/signing-
progress redesign + env-to-admin migration + dev-mode banner) with the
2026-05-18 audit fix wave (3 CRITICAL, 14 HIGH, 28 MEDIUM, 6 LOW).
CRITICAL (3):
- C-01 interest-berths INNER JOIN -> LEFT JOIN so hard-deleted berths
no longer silently drop interest links
- C-02 /setup added to PUBLIC_PATHS; fresh-deploy bootstrap loop fixed
- C-03 generic PATCH /interests/[id] no longer accepts pipelineStage —
callers must go through /stage with the override-guard chain
HIGH (14/15):
- H-01 explicit ON DELETE on previously-implicit NO ACTION FKs across
interests/documents/reservations/reminders/invoices (migration 0070)
- H-02 login page reads ?redirect= param with same-origin guard
- H-03 CRM invite token moves to URL fragment so it never lands in
nginx access logs / Referer headers
- H-04 Retry-After header on sign-in-by-identifier 429 (RFC 6585 §4)
- H-05 toggleAccount writes an audit row
- H-06 upsertSetting masks any value whose key ends with _encrypted
- H-07 archiveClient cascade fires per-interest audit rows
- H-08 createSalesTransporter applies SMTP_TIMEOUTS
- H-09 AppShell stable children — viewport flip across breakpoint no
longer destroys in-progress form drafts
- H-10 portal documents page swaps Unicode glyph status icons for
Lucide CheckCircle2/XCircle/Circle + aria-labels
- H-12 list components swap alert(...) for toast.warning(...)
- H-13 5 icon-only buttons gain aria-label
- H-14 parseBody treats empty bodies as {}
- H-15 admin layout renders a 403 panel instead of silent bounce
- H-11 not applicable — mobile-search-overlay IS a mobile bottom-sheet
MEDIUM (28+):
- M-MT01-05 defense-in-depth port_id/parent-id filters on UPDATE/DELETE
WHEREs across custom-fields, notes (all 6 entity types x update +
delete), client-contacts, yacht ownerClient lookup, webhook reads
- M-D01 documents-hub realtime event-name typo (file:created -> uploaded)
- M-EM01 portal-auth emails thread through portId
- M-EM02 sendEmail accepts cc/bcc params
- M-EM04 notification_digest catalog key
- M-IN01 portal presigned download URLs use 4h TTL
- M-IN02 OpenAI client lazy-instantiated
- M-IN04 stale pdfme refs updated to pdf-lib AcroForm
- M-IN05 umami.testConnection returns tagged union
- M-L01 reservations tenure_type unified with berths
- M-L02 report-generators canonicalize stage values
- M-AU01 audit log placeholder copy fixed
- M-AU04 outcome_set / outcome_cleared distinct audit verbs
- M-NEW-2 activity feed entity name+type separator
- M-R01 portal allowlist narrowed + portal_session backstop in proxy
- M-SC02 companies archived partial index
- M-SC04 audit_logs.searchText documented as DB-managed
- M-S01 storage_s3_access_key_encrypted admin field
- M-U01 audit log empty state uses <EmptyState>
- M-U09 invoice delete dialog -> <AlertDialog>
- M-U10 toast.success on ClientForm + InterestForm create/edit
- M-U11 settings-form-card logo preview alt text
- M-U14 mobile topbar title on clients/yachts/interests/berths
- M-U15 Invoices in mobile More-sheet
LOW (6/8):
- L-AU01 severity defaults for security-relevant verbs
- L-AU02 +13 missing actions in admin audit filter
- L-AU03 +7 missing entity types in admin audit filter
- L-AU04 dead listAuditLogs stubbed
- L-D02 CLAUDE.md Owner-wins chain tightened
Bonus — Document detail polish (#67 partial, 3/6 deliverables):
- state-aware action button per signer
- watcher Add UI with display-name resolution
- cleanSignerName cleanup
Prior session work bundled in:
- Documenso v2 webhook + envelope-ID normalization + sequential signing
- SigningProgress UI redesign (avatars, per-signer state, timestamps)
- env->admin settings registry + RegistryDrivenForm + encrypted creds
- Embedded-signing card + Test connection + setup help
- Dev-mode EMAIL_REDIRECT_TO banner
- Pipeline rules admin page
- Sales email config card
- Audit log details Sheet
- EOI tab: Finalising badge, absolute timestamps, sequential indicator
- Notes pipeline_stage_at_creation (migration 0069)
- Documenso numeric ID dual-key webhook (migration 0068)
- Dimensions criterion copy (migration 0067)
Tests: 1374/1374 vitest pass. tsc clean. lint clean.
See docs/AUDIT-FIX-WAVE-2026-05-18.md for the full progress report and
the user-input items still pending.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
427 lines
13 KiB
TypeScript
427 lines
13 KiB
TypeScript
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
import { db } from '@/lib/db';
|
|
import { documents, documentTemplates } from '@/lib/db/schema/documents';
|
|
import { clientAddresses, clientContacts } from '@/lib/db/schema/clients';
|
|
import {
|
|
interests as interestsTable,
|
|
interestBerths as interestBerthsTable,
|
|
} from '@/lib/db/schema/interests';
|
|
import { systemSettings } from '@/lib/db/schema/system';
|
|
import { ValidationError } from '@/lib/errors';
|
|
|
|
import { makeBerth, makeClient, makePort, makeYacht } from '../helpers/factories';
|
|
|
|
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
|
|
vi.mock('@/lib/services/documenso-client', () => ({
|
|
createDocument: vi.fn(),
|
|
sendDocument: vi.fn(),
|
|
generateDocumentFromTemplate: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@/lib/minio', async () => {
|
|
const actual = await vi.importActual<typeof import('@/lib/minio')>('@/lib/minio');
|
|
return {
|
|
...actual,
|
|
minioClient: {
|
|
putObject: vi.fn().mockResolvedValue(undefined),
|
|
getObject: vi.fn().mockImplementation(async () => {
|
|
async function* gen() {
|
|
yield Buffer.from('fake-pdf');
|
|
}
|
|
return gen();
|
|
}),
|
|
},
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/socket/server', () => ({
|
|
emitToRoom: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@/lib/pdf/fill-eoi-form', () => ({
|
|
generateEoiPdfFromTemplate: vi
|
|
.fn()
|
|
.mockResolvedValue(new Uint8Array(Buffer.from('fake-eoi-pdf'))),
|
|
loadEoiTemplatePdf: vi.fn(),
|
|
fillEoiFormFields: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@/lib/audit', () => ({
|
|
createAuditLog: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
async function insertInApptemplate(portId: string) {
|
|
const [row] = await db
|
|
.insert(documentTemplates)
|
|
.values({
|
|
portId,
|
|
name: 'Test EOI',
|
|
templateType: 'eoi',
|
|
bodyHtml: '<p>Hello {{client.fullName}} for berth {{berth.mooringNumber}}</p>',
|
|
createdBy: 'test',
|
|
})
|
|
.returning();
|
|
return row!;
|
|
}
|
|
|
|
async function insertInterest(args: {
|
|
portId: string;
|
|
clientId: string;
|
|
yachtId: string;
|
|
berthId: string;
|
|
}) {
|
|
const [row] = await db
|
|
.insert(interestsTable)
|
|
.values({
|
|
portId: args.portId,
|
|
clientId: args.clientId,
|
|
yachtId: args.yachtId,
|
|
pipelineStage: 'open',
|
|
})
|
|
.returning();
|
|
// Plan §3.4: legacy interests.berth_id replaced by the junction.
|
|
await db.insert(interestBerthsTable).values({
|
|
interestId: row!.id,
|
|
berthId: args.berthId,
|
|
isPrimary: true,
|
|
isSpecificInterest: true,
|
|
});
|
|
return row!;
|
|
}
|
|
|
|
// ─── Setup ────────────────────────────────────────────────────────────────────
|
|
|
|
let setup: {
|
|
portId: string;
|
|
clientId: string;
|
|
yachtId: string;
|
|
berthId: string;
|
|
interestId: string;
|
|
inAppTemplateId: string;
|
|
};
|
|
|
|
let generateAndSign: typeof import('@/lib/services/document-templates').generateAndSign;
|
|
|
|
beforeAll(async () => {
|
|
({ generateAndSign } = await import('@/lib/services/document-templates'));
|
|
|
|
const port = await makePort();
|
|
const client = await makeClient({
|
|
portId: port.id,
|
|
overrides: { fullName: 'Dual Path Client', nationalityIso: 'US' },
|
|
});
|
|
await db.insert(clientContacts).values({
|
|
clientId: client.id,
|
|
channel: 'email',
|
|
value: 'client@example.com',
|
|
isPrimary: true,
|
|
});
|
|
await db.insert(clientAddresses).values({
|
|
clientId: client.id,
|
|
portId: port.id,
|
|
streetAddress: '1 Wharf Rd',
|
|
city: 'Harbor',
|
|
countryIso: 'US',
|
|
isPrimary: true,
|
|
});
|
|
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
name: 'Dual Path Yacht',
|
|
overrides: { lengthFt: '45', widthFt: '14', draftFt: '6' },
|
|
});
|
|
|
|
const berth = await makeBerth({
|
|
portId: port.id,
|
|
overrides: { mooringNumber: 'DP-1' },
|
|
});
|
|
|
|
const interest = await insertInterest({
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
berthId: berth.id,
|
|
});
|
|
|
|
const template = await insertInApptemplate(port.id);
|
|
|
|
// Seed the per-port `eoi_signers` legacy blob so getPortEoiSigners returns
|
|
// a developer/approver pair. The env-to-admin migration retired the
|
|
// hardcoded DEFAULT_DEVELOPER_NAME constants, so tests that exercise
|
|
// auto-derived signers must seed the settings themselves.
|
|
await db.insert(systemSettings).values({
|
|
portId: port.id,
|
|
key: 'eoi_signers',
|
|
value: {
|
|
developer: { name: 'David Mizrahi', email: 'dm@portnimara.com' },
|
|
approver: { name: 'Abbie May', email: 'sales@portnimara.com' },
|
|
},
|
|
});
|
|
|
|
setup = {
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
berthId: berth.id,
|
|
interestId: interest.id,
|
|
inAppTemplateId: template.id,
|
|
};
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
const meta = {
|
|
userId: 'test-user',
|
|
portId: '',
|
|
ipAddress: '127.0.0.1',
|
|
userAgent: 'vitest',
|
|
};
|
|
|
|
// ─── Pathway: inapp ───────────────────────────────────────────────────────────
|
|
|
|
describe('generateAndSign — inapp pathway', () => {
|
|
it('generates PDF via pdfme, uploads to MinIO, and sends to Documenso', async () => {
|
|
const client = await import('@/lib/services/documenso-client');
|
|
vi.mocked(client.createDocument).mockResolvedValue({
|
|
id: 'documenso-inapp-123',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
vi.mocked(client.sendDocument).mockResolvedValue({
|
|
id: 'documenso-inapp-123',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
|
|
const result = await generateAndSign(
|
|
setup.inAppTemplateId,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[{ name: 'Client', email: 'client@example.com', role: 'signer', signingOrder: 1 }],
|
|
'inapp',
|
|
{ ...meta, portId: setup.portId },
|
|
);
|
|
|
|
expect(result.file).not.toBeNull();
|
|
expect(client.createDocument).toHaveBeenCalledOnce();
|
|
expect(client.sendDocument).toHaveBeenCalledWith('documenso-inapp-123');
|
|
|
|
const [docRow] = await db
|
|
.select()
|
|
.from(documents)
|
|
.where(eq(documents.id, (result.document as { id: string }).id));
|
|
expect(docRow?.documensoId).toBe('documenso-inapp-123');
|
|
expect(docRow?.status).toBe('sent');
|
|
expect(docRow?.fileId).toBeTruthy();
|
|
});
|
|
|
|
it('auto-derives signers for EOI templates when none are provided', async () => {
|
|
const client = await import('@/lib/services/documenso-client');
|
|
vi.mocked(client.createDocument).mockResolvedValue({
|
|
id: 'doc-auto-signers',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
vi.mocked(client.sendDocument).mockResolvedValue({
|
|
id: 'doc-auto-signers',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
|
|
await generateAndSign(
|
|
setup.inAppTemplateId,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[],
|
|
'inapp',
|
|
{ ...meta, portId: setup.portId },
|
|
);
|
|
|
|
expect(client.createDocument).toHaveBeenCalledOnce();
|
|
const recipients = vi.mocked(client.createDocument).mock.calls[0]![2];
|
|
expect(recipients).toHaveLength(3);
|
|
expect(recipients[0]?.name).toBe('Dual Path Client');
|
|
expect(recipients[1]?.name).toBe('David Mizrahi');
|
|
expect(recipients[2]?.role).toBe('approver');
|
|
});
|
|
|
|
it('throws ValidationError when non-EOI template has no signers', async () => {
|
|
const [other] = await db
|
|
.insert(documentTemplates)
|
|
.values({
|
|
portId: setup.portId,
|
|
name: 'Plain Letter',
|
|
templateType: 'welcome_letter',
|
|
bodyHtml: '<p>x</p>',
|
|
createdBy: 'test',
|
|
})
|
|
.returning();
|
|
|
|
await expect(
|
|
generateAndSign(other!.id, setup.portId, { clientId: setup.clientId }, [], 'inapp', {
|
|
...meta,
|
|
portId: setup.portId,
|
|
}),
|
|
).rejects.toThrow(ValidationError);
|
|
});
|
|
|
|
it('throws ValidationError when templateId is null', async () => {
|
|
await expect(
|
|
generateAndSign(
|
|
null,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[{ name: 'X', email: 'x@x.com', role: 'signer', signingOrder: 1 }],
|
|
'inapp',
|
|
{ ...meta, portId: setup.portId },
|
|
),
|
|
).rejects.toThrow(ValidationError);
|
|
});
|
|
|
|
it('uses the EOI source-PDF path for templateType=eoi', async () => {
|
|
const fillModule = await import('@/lib/pdf/fill-eoi-form');
|
|
const client = await import('@/lib/services/documenso-client');
|
|
vi.mocked(client.createDocument).mockResolvedValue({
|
|
id: 'doc-eoi-pdf',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
vi.mocked(client.sendDocument).mockResolvedValue({
|
|
id: 'doc-eoi-pdf',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
|
|
await generateAndSign(
|
|
setup.inAppTemplateId,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[{ name: 'C', email: 'c@x.com', role: 'signer', signingOrder: 1 }],
|
|
'inapp',
|
|
{ ...meta, portId: setup.portId },
|
|
);
|
|
|
|
expect(fillModule.generateEoiPdfFromTemplate).toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects non-EOI template types on the inapp pathway', async () => {
|
|
// Non-EOI in-app PDF rendering was removed in the PDF stack overhaul
|
|
// (see docs/superpowers/specs/2026-05-12-pdf-stack-overhaul-design.md).
|
|
// The only surviving in-app pathway is EOI via pdf-lib AcroForm fill.
|
|
const [other] = await db
|
|
.insert(documentTemplates)
|
|
.values({
|
|
portId: setup.portId,
|
|
name: 'Welcome Letter',
|
|
templateType: 'welcome_letter',
|
|
bodyHtml: '<p>Welcome {{client.fullName}}</p>',
|
|
createdBy: 'test',
|
|
})
|
|
.returning();
|
|
|
|
await expect(
|
|
generateAndSign(
|
|
other!.id,
|
|
setup.portId,
|
|
{ clientId: setup.clientId },
|
|
[{ name: 'C', email: 'c@x.com', role: 'signer', signingOrder: 1 }],
|
|
'inapp',
|
|
{ ...meta, portId: setup.portId },
|
|
),
|
|
).rejects.toThrow(/welcome_letter/);
|
|
});
|
|
});
|
|
|
|
// ─── Pathway: documenso-template ──────────────────────────────────────────────
|
|
|
|
describe('generateAndSign — documenso-template pathway', () => {
|
|
it('calls Documenso template-generate endpoint and records a documents row', async () => {
|
|
const client = await import('@/lib/services/documenso-client');
|
|
vi.mocked(client.generateDocumentFromTemplate).mockResolvedValue({
|
|
id: 'documenso-template-456',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
|
|
const result = await generateAndSign(
|
|
null,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[],
|
|
'documenso-template',
|
|
{ ...meta, portId: setup.portId },
|
|
);
|
|
|
|
expect(result.file).toBeNull();
|
|
expect(client.generateDocumentFromTemplate).toHaveBeenCalledOnce();
|
|
const [templateArg, payloadArg] = vi.mocked(client.generateDocumentFromTemplate).mock.calls[0]!;
|
|
expect(typeof templateArg).toBe('number');
|
|
expect(payloadArg).toMatchObject({
|
|
externalId: `loi-${setup.interestId}`,
|
|
formValues: {
|
|
Name: 'Dual Path Client',
|
|
'Yacht Name': 'Dual Path Yacht',
|
|
'Berth Number': 'DP-1',
|
|
},
|
|
});
|
|
|
|
const [docRow] = await db
|
|
.select()
|
|
.from(documents)
|
|
.where(eq(documents.id, (result.document as { id: string }).id));
|
|
expect(docRow?.documensoId).toBe('documenso-template-456');
|
|
expect(docRow?.status).toBe('sent');
|
|
expect(docRow?.documentType).toBe('eoi');
|
|
expect(docRow?.interestId).toBe(setup.interestId);
|
|
expect(docRow?.fileId).toBeNull();
|
|
});
|
|
|
|
it('throws ValidationError when interestId is missing', async () => {
|
|
await expect(
|
|
generateAndSign(null, setup.portId, { clientId: setup.clientId }, [], 'documenso-template', {
|
|
...meta,
|
|
portId: setup.portId,
|
|
}),
|
|
).rejects.toThrow(ValidationError);
|
|
});
|
|
|
|
it('does NOT call createDocument / sendDocument / minio for this pathway', async () => {
|
|
const client = await import('@/lib/services/documenso-client');
|
|
vi.mocked(client.generateDocumentFromTemplate).mockResolvedValue({
|
|
id: 'documenso-template-789',
|
|
status: 'PENDING',
|
|
numericId: null,
|
|
recipients: [],
|
|
});
|
|
|
|
await generateAndSign(
|
|
null,
|
|
setup.portId,
|
|
{ clientId: setup.clientId, interestId: setup.interestId },
|
|
[],
|
|
'documenso-template',
|
|
{ ...meta, portId: setup.portId },
|
|
);
|
|
|
|
expect(client.createDocument).not.toHaveBeenCalled();
|
|
expect(client.sendDocument).not.toHaveBeenCalled();
|
|
});
|
|
});
|