feat(post-audit): Phase 3 EOI overrides + 3c spawn + 3d promote + Phase 4 worker

Phase 3b — EOI dialog field overrides:
- New EoiOverridesInput shape (clientEmail / clientPhone / yachtName)
  threaded through generate-and-sign validator + both pathways
  (in-app pdf-lib fill, Documenso template generate).
- src/lib/services/eoi-overrides.service.ts applies side-effects in one
  transaction: useOnlyForThisEoi writes documents.override_* and stops;
  setAsDefault demotes the prior primary + promotes (existing contactId)
  or inserts + promotes (fresh value); neither flag inserts a non-primary
  client_contacts row for future dropdown reuse.
- Document override columns persisted post-insert, with a 1-minute
  source_document_id backfill on freshly inserted contact rows.
- eoi-context route returns available.{emails, phones} so the dialog
  can render combobox options.
- <OverridableContactField> in eoi-generate-dialog.tsx renders the
  combobox + manual input + 2 checkboxes per field with mutually
  exclusive intent semantics.

Phase 3c — yacht spawn from EOI dialog:
- YachtForm gains createExtras + onCreated callbacks; the EOI dialog
  opens it as a nested Sheet pre-filled with the linked client as owner.
  On save the new yacht is stamped source='eoi-generated' and the
  interest is PATCHed with the new yachtId so the EOI context reflows.

Phase 3d — promote-to-primary + audit + [EOI] badge:
- POST /api/v1/clients/:id/contacts/:contactId/promote-to-primary
  (transactional demote+promote via promoteContactToPrimary).
- src/lib/audit.ts AuditAction type adds eoi_field_override,
  promote_to_primary, eoi_spawn_yacht (DB column is free-text).
- ContactsEditor surfaces an [EOI] badge on non-primary rows where
  source='eoi-custom-input'.

Phase 4 — worker + TOD picker:
- processOverdueReminders refactored to UPDATE...RETURNING with a
  fired_at IS NULL gate so parallel workers can't double-fire. Uses
  the idx_reminders_due_unfired partial index from migration 0072.
- /settings gets a "Default reminder time" time-of-day picker; the
  value lands in user_profiles.preferences.digestTimeOfDay (validated
  HH:MM at the route). <ReminderForm> seeds its dueAt from this
  preference via a React-Query me-prefs fetch.

Phase 6 hardening:
- IMAP bounce poller strips whitespace from IMAP_PASS so a copy-paste
  of Google Workspace's 16-char App Password formatted as
  "abcd efgh ijkl mnop" still authenticates. Workspace activation
  procedure documented in MASTER-PLAN §Phase 6 (was previously written
  to CLAUDE.md, which was bloat — moved to the plan).

Quality gates: 1374/1374 vitest, tsc clean, lint 0 errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-18 16:18:03 +02:00
parent 503207ef68
commit eaab14943b
20 changed files with 1119 additions and 92 deletions

View File

@@ -28,6 +28,13 @@ import { getPortDocumensoConfig } from '@/lib/services/port-config';
import { generateEoiPdfFromTemplate } from '@/lib/pdf/fill-eoi-form';
import { MERGE_FIELDS, type MergeFieldCatalog } from '@/lib/templates/merge-fields';
import { buildEoiContext } from '@/lib/services/eoi-context';
import {
applyEoiOverridesBeforeRender,
applyOverridesToContext,
persistDocumentOverrides,
type EoiOverridesInput,
type AppliedOverrides,
} from '@/lib/services/eoi-overrides.service';
import { getPrimaryBerth } from '@/lib/services/interest-berths.service';
import type {
CreateTemplateInput,
@@ -484,12 +491,16 @@ async function generateEoiFromSourcePdf(
context: GenerateInput,
meta: AuditMeta,
options?: { dimensionUnit?: 'ft' | 'm' },
applied: AppliedOverrides = { resolved: {}, documentOverrideColumns: {} },
): Promise<{ document: DbDocument; file: DbFile }> {
if (!context.interestId) {
throw new ValidationError('interestId is required for EOI template generation');
}
const eoiContext = await buildEoiContext(context.interestId, portId);
const eoiContext = applyOverridesToContext(
await buildEoiContext(context.interestId, portId),
applied,
);
const pdfBytes = await generateEoiPdfFromTemplate(eoiContext, {
dimensionUnit: options?.dimensionUnit ?? eoiContext.yacht?.lengthUnit ?? 'ft',
});
@@ -586,15 +597,24 @@ export async function generateAndSign(
signers: GenerateAndSignInput['signers'],
pathway: 'inapp' | 'documenso-template',
meta: AuditMeta,
options?: { dimensionUnit?: 'ft' | 'm' },
options?: { dimensionUnit?: 'ft' | 'm'; overrides?: EoiOverridesInput },
) {
// Phase 3b — apply per-field overrides BEFORE either pathway resolves the
// EOI context, so any setAsDefault contact promotion is visible to the
// buildEoiContext read. The returned `applied.resolved` is layered onto
// the in-memory context for useOnlyForThisEoi / fresh-value cases where
// the canonical record isn't being touched.
const applied = context.interestId
? await applyEoiOverridesBeforeRender(portId, context.interestId, options?.overrides, meta)
: { resolved: {}, documentOverrideColumns: {} };
if (pathway === 'documenso-template') {
return generateAndSignViaDocumensoTemplate(portId, context, meta, options);
return generateAndSignViaDocumensoTemplate(portId, context, meta, options, applied);
}
if (!templateId) {
throw new ValidationError('templateId is required for inapp pathway');
}
return generateAndSignViaInApp(templateId, portId, context, signers, meta, options);
return generateAndSignViaInApp(templateId, portId, context, signers, meta, options, applied);
}
async function generateAndSignViaInApp(
@@ -604,6 +624,7 @@ async function generateAndSignViaInApp(
signers: GenerateAndSignInput['signers'],
meta: AuditMeta,
options?: { dimensionUnit?: 'ft' | 'm' },
applied: AppliedOverrides = { resolved: {}, documentOverrideColumns: {} },
) {
const template = await getTemplateById(templateId, portId);
@@ -662,8 +683,14 @@ async function generateAndSignViaInApp(
context,
meta,
options,
applied,
);
// Phase 3b — record per-document override columns + backfill the
// source_document_id on any client_contacts rows inserted during the
// override side-effects.
await persistDocumentOverrides(documentRecord.id, applied, meta);
// Fetch PDF bytes from the active storage backend to send to Documenso.
const pdfStream = await (await getStorageBackend()).get(file.storagePath);
const chunks: Buffer[] = [];
@@ -729,12 +756,16 @@ async function generateAndSignViaDocumensoTemplate(
context: GenerateInput,
meta: AuditMeta,
options?: { dimensionUnit?: 'ft' | 'm' },
applied: AppliedOverrides = { resolved: {}, documentOverrideColumns: {} },
) {
if (!context.interestId) {
throw new ValidationError('interestId is required for documenso-template pathway');
}
const eoiContext = await buildEoiContext(context.interestId, portId);
const eoiContext = applyOverridesToContext(
await buildEoiContext(context.interestId, portId),
applied,
);
const signers = await getPortEoiSigners(portId);
// Per-port Documenso template + recipient IDs (with env fallback). Each
// tenant pointing at its own Documenso instance has different numeric
@@ -800,6 +831,10 @@ async function generateAndSignViaDocumensoTemplate(
})
.returning();
// Phase 3b — record any per-document override columns + backfill
// source_document_id on freshly inserted contact rows.
await persistDocumentOverrides(documentRecord!.id, applied, meta);
// Persist the per-recipient signer rows from Documenso's create response.
// Without these the EOI tab's "Signing progress" panel shows
// "No signers loaded" forever (the webhook handler only updates existing