Files
pn-new-crm/tests/unit/services/documenso-payload.test.ts

222 lines
7.2 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from 'vitest';
import { buildDocumensoPayload } from '@/lib/services/documenso-payload';
import type { EoiContext } from '@/lib/services/eoi-context';
function makeContext(overrides?: Partial<EoiContext>): EoiContext {
return {
client: {
fullName: 'Alice Smith',
nationality: 'US',
primaryEmail: 'alice@example.com',
primaryPhone: '+1-555-0100',
address: { street: '123 Main St', city: 'Austin', country: 'USA' },
},
yacht: {
name: 'Sea Breeze',
lengthFt: '45',
widthFt: '14',
draftFt: '6',
lengthM: null,
widthM: null,
draftM: null,
hullNumber: 'ABC-123',
flag: 'US',
yearBuilt: 2020,
},
company: null,
owner: { type: 'client', name: 'Alice Smith' },
berth: {
mooringNumber: 'A-12',
area: 'North Dock',
lengthFt: '50',
price: '1200',
priceCurrency: 'USD',
tenureType: 'permanent',
},
interest: {
stage: 'open',
leadCategory: null,
dateFirstContact: null,
notes: null,
},
port: {
name: 'Port Nimara',
defaultCurrency: 'USD',
},
date: { today: '2026-04-23', year: '2026' },
...overrides,
};
}
const OPTIONS = {
interestId: 'int-123',
clientRecipientId: 192,
developerRecipientId: 193,
approvalRecipientId: 194,
};
describe('buildDocumensoPayload', () => {
it('builds title as "{fullName}-EOI-NDA"', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.title).toBe('Alice Smith-EOI-NDA');
});
it('builds externalId as "loi-{interestId}"', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.externalId).toBe('loi-int-123');
});
it('formats formValues with all EoiContext fields', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.formValues).toEqual({
Name: 'Alice Smith',
Email: 'alice@example.com',
Address: '123 Main St, Austin, USA',
'Yacht Name': 'Sea Breeze',
Length: '45',
Width: '14',
Draft: '6',
'Berth Number': 'A-12',
Lease_10: false,
Purchase: true,
});
});
it('defaults missing primaryEmail to empty string', () => {
const ctx = makeContext({ client: { ...makeContext().client, primaryEmail: null } });
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues.Email).toBe('');
expect(payload.recipients[0]!.email).toBe('');
});
it('defaults missing yacht dimensions to empty strings', () => {
feat(eoi): align prerequisites with EOI document structure Match the gate to the actual EOI's structure (Section 2 vs Section 3) so the rep can generate the document the moment they have what they need — and not before. Required (Section 2 — top paragraph): - Client name - Client primary email - Client primary address Optional (Section 3 — left blank when absent): - Linked yacht (name, dimensions) - Linked berth (mooring number) Previously the dialog blocked generation unless yacht AND berth were both linked, which was overzealous — early-stage EOIs are routinely sent before a specific berth is pinned down. - eoi-context.ts: yacht and berth are now nullable in the returned context. The hard ValidationError is now driven by the EOI's Section 2 fields (name/email/address) rather than yacht/berth presence. The owner block falls back to the interest's client when no yacht is linked, so signing parties remain resolvable. - documenso-payload.ts + fill-eoi-form.ts: Section 3 form values render as empty strings when yacht or berth are absent, so the rendered PDF leaves those template inputs blank. - document-templates.ts: yacht.* and berth.* tokens fall back to empty strings; the legacy-fallback catch handler also recognises the new "missing required client details" error. - interests.service.ts: getInterestById now also returns `clientPrimaryEmail` and `clientHasAddress` so the Documents tab can compute the EOI prerequisites checklist client-side without an extra fetch. - eoi-generate-dialog.tsx: prereqs split into two groups visually — Required (with red ✗ when missing) and Optional (with grey – when absent). The Generate button only requires the Required block to pass. A small amber banner surfaces when Required is incomplete so the rep knows where to add the missing data. Tests: 835/835 pass. Replaces the obsolete "throws on missing yacht/ berth" tests with parity coverage for the new behaviour ("builds a valid context when yacht/berth missing", "throws when client email/ address missing"). Adds a payload test for the empty-Section-3 case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 03:11:14 +02:00
const baseYacht = makeContext().yacht!;
const ctx = makeContext({
feat(eoi): align prerequisites with EOI document structure Match the gate to the actual EOI's structure (Section 2 vs Section 3) so the rep can generate the document the moment they have what they need — and not before. Required (Section 2 — top paragraph): - Client name - Client primary email - Client primary address Optional (Section 3 — left blank when absent): - Linked yacht (name, dimensions) - Linked berth (mooring number) Previously the dialog blocked generation unless yacht AND berth were both linked, which was overzealous — early-stage EOIs are routinely sent before a specific berth is pinned down. - eoi-context.ts: yacht and berth are now nullable in the returned context. The hard ValidationError is now driven by the EOI's Section 2 fields (name/email/address) rather than yacht/berth presence. The owner block falls back to the interest's client when no yacht is linked, so signing parties remain resolvable. - documenso-payload.ts + fill-eoi-form.ts: Section 3 form values render as empty strings when yacht or berth are absent, so the rendered PDF leaves those template inputs blank. - document-templates.ts: yacht.* and berth.* tokens fall back to empty strings; the legacy-fallback catch handler also recognises the new "missing required client details" error. - interests.service.ts: getInterestById now also returns `clientPrimaryEmail` and `clientHasAddress` so the Documents tab can compute the EOI prerequisites checklist client-side without an extra fetch. - eoi-generate-dialog.tsx: prereqs split into two groups visually — Required (with red ✗ when missing) and Optional (with grey – when absent). The Generate button only requires the Required block to pass. A small amber banner surfaces when Required is incomplete so the rep knows where to add the missing data. Tests: 835/835 pass. Replaces the obsolete "throws on missing yacht/ berth" tests with parity coverage for the new behaviour ("builds a valid context when yacht/berth missing", "throws when client email/ address missing"). Adds a payload test for the empty-Section-3 case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 03:11:14 +02:00
yacht: { ...baseYacht, lengthFt: null, widthFt: null, draftFt: null },
});
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues.Length).toBe('');
expect(payload.formValues.Width).toBe('');
expect(payload.formValues.Draft).toBe('');
});
feat(eoi): align prerequisites with EOI document structure Match the gate to the actual EOI's structure (Section 2 vs Section 3) so the rep can generate the document the moment they have what they need — and not before. Required (Section 2 — top paragraph): - Client name - Client primary email - Client primary address Optional (Section 3 — left blank when absent): - Linked yacht (name, dimensions) - Linked berth (mooring number) Previously the dialog blocked generation unless yacht AND berth were both linked, which was overzealous — early-stage EOIs are routinely sent before a specific berth is pinned down. - eoi-context.ts: yacht and berth are now nullable in the returned context. The hard ValidationError is now driven by the EOI's Section 2 fields (name/email/address) rather than yacht/berth presence. The owner block falls back to the interest's client when no yacht is linked, so signing parties remain resolvable. - documenso-payload.ts + fill-eoi-form.ts: Section 3 form values render as empty strings when yacht or berth are absent, so the rendered PDF leaves those template inputs blank. - document-templates.ts: yacht.* and berth.* tokens fall back to empty strings; the legacy-fallback catch handler also recognises the new "missing required client details" error. - interests.service.ts: getInterestById now also returns `clientPrimaryEmail` and `clientHasAddress` so the Documents tab can compute the EOI prerequisites checklist client-side without an extra fetch. - eoi-generate-dialog.tsx: prereqs split into two groups visually — Required (with red ✗ when missing) and Optional (with grey – when absent). The Generate button only requires the Required block to pass. A small amber banner surfaces when Required is incomplete so the rep knows where to add the missing data. Tests: 835/835 pass. Replaces the obsolete "throws on missing yacht/ berth" tests with parity coverage for the new behaviour ("builds a valid context when yacht/berth missing", "throws when client email/ address missing"). Adds a payload test for the empty-Section-3 case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 03:11:14 +02:00
it('renders empty Section 3 when yacht and berth are not linked', () => {
const ctx = makeContext({ yacht: null, berth: null });
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues['Yacht Name']).toBe('');
expect(payload.formValues.Length).toBe('');
expect(payload.formValues.Width).toBe('');
expect(payload.formValues.Draft).toBe('');
expect(payload.formValues['Berth Number']).toBe('');
});
it('formats empty address when client has no address', () => {
const ctx = makeContext({ client: { ...makeContext().client, address: null } });
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues.Address).toBe('');
});
it('skips null parts in address', () => {
const ctx = makeContext({
client: {
...makeContext().client,
address: { street: '', city: 'Austin', country: 'USA' },
},
});
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues.Address).toBe('Austin, USA');
});
it('sets Lease_10=false and Purchase=true (hardcoded)', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.formValues.Lease_10).toBe(false);
expect(payload.formValues.Purchase).toBe(true);
});
it('includes client, developer, and approver recipients in signing order', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.recipients).toHaveLength(3);
expect(payload.recipients[0]).toEqual({
id: 192,
name: 'Alice Smith',
email: 'alice@example.com',
role: 'SIGNER',
signingOrder: 1,
});
expect(payload.recipients[1]).toEqual({
id: 193,
name: 'David Mizrahi',
email: 'dm@portnimara.com',
role: 'SIGNER',
signingOrder: 2,
});
expect(payload.recipients[2]).toEqual({
id: 194,
name: 'Abbie May',
email: 'sales@portnimara.com',
role: 'APPROVER',
signingOrder: 3,
});
});
it('allows overriding developer/approver recipient names', () => {
const payload = buildDocumensoPayload(makeContext(), {
...OPTIONS,
developerName: 'Custom Dev',
developerEmail: 'dev@custom.com',
approverName: 'Custom Approver',
approverEmail: 'approve@custom.com',
});
expect(payload.recipients[1]!.name).toBe('Custom Dev');
expect(payload.recipients[1]!.email).toBe('dev@custom.com');
expect(payload.recipients[2]!.name).toBe('Custom Approver');
expect(payload.recipients[2]!.email).toBe('approve@custom.com');
});
it('builds message with port name and greeting', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.meta.message).toContain('Dear Alice Smith');
expect(payload.meta.message).toContain('Port Nimara');
expect(payload.meta.message).toContain('Best Regards');
// No company on-behalf block for client-owned yachts
expect(payload.meta.message).not.toContain('On behalf of');
});
it('adds company on-behalf block for company-owned yachts', () => {
const ctx = makeContext({
company: {
name: 'Aegean Holdings',
legalName: 'Aegean Holdings SA',
taxId: null,
billingAddress: null,
},
owner: { type: 'company', name: 'Aegean Holdings', legalName: 'Aegean Holdings SA' },
});
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.meta.message).toContain('On behalf of Aegean Holdings SA');
});
it('uses company name when legalName is missing in on-behalf block', () => {
const ctx = makeContext({
company: { name: 'Blue Seas', legalName: null, taxId: null, billingAddress: null },
owner: { type: 'company', name: 'Blue Seas' },
});
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.meta.message).toContain('On behalf of Blue Seas');
});
it('uses default redirect URL when not provided', () => {
const payload = buildDocumensoPayload(makeContext(), OPTIONS);
expect(payload.meta.redirectUrl).toBe('https://portnimara.com');
});
it('uses custom redirect URL when provided', () => {
const payload = buildDocumensoPayload(makeContext(), {
...OPTIONS,
redirectUrl: 'https://custom.example.com',
});
expect(payload.meta.redirectUrl).toBe('https://custom.example.com');
});
});