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

@@ -0,0 +1,29 @@
import { NextResponse } from 'next/server';
import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { promoteContactToPrimary } from '@/lib/services/clients.service';
/**
* Phase 3d — promote a non-primary `client_contacts` row to primary,
* demoting the prior primary for the same channel inside a single
* transaction. Surfaces from the "[EOI] Set as primary" action on the
* client detail panel, and from the EOI dialog's "Set as default for
* future docs" toggle (though that path calls the service directly via
* `eoi-overrides.service`).
*/
export const POST = withAuth(
withPermission('clients', 'edit', async (_req, ctx, params) => {
try {
const contact = await promoteContactToPrimary(params.contactId!, params.id!, ctx.portId, {
userId: ctx.userId,
portId: ctx.portId,
ipAddress: ctx.ipAddress,
userAgent: ctx.userAgent,
});
return NextResponse.json({ data: contact });
} catch (error) {
return errorResponse(error);
}
}),
);

View File

@@ -26,7 +26,7 @@ export const POST = withAuth(
ipAddress: ctx.ipAddress,
userAgent: ctx.userAgent,
},
{ dimensionUnit: body.dimensionUnit },
{ dimensionUnit: body.dimensionUnit, overrides: body.overrides },
);
return NextResponse.json({ data: result }, { status: 201 });
} catch (error) {

View File

@@ -1,24 +1,66 @@
import { NextResponse } from 'next/server';
import { and, desc, eq } from 'drizzle-orm';
import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { db } from '@/lib/db';
import { interests } from '@/lib/db/schema/interests';
import { clientContacts } from '@/lib/db/schema/clients';
import { errorResponse, NotFoundError } from '@/lib/errors';
import { buildEoiContext } from '@/lib/services/eoi-context';
/**
* Returns the resolved `EoiContext` the actual data that would be
* auto-filled into the EOI document — for the given interest. Drives
* the EOI generate dialog's pre-flight preview so a sales rep can see
* (and correct) every value before sending the document for signing.
* Returns the resolved `EoiContext` for the given interest. Drives the
* EOI generate dialog's pre-flight preview so a sales rep can see (and
* correct) every value before sending the document for signing.
*
* No mutation; pure read of denormalized data the EOI builder already
* computes server-side. Returns 404 if the interest is missing or in
* another port (the buildEoiContext function throws NotFoundError).
* Augments the core context with `available.emails` / `available.phones`
* — every non-deleted client_contacts row for the linked client. The
* dialog renders these as combobox options so the rep can pick a
* secondary contact for this EOI (Phase 3b).
*
* No mutation; pure read.
*/
export const GET = withAuth(
withPermission('interests', 'view', async (_req, ctx, params) => {
try {
const context = await buildEoiContext(params.id!, ctx.portId);
return NextResponse.json({ data: context });
// Resolve the linked client to fetch every contact row. The core
// context only carries the single "primary" value; the dialog needs
// the full set to render the combobox.
const interest = await db.query.interests.findFirst({
where: and(eq(interests.id, params.id!), eq(interests.portId, ctx.portId)),
});
if (!interest) throw new NotFoundError('Interest');
const contactRows = await db
.select({
id: clientContacts.id,
channel: clientContacts.channel,
value: clientContacts.value,
isPrimary: clientContacts.isPrimary,
source: clientContacts.source,
})
.from(clientContacts)
.where(eq(clientContacts.clientId, interest.clientId))
.orderBy(desc(clientContacts.isPrimary), desc(clientContacts.updatedAt));
const available = {
emails: contactRows
.filter((c) => c.channel === 'email')
.map((c) => ({ id: c.id, value: c.value, isPrimary: c.isPrimary, source: c.source })),
phones: contactRows
.filter((c) => c.channel === 'phone' || c.channel === 'whatsapp')
.map((c) => ({
id: c.id,
value: c.value,
isPrimary: c.isPrimary,
channel: c.channel,
source: c.source,
})),
};
return NextResponse.json({ data: { ...context, available } });
} catch (error) {
return errorResponse(error);
}

View File

@@ -65,6 +65,14 @@ const updateProfileSchema = z.object({
.strict(),
)
.optional(),
// Phase 4 — per-user default reminder firing time-of-day. HH:MM in
// 24h local clock (validated below). Per-reminder dueAt overrides
// this; this is only the dialog's default when the rep doesn't
// pick an explicit time. Server clamps to '00:00''23:59'.
digestTimeOfDay: z
.string()
.regex(/^([01]\d|2[0-3]):[0-5]\d$/, 'use HH:MM 24-hour format')
.optional(),
})
.strict()
.optional(),
@@ -161,6 +169,8 @@ export const PATCH = withAuth(async (req, ctx: AuthContext) => {
'timezone',
'tablePreferences',
'defaultPortId',
// Phase 4 — reminder default firing time.
'digestTimeOfDay',
]);
const existing = (profile.preferences as Record<string, unknown>) ?? {};
const merged = Object.fromEntries(