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

@@ -80,3 +80,8 @@ export * from './reports';
// Relations (must come last - references all tables)
export * from './relations';
export * from './tracked-links';
// Field-level override history (supplemental-info submissions, future
// form-template binding). Renders the "Field history" panel on the
// Interest + Client detail pages.
export * from './interest-field-history';

View File

@@ -0,0 +1,63 @@
/**
* Field-level override history for interests + their linked clients.
*
* Every time a field on an interest or its linked client is overridden
* via an explicit channel (today: supplemental-info form submission;
* future: form-templates, AI-assisted extraction acceptance), a row
* lands here. Distinct from `audit_logs` — that table tracks every
* CRUD event for compliance; this one tracks only deliberate overrides
* so the Interest + Client "Field history" panels can surface them
* compactly.
*/
import { pgTable, text, jsonb, timestamp, index } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
import { ports } from './ports';
import { interests } from './interests';
import { clients } from './clients';
export const interestFieldHistory = pgTable(
'interest_field_history',
{
id: text('id')
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
portId: text('port_id')
.notNull()
.references(() => ports.id),
interestId: text('interest_id').references(() => interests.id, { onDelete: 'cascade' }),
/** Denormalized for fast lookup on the Client detail "Field history"
* panel — overrides that come in via a supplemental-info form
* carry both interest + client refs. Direct-edit overrides may
* only carry one. */
clientId: text('client_id').references(() => clients.id, { onDelete: 'cascade' }),
/** Dotted path of the field that was overridden. Examples:
* 'client.fullName'
* 'interest.desiredLengthFt'
* 'client.address.streetAddress'
* The Field history panel formats this for display. */
fieldPath: text('field_path').notNull(),
oldValue: jsonb('old_value'),
newValue: jsonb('new_value').notNull(),
/** Provenance: 'supplemental_form' | 'rep_edit' | 'system_inferred' |
* 'ai_extraction' (future). Drives the "Submitted via X" copy. */
source: text('source').notNull(),
/** Optional FK to the form_submissions row that triggered the
* override. Lets the UI link back to the original submission. */
submissionId: text('submission_id'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
createdBy: text('created_by'),
},
(table) => [
index('idx_ifh_interest_created')
.on(table.portId, table.interestId, table.createdAt)
.where(sql`${table.interestId} IS NOT NULL`),
index('idx_ifh_client_created')
.on(table.portId, table.clientId, table.createdAt)
.where(sql`${table.clientId} IS NOT NULL`),
],
);
export type InterestFieldHistory = typeof interestFieldHistory.$inferSelect;
export type NewInterestFieldHistory = typeof interestFieldHistory.$inferInsert;