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>
This commit is contained in:
Matt Ciaccio
2026-05-02 03:11:14 +02:00
parent 76a7387dcc
commit d197f8b321
10 changed files with 316 additions and 114 deletions

View File

@@ -91,8 +91,9 @@ describe('buildDocumensoPayload', () => {
});
it('defaults missing yacht dimensions to empty strings', () => {
const baseYacht = makeContext().yacht!;
const ctx = makeContext({
yacht: { ...makeContext().yacht, lengthFt: null, widthFt: null, draftFt: null },
yacht: { ...baseYacht, lengthFt: null, widthFt: null, draftFt: null },
});
const payload = buildDocumensoPayload(ctx, OPTIONS);
expect(payload.formValues.Length).toBe('');
@@ -100,6 +101,16 @@ describe('buildDocumensoPayload', () => {
expect(payload.formValues.Draft).toBe('');
});
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);

View File

@@ -28,6 +28,33 @@ async function insertInterest(args: {
return row!;
}
/**
* Adds the EOI-required client details (primary email + primary address) so
* `buildEoiContext` clears its hard gate. Tests that exercise non-EOI-gating
* behavior should call this once per client they create.
*/
async function seedClientEoiPrereqs(args: {
clientId: string;
portId: string;
email?: string;
street?: string;
}) {
await db.insert(clientContacts).values({
clientId: args.clientId,
channel: 'email',
value: args.email ?? `client-${args.clientId.slice(0, 8)}@example.com`,
isPrimary: true,
});
await db.insert(clientAddresses).values({
clientId: args.clientId,
portId: args.portId,
streetAddress: args.street ?? '1 Harbour Way',
city: 'Anguilla',
countryIso: 'AI',
isPrimary: true,
});
}
// ─── Tests ────────────────────────────────────────────────────────────────────
describe('buildEoiContext', () => {
@@ -107,13 +134,13 @@ describe('buildEoiContext', () => {
});
// Yacht assertions.
expect(ctx.yacht.name).toBe('Sea Breeze');
expect(ctx.yacht.hullNumber).toBe('HN-1');
expect(ctx.yacht.yearBuilt).toBe(2020);
expect(ctx.yacht?.name).toBe('Sea Breeze');
expect(ctx.yacht?.hullNumber).toBe('HN-1');
expect(ctx.yacht?.yearBuilt).toBe(2020);
// Berth assertions.
expect(ctx.berth.mooringNumber).toBe('M-42');
expect(ctx.berth.area).toBe('North');
expect(ctx.berth?.mooringNumber).toBe('M-42');
expect(ctx.berth?.area).toBe('North');
// Interest assertions.
expect(ctx.interest.stage).toBe('in_communication');
@@ -144,6 +171,7 @@ describe('buildEoiContext', () => {
portId: port.id,
overrides: { fullName: 'Bob Contact' },
});
await seedClientEoiPrereqs({ clientId: client.id, portId: port.id });
const yacht = await makeYacht({
portId: port.id,
@@ -187,6 +215,7 @@ describe('buildEoiContext', () => {
});
const client = await makeClient({ portId: port.id });
await seedClientEoiPrereqs({ clientId: client.id, portId: port.id });
const yacht = await makeYacht({
portId: port.id,
ownerType: 'company',
@@ -211,9 +240,10 @@ describe('buildEoiContext', () => {
expect(ctx.company!.billingAddress).toContain('Anguilla');
});
it('throws ValidationError when interest has no yacht', async () => {
it('builds a valid context when yacht is missing (Section 3 left blank)', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await seedClientEoiPrereqs({ clientId: client.id, portId: port.id });
const berth = await makeBerth({ portId: port.id });
const interest = await insertInterest({
@@ -223,13 +253,18 @@ describe('buildEoiContext', () => {
berthId: berth.id,
});
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(ValidationError);
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(/interest has no yacht/i);
const ctx = await buildEoiContext(interest.id, port.id);
expect(ctx.yacht).toBeNull();
expect(ctx.berth?.mooringNumber).toBe(berth.mooringNumber);
// Owner falls back to the interest's client when no yacht is linked.
expect(ctx.owner.type).toBe('client');
expect(ctx.owner.name).toBe(client.fullName);
});
it('throws ValidationError when interest has no berth', async () => {
it('builds a valid context when berth is missing (Section 3 left blank)', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await seedClientEoiPrereqs({ clientId: client.id, portId: port.id });
const yacht = await makeYacht({
portId: port.id,
ownerType: 'client',
@@ -243,8 +278,45 @@ describe('buildEoiContext', () => {
berthId: null,
});
const ctx = await buildEoiContext(interest.id, port.id);
expect(ctx.berth).toBeNull();
expect(ctx.yacht?.name).toBe(yacht.name);
});
it('throws ValidationError when client has no email', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
// Address only, no email — gate should fail.
await db.insert(clientAddresses).values({
clientId: client.id,
portId: port.id,
streetAddress: '1 Harbour Way',
city: 'Anguilla',
countryIso: 'AI',
isPrimary: true,
});
const interest = await insertInterest({ portId: port.id, clientId: client.id });
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(ValidationError);
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(/interest has no berth/i);
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(/client email/i);
});
it('throws ValidationError when client has no primary address', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
// Email only, no address — gate should fail.
await db.insert(clientContacts).values({
clientId: client.id,
channel: 'email',
value: 'test@example.com',
isPrimary: true,
});
const interest = await insertInterest({ portId: port.id, clientId: client.id });
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(ValidationError);
await expect(buildEoiContext(interest.id, port.id)).rejects.toThrow(/client address/i);
});
it('throws NotFoundError for non-existent interest', async () => {