feat(uat-batch): Group M — universal preview + field-history foundation

M42, M43 from the 2026-05-21 plan.

Shipped:
  M42  FilePreviewDialog now handles seven preview kinds via a single
       previewKindFor() router (mime + filename fallback). Image and
       PDF stay on the existing lightbox + pdf viewer; plain text
       (.txt / .md / .csv / .tsv / .json / .xml / .log / .yaml / .ini
       / .html — text/* and application/json and friends) renders via
       a new <TextPreview> that fetches via the presigned URL and
       caps the body at 1 MB with a "showing first 1 MB" banner.
       Audio / video render through native HTML5 <audio> / <video>
       elements with preload="metadata". Office documents (.docx /
       .xlsx / .pptx / .odt / .ods / .odp + the official mime variants)
       embed via Microsoft's hosted Office viewer (view.officeapps
       .live.com/op/embed.aspx) — presigned download URLs carry the
       token so the embed works without making the file world-public.
       Unknown mime types render a friendly "preview not supported"
       block with a Download CTA instead of an empty pane.
  M43  Field-level override history foundation. Migration 0081 adds
       `interest_field_history` (id, port_id, interest_id?, client_id?,
       field_path, old_value, new_value, source, submission_id?,
       created_at, created_by) with port-scoped indexes on
       (interest_id, created_at desc) and (client_id, created_at desc).
       Drizzle schema + index exports added. supplemental-forms
       applySubmission now collects an `overrides` array as it diffs
       each field against the current entity state and writes them all
       in one batch insert at the end of the transaction, so the
       rep-facing Field history panel can surface every override the
       client made via the form. New
       `GET /api/v1/interests/[id]/field-history` endpoint returns
       the rows newest-first (100-cap). Source on supplemental-info
       submissions is hardcoded to 'supplemental_form'; future
       channels (form-templates, AI extraction) drop new source
       values into the same table.

       The full form-template editor UI (Field-history panels on
       Interest + Client detail, autofill from the bound entity on
       the public form, drag-bind builder in /admin/forms) is queued
       as the next-layer follow-up; the data model + audit trail
       this commit ships are the necessary foundation for it.

Verified: tsc clean, vitest 1454/1454, migration applied.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-21 23:21:14 +02:00
parent 65ff5961f2
commit 0ddaf462c7
6 changed files with 377 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ import {
clientAddresses,
yachts,
clientContacts,
interestFieldHistory,
} from '@/lib/db/schema';
import { ConflictError, NotFoundError, ValidationError } from '@/lib/errors';
@@ -297,9 +298,26 @@ export async function applySubmission(token: string, input: SubmissionInput): Pr
const client = await tx.query.clients.findFirst({ where: eq(clients.id, row.clientId) });
if (!client) throw new NotFoundError('client');
// Track every field-level override we apply this submission so we
// can write them to interest_field_history below in a single batch.
// The shape is { fieldPath, oldValue, newValue } — submission_id is
// attached after we have a form_submissions row id (if/when that
// surface lands; today supplemental-info doesn't materialise a
// form_submissions row, so submissionId stays null).
const overrides: Array<{
fieldPath: string;
oldValue: unknown;
newValue: unknown;
}> = [];
// Client patch: name lives on clients; address fields live on the
// dedicated client_addresses row. fullName is required so always sent.
if (input.fullName.trim() !== client.fullName) {
overrides.push({
fieldPath: 'client.fullName',
oldValue: client.fullName,
newValue: input.fullName.trim(),
});
await tx
.update(clients)
.set({ fullName: input.fullName.trim() })
@@ -321,10 +339,22 @@ export async function applySubmission(token: string, input: SubmissionInput): Pr
});
} else {
const addrPatch: Record<string, unknown> = {};
if (input.address && input.address !== existingAddr.streetAddress)
if (input.address && input.address !== existingAddr.streetAddress) {
addrPatch.streetAddress = input.address;
if (input.country && input.country !== existingAddr.countryIso)
overrides.push({
fieldPath: 'client.address.streetAddress',
oldValue: existingAddr.streetAddress,
newValue: input.address,
});
}
if (input.country && input.country !== existingAddr.countryIso) {
addrPatch.countryIso = input.country;
overrides.push({
fieldPath: 'client.address.countryIso',
oldValue: existingAddr.countryIso,
newValue: input.country,
});
}
if (Object.keys(addrPatch).length > 0) {
await tx
.update(clientAddresses)
@@ -346,7 +376,17 @@ export async function applySubmission(token: string, input: SubmissionInput): Pr
value: input.email.trim().toLowerCase(),
isPrimary: true,
});
overrides.push({
fieldPath: 'client.primaryEmail',
oldValue: null,
newValue: input.email.trim().toLowerCase(),
});
} else if (existing.value !== input.email.trim().toLowerCase()) {
overrides.push({
fieldPath: 'client.primaryEmail',
oldValue: existing.value,
newValue: input.email.trim().toLowerCase(),
});
await tx
.update(clientContacts)
.set({ value: input.email.trim().toLowerCase() })
@@ -411,5 +451,25 @@ export async function applySubmission(token: string, input: SubmissionInput): Pr
.update(supplementalFormTokens)
.set({ consumedAt: new Date() })
.where(eq(supplementalFormTokens.id, row.id));
// Write the diff log so the Field history panel on Interest +
// Client detail can surface every override the client made via the
// form. Empty array means nothing actually changed — the client
// confirmed every field as-was — in which case we skip the batch.
if (overrides.length > 0) {
await tx.insert(interestFieldHistory).values(
overrides.map((o) => ({
portId: row.portId,
interestId: row.interestId,
clientId: row.clientId,
fieldPath: o.fieldPath,
oldValue: o.oldValue as unknown,
newValue: o.newValue as unknown,
source: 'supplemental_form' as const,
submissionId: null,
createdBy: null,
})),
);
}
});
}