feat(eoi): in-app pathway fills the same source PDF as Documenso
When the in-app pathway is used for EOI templates, we now load the same source PDF that the Documenso template uploads and fill its AcroForm fields with values from EoiContext via pdf-lib. Field names mirror the Documenso template's formValues keys exactly (Name, Email, Address, Yacht Name, Length, Width, Draft, Berth Number + Lease_10 / Purchase checkboxes), so both pathways produce equivalent legal documents — only the renderer differs. The form is left interactive (not flattened) so a recipient can still adjust values before signing. Non-EOI templates (welcome letters, acknowledgments, etc.) keep using the existing HTML→pdfme path. Adds: - pdf-lib direct dep - src/lib/pdf/fill-eoi-form.ts — load + fill helpers, EOI_TEMPLATE_PDF_PATH env override - assets/ + README documenting the expected source PDF - next.config outputFileTracingIncludes so the asset is bundled in the standalone build Tests: 8 new (4 fill-form unit + 2 source-PDF route + 2 fallback); 645/645 green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,14 @@ vi.mock('@/lib/pdf/generate', () => ({
|
||||
generatePdf: vi.fn().mockResolvedValue(new Uint8Array(Buffer.from('fake-pdf'))),
|
||||
}));
|
||||
|
||||
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),
|
||||
}));
|
||||
@@ -223,6 +231,74 @@ describe('generateAndSign — inapp pathway', () => {
|
||||
),
|
||||
).rejects.toThrow(ValidationError);
|
||||
});
|
||||
|
||||
it('uses the EOI source-PDF path (not pdfme HTML) for templateType=eoi', async () => {
|
||||
const fillModule = await import('@/lib/pdf/fill-eoi-form');
|
||||
const pdfModule = await import('@/lib/pdf/generate');
|
||||
const client = await import('@/lib/services/documenso-client');
|
||||
vi.mocked(client.createDocument).mockResolvedValue({
|
||||
id: 'doc-eoi-pdf',
|
||||
status: 'PENDING',
|
||||
recipients: [],
|
||||
});
|
||||
vi.mocked(client.sendDocument).mockResolvedValue({
|
||||
id: 'doc-eoi-pdf',
|
||||
status: 'PENDING',
|
||||
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();
|
||||
expect(pdfModule.generatePdf).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('falls back to HTML→pdfme for non-EOI template types', async () => {
|
||||
// Create a non-EOI template inline.
|
||||
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();
|
||||
|
||||
const fillModule = await import('@/lib/pdf/fill-eoi-form');
|
||||
const pdfModule = await import('@/lib/pdf/generate');
|
||||
const client = await import('@/lib/services/documenso-client');
|
||||
vi.mocked(client.createDocument).mockResolvedValue({
|
||||
id: 'doc-welcome',
|
||||
status: 'PENDING',
|
||||
recipients: [],
|
||||
});
|
||||
vi.mocked(client.sendDocument).mockResolvedValue({
|
||||
id: 'doc-welcome',
|
||||
status: 'PENDING',
|
||||
recipients: [],
|
||||
});
|
||||
|
||||
await generateAndSign(
|
||||
other!.id,
|
||||
setup.portId,
|
||||
{ clientId: setup.clientId },
|
||||
[{ name: 'C', email: 'c@x.com', role: 'signer', signingOrder: 1 }],
|
||||
'inapp',
|
||||
{ ...meta, portId: setup.portId },
|
||||
);
|
||||
|
||||
expect(pdfModule.generatePdf).toHaveBeenCalled();
|
||||
expect(fillModule.generateEoiPdfFromTemplate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Pathway: documenso-template ──────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user