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

@@ -237,18 +237,20 @@ export async function resolveTemplate(
tokenMap['{{client.phone}}'] = eoi.client.primaryPhone ?? '';
tokenMap['{{client.nationality}}'] = eoi.client.nationality ?? '';
// Yacht tokens
tokenMap['{{yacht.name}}'] = eoi.yacht.name;
tokenMap['{{yacht.hullNumber}}'] = eoi.yacht.hullNumber ?? '';
tokenMap['{{yacht.flag}}'] = eoi.yacht.flag ?? '';
// Yacht tokens — `eoi.yacht` is null when no yacht is linked
// (Section 3 of the EOI is optional). Tokens render as empty strings
// in that case so the template still produces output.
tokenMap['{{yacht.name}}'] = eoi.yacht?.name ?? '';
tokenMap['{{yacht.hullNumber}}'] = eoi.yacht?.hullNumber ?? '';
tokenMap['{{yacht.flag}}'] = eoi.yacht?.flag ?? '';
tokenMap['{{yacht.yearBuilt}}'] =
eoi.yacht.yearBuilt != null ? String(eoi.yacht.yearBuilt) : '';
tokenMap['{{yacht.lengthFt}}'] = eoi.yacht.lengthFt ?? '';
tokenMap['{{yacht.widthFt}}'] = eoi.yacht.widthFt ?? '';
tokenMap['{{yacht.draftFt}}'] = eoi.yacht.draftFt ?? '';
tokenMap['{{yacht.lengthM}}'] = eoi.yacht.lengthM ?? '';
tokenMap['{{yacht.widthM}}'] = eoi.yacht.widthM ?? '';
tokenMap['{{yacht.draftM}}'] = eoi.yacht.draftM ?? '';
eoi.yacht?.yearBuilt != null ? String(eoi.yacht.yearBuilt) : '';
tokenMap['{{yacht.lengthFt}}'] = eoi.yacht?.lengthFt ?? '';
tokenMap['{{yacht.widthFt}}'] = eoi.yacht?.widthFt ?? '';
tokenMap['{{yacht.draftFt}}'] = eoi.yacht?.draftFt ?? '';
tokenMap['{{yacht.lengthM}}'] = eoi.yacht?.lengthM ?? '';
tokenMap['{{yacht.widthM}}'] = eoi.yacht?.widthM ?? '';
tokenMap['{{yacht.draftM}}'] = eoi.yacht?.draftM ?? '';
// EoiContext doesn't expose the yacht.registration column — look it up
// separately (cheap, indexed fetch) so the token resolves when present.
@@ -281,29 +283,31 @@ export async function resolveTemplate(
tokenMap['{{owner.name}}'] = eoi.owner.name;
tokenMap['{{owner.legalName}}'] = eoi.owner.legalName ?? '';
// Berth tokens (from EoiContext)
tokenMap['{{berth.mooringNumber}}'] = eoi.berth.mooringNumber;
tokenMap['{{berth.area}}'] = eoi.berth.area ?? '';
tokenMap['{{berth.lengthFt}}'] = eoi.berth.lengthFt ?? '';
tokenMap['{{berth.price}}'] = eoi.berth.price ?? '';
tokenMap['{{berth.priceCurrency}}'] = eoi.berth.priceCurrency;
tokenMap['{{berth.tenureType}}'] = eoi.berth.tenureType;
// Berth tokens — also optional. Render empty when no berth is linked.
tokenMap['{{berth.mooringNumber}}'] = eoi.berth?.mooringNumber ?? '';
tokenMap['{{berth.area}}'] = eoi.berth?.area ?? '';
tokenMap['{{berth.lengthFt}}'] = eoi.berth?.lengthFt ?? '';
tokenMap['{{berth.price}}'] = eoi.berth?.price ?? '';
tokenMap['{{berth.priceCurrency}}'] = eoi.berth?.priceCurrency ?? '';
tokenMap['{{berth.tenureType}}'] = eoi.berth?.tenureType ?? '';
// Interest tokens
tokenMap['{{interest.stage}}'] = eoi.interest.stage;
tokenMap['{{interest.leadCategory}}'] = eoi.interest.leadCategory ?? '';
tokenMap['{{interest.berthNumber}}'] = eoi.berth.mooringNumber;
tokenMap['{{interest.berthNumber}}'] = eoi.berth?.mooringNumber ?? '';
tokenMap['{{interest.dateFirstContact}}'] = eoi.interest.dateFirstContact
? eoi.interest.dateFirstContact.toLocaleDateString('en-GB')
: '';
tokenMap['{{interest.notes}}'] = eoi.interest.notes ?? '';
} catch (err) {
// buildEoiContext throws ValidationError when the interest has no yacht
// or berth; non-EOI templates don't need those. Fall through to the
// legacy resolution path below. Re-throw anything else.
// buildEoiContext throws ValidationError when the EOI's required client
// fields (name/email/address — Section 2) are missing. For non-EOI
// templates (correspondence, welcome letters, etc.) those gates don't
// apply — fall through to the legacy resolution path below. Re-throw
// anything else.
if (
!(err instanceof ValidationError) ||
!/interest has no (yacht|berth)/i.test(err.message)
!/missing required client details|interest has no (yacht|berth)/i.test(err.message)
) {
throw err;
}