fix(audit-v3): platform-wide deferred-list cleanup (rounds 1-4)

Working through the audit-v2 deferred backlog. Each round was tested
(typecheck + 1168/1168 vitest) before moving on.

Round 1 — DB performance + AI cost visibility:
- Add missing FK indexes Postgres doesn't auto-create on
  berth_reservations.{interest_id, contract_file_id},
  documents.{file_id, signed_file_id}, document_events.signer_id,
  document_templates.source_file_id, form_submissions.{form_template_id,
  client_id}, document_sends.{brochure_id, brochure_version_id,
  sent_by_user_id}. Without these, RESTRICT-checks on parent delete +
  reverse-lookups walk the child tables fully. Migration 0037.
- AI worker now writes one ai_usage_ledger row per OpenAI call so admins
  can audit spend per port/user/feature and future per-port budgets have
  history to read from. Failure to write is logged-not-thrown so the
  user-facing email draft is unaffected.

Round 2 — Boot-time + transport hardening:
- S3 backend verifies the bucket exists at startup (or auto-creates
  when MINIO_AUTO_CREATE_BUCKET=true). A typo'd bucket name now
  surfaces with a clear boot error instead of a vague Minio error
  inside the first user-facing request.
- Documenso v1 placeFields: 3-attempt exponential-backoff retry on 5xx
  + network errors, fail-fast on 4xx. Stops one transient flake from
  leaving a document with a partial field set.
- FilesystemBackend logs a structured warn-once at boot when the dev
  HMAC fallback is in effect, so two processes started with different
  BETTER_AUTH_SECRET values are observable (random 401s on file
  downloads otherwise).
- Logger redact paths extended to cover *.headers.{authorization,
  cookie}, *.config.headers.authorization, encrypted-credential blobs
  (secretKeyEncrypted, smtpPassEncrypted, etc.), the Documenso
  X-Documenso-Secret header, and 2-level nested forms.

Round 3 — UI feedback + permission gates:
- Storage admin migrate dialog: success toast with row count + error
  toast on both dryRun and migrate mutations.
- Invoice detail Send + Record-payment buttons wrapped in
  PermissionGate (invoices.send / invoices.record_payment); both
  mutations now toast on success/error.
- Admin user list Edit button wrapped in PermissionGate(admin.manage_users).
- Scan-receipt page surfaces an amber warning when OCR fails so reps
  know they can fill the form manually instead of staring at a stalled
  spinner; the editable form now also opens on scanMutation.isError
  / uploadedFile, not only on success.
- Email threads list now renders skeleton rows during load + shared
  EmptyState for the empty case (was a single "Loading…" line).

Round 4 — Service / route correctness:
- documentSends.sent_by_user_id was a free-text NOT NULL column with no
  FK. Now nullable + FK to user(id) ON DELETE SET NULL so the audit row
  survives a user being hard-deleted. Migration 0038 with a defensive
  null-out for any orphan ids before attaching the constraint.
- Saved-views route: documented why withAuth alone is correct (the
  service strictly filters by (portId, userId) — owner-only by design).
- Public-interests audit log: replaced "userId: null as unknown as
  string" cast with userId: null; AuditLogParams already accepts null
  for system-generated events.
- EOI in-app PDF fill: extracted setBerthRange() that, when the
  AcroForm field is missing AND the context has a non-empty range
  string, logs a structured warn so the deployment gap (live Documenso
  template needs the field) is observable instead of silently dropping
  the multi-berth range.

Test status: 1168/1168 vitest. tsc clean. Two new migrations
(0037/0038) need pnpm db:push (or migration apply) on the dev DB.
Deferred-doc updated with the remaining open items (bigger refactors).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Ciaccio
2026-05-05 12:49:53 +02:00
parent ade4c9e77d
commit 687a1f1c2f
20 changed files with 442 additions and 103 deletions

View File

@@ -3,6 +3,7 @@
import { useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { CheckCircle2, HardDrive, Loader2, RefreshCw, ServerCog, XCircle } from 'lucide-react';
import { toast } from 'sonner';
import { PageHeader } from '@/components/shared/page-header';
import { Button } from '@/components/ui/button';
@@ -56,6 +57,8 @@ export function StorageAdminPanel() {
setDryRun(result.data);
setConfirmOpen(true);
},
onError: (e) =>
toast.error(e instanceof Error ? e.message : 'Storage migration dry-run failed'),
});
const migrateMutation = useMutation({
@@ -64,11 +67,14 @@ export function StorageAdminPanel() {
method: 'POST',
body: JSON.stringify({ ...opts, dryRun: false }),
}),
onSuccess: () => {
onSuccess: (result) => {
setConfirmOpen(false);
setDryRun(null);
const copied = result.data.rowsMigrated ?? 0;
toast.success(`Storage migration complete (${copied} file${copied === 1 ? '' : 's'} copied)`);
queryClient.invalidateQueries({ queryKey: ['admin', 'storage', 'status'] });
},
onError: (e) => toast.error(e instanceof Error ? e.message : 'Storage migration failed'),
});
const testMutation = useMutation({

View File

@@ -7,6 +7,7 @@ import { Pencil, Trash2, Plus, ShieldCheck, ShieldOff } from 'lucide-react';
import { DataTable } from '@/components/shared/data-table';
import { PageHeader } from '@/components/shared/page-header';
import { ConfirmationDialog } from '@/components/shared/confirmation-dialog';
import { PermissionGate } from '@/components/shared/permission-gate';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { apiFetch } from '@/lib/api/client';
@@ -111,10 +112,12 @@ export function UserList() {
header: '',
cell: ({ row }) => (
<div className="flex items-center justify-end gap-1">
<Button variant="ghost" size="sm" onClick={() => handleEditUser(row.original)}>
<Pencil className="h-4 w-4" />
<span className="sr-only">Edit</span>
</Button>
<PermissionGate resource="admin" action="manage_users">
<Button variant="ghost" size="sm" onClick={() => handleEditUser(row.original)}>
<Pencil className="h-4 w-4" />
<span className="sr-only">Edit</span>
</Button>
</PermissionGate>
<ConfirmationDialog
trigger={
<Button variant="ghost" size="sm" className="text-destructive hover:text-destructive">

View File

@@ -5,6 +5,8 @@ import { formatDistanceToNow } from 'date-fns';
import { Mail } from 'lucide-react';
import { apiFetch } from '@/lib/api/client';
import { EmptyState } from '@/components/shared/empty-state';
import { Skeleton } from '@/components/ui/skeleton';
interface Thread {
id: string;
@@ -27,20 +29,32 @@ export function EmailThreadsList() {
});
if (isLoading) {
return <p className="text-sm text-muted-foreground">Loading threads</p>;
// Skeleton rows shaped like the real list so the layout doesn't pop.
return (
<div className="rounded-lg border divide-y">
{Array.from({ length: 4 }).map((_, i) => (
<div key={i} className="p-3 space-y-2">
<div className="flex items-center justify-between gap-2">
<Skeleton className="h-4 w-1/3" />
<Skeleton className="h-3 w-16" />
</div>
<Skeleton className="h-3 w-1/2" />
<Skeleton className="h-3 w-2/3" />
</div>
))}
</div>
);
}
const threads = data?.data ?? [];
if (threads.length === 0) {
return (
<div className="rounded-lg border border-dashed p-8 text-center text-muted-foreground">
<Mail className="mx-auto h-6 w-6 mb-2" />
<p className="text-sm">No email threads yet.</p>
<p className="text-xs">
Connect an account and trigger a sync to see incoming threads here.
</p>
</div>
<EmptyState
icon={Mail}
title="No email threads yet"
description="Connect an account and trigger a sync to see incoming threads here."
/>
);
}

View File

@@ -11,6 +11,8 @@ import { format } from 'date-fns';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { PermissionGate } from '@/components/shared/permission-gate';
import { toast } from 'sonner';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import {
@@ -93,9 +95,11 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
const sendMutation = useMutation({
mutationFn: () => apiFetch(`/api/v1/invoices/${invoiceId}/send`, { method: 'POST' }),
onSuccess: () => {
toast.success('Invoice sent');
queryClient.invalidateQueries({ queryKey: ['invoices', invoiceId] });
queryClient.invalidateQueries({ queryKey: ['invoices'] });
},
onError: (e) => toast.error(e instanceof Error ? e.message : 'Could not send invoice'),
});
const paymentForm = useForm<RecordPaymentInput>({
@@ -110,9 +114,11 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
body: values,
}),
onSuccess: () => {
toast.success('Payment recorded');
queryClient.invalidateQueries({ queryKey: ['invoices', invoiceId] });
queryClient.invalidateQueries({ queryKey: ['invoices'] });
},
onError: (e) => toast.error(e instanceof Error ? e.message : 'Could not record payment'),
});
if (isLoading) {
@@ -150,19 +156,21 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
</div>
<div className="flex items-center gap-2">
{invoice.status === 'draft' && (
<Button
variant="outline"
size="sm"
onClick={() => sendMutation.mutate()}
disabled={sendMutation.isPending}
>
{sendMutation.isPending ? (
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<Send className="mr-1.5 h-4 w-4" />
)}
Send Invoice
</Button>
<PermissionGate resource="invoices" action="send">
<Button
variant="outline"
size="sm"
onClick={() => sendMutation.mutate()}
disabled={sendMutation.isPending}
>
{sendMutation.isPending ? (
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<Send className="mr-1.5 h-4 w-4" />
)}
Send Invoice
</Button>
</PermissionGate>
)}
</div>
</div>
@@ -347,63 +355,69 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
</CardContent>
</Card>
) : (
<Card>
<CardHeader>
<CardTitle className="text-sm font-medium">Record Payment</CardTitle>
</CardHeader>
<CardContent>
<form
onSubmit={paymentForm.handleSubmit((values) => paymentMutation.mutate(values))}
className="space-y-4"
>
<div className="space-y-1">
<Label htmlFor="paymentDate">Payment Date</Label>
<Input id="paymentDate" type="date" {...paymentForm.register('paymentDate')} />
{paymentForm.formState.errors.paymentDate && (
<p className="text-xs text-destructive">
{paymentForm.formState.errors.paymentDate.message}
</p>
)}
</div>
<div className="space-y-1">
<Label htmlFor="paymentMethod">Payment Method</Label>
<Select
value={paymentForm.watch('paymentMethod') ?? ''}
onValueChange={(v) =>
paymentForm.setValue('paymentMethod', v, { shouldValidate: true })
}
>
<SelectTrigger id="paymentMethod">
<SelectValue placeholder="Select a method" />
</SelectTrigger>
<SelectContent>
{PAYMENT_METHOD_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{opt.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="paymentReference">Reference / Transaction ID</Label>
<Input
id="paymentReference"
placeholder="Optional reference"
{...paymentForm.register('paymentReference')}
/>
</div>
<Button type="submit" disabled={paymentMutation.isPending}>
{paymentMutation.isPending ? (
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<CreditCard className="mr-1.5 h-4 w-4" />
)}
Mark as Paid
</Button>
</form>
</CardContent>
</Card>
<PermissionGate resource="invoices" action="record_payment">
<Card>
<CardHeader>
<CardTitle className="text-sm font-medium">Record Payment</CardTitle>
</CardHeader>
<CardContent>
<form
onSubmit={paymentForm.handleSubmit((values) => paymentMutation.mutate(values))}
className="space-y-4"
>
<div className="space-y-1">
<Label htmlFor="paymentDate">Payment Date</Label>
<Input
id="paymentDate"
type="date"
{...paymentForm.register('paymentDate')}
/>
{paymentForm.formState.errors.paymentDate && (
<p className="text-xs text-destructive">
{paymentForm.formState.errors.paymentDate.message}
</p>
)}
</div>
<div className="space-y-1">
<Label htmlFor="paymentMethod">Payment Method</Label>
<Select
value={paymentForm.watch('paymentMethod') ?? ''}
onValueChange={(v) =>
paymentForm.setValue('paymentMethod', v, { shouldValidate: true })
}
>
<SelectTrigger id="paymentMethod">
<SelectValue placeholder="Select a method" />
</SelectTrigger>
<SelectContent>
{PAYMENT_METHOD_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{opt.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="paymentReference">Reference / Transaction ID</Label>
<Input
id="paymentReference"
placeholder="Optional reference"
{...paymentForm.register('paymentReference')}
/>
</div>
<Button type="submit" disabled={paymentMutation.isPending}>
{paymentMutation.isPending ? (
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<CreditCard className="mr-1.5 h-4 w-4" />
)}
Mark as Paid
</Button>
</form>
</CardContent>
</Card>
</PermissionGate>
)}
</TabsContent>
</Tabs>