feat(pipeline): 9→7 stage refactor + v1.1 hardening wave

Replaces the legacy 9-stage pipeline with 7 canonical stages
(enquiry → qualified → eoi → reservation → deposit_paid → contract →
nurturing) plus three doc sub-status columns (eoi_doc_status,
reservation_doc_status, contract_doc_status) that track sent/signed
within a single stage instead of branching it.

Schema (migration 0062):
- interests gains assigned_to, deposit_expected_amount/currency,
  three doc-status columns, two documenso-id columns, and
  date_reservation_signed.
- New tables: qualification_criteria (per-port admin-configurable),
  interest_qualifications (per-interest state), payments (deposit /
  balance / refund records keyed to interest + client).
- Default qualification criteria seeded for every existing port.
- Dummy-data UPDATEs collapse Sent/Signed pairs and 'completed' into
  the new stage + doc-status + outcome shape.

Migration 0063 adds interest_contact_log.voice_transcript and
template_used columns for v1.1-A/B (quick-template buttons + voice
transcription via Web Speech API).

v1.1 phase work bundled here:
- A/B: Quick-template buttons (Call / Visit / Email) + mic toggle on
       the contact-log compose dialog (useVoiceTranscription hook).
- C:   berth-rules-engine wraps state writes in pg_advisory_xact_lock
       with an idempotent re-read; emits rule_evaluated audit traces.
- D:   Documenso webhook: reservation/contract sub-status stamping
       moved out of the PDF-download try-block so a download failure
       no longer swallows the stamp. New integration test coverage.
- E:   /admin/qualification-criteria CRUD page + admin component.
- F:   default_new_interest_owner exposed in System Settings.
- G:   recentActivityCount + active_engagement deal-pulse signal
       surfaced as a chip on interests + hot-deals card.
- H:   interest_assigned notification on assignedTo change (skips
       self-assign, uses a dedupe key).

Plus the supporting components: AssignedToChip, DealPulseChip,
PaymentsSection, QualificationChecklist, MultiEoiChip,
SkipAheadBanner, WonStatusPanel, InterestBerthStatusBanner,
SupplementalInfoRequestButton, UserPicker.

Tests: 1370/1370 vitest pass (added deal-health unit suite +
expanded constants/validators/pipeline-transitions coverage). tsc
clean, eslint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-14 03:39:21 +02:00
parent b10bf9bf8e
commit 6b28459c45
110 changed files with 5402 additions and 796 deletions

View File

@@ -3,27 +3,34 @@
import { useMemo, useState } from 'react';
import Link from 'next/link';
import {
Bell,
Activity,
BarChart3,
BellRing,
BookOpen,
Briefcase,
Database,
ClipboardList,
CopyCheck,
DatabaseBackup,
FilePen,
FileSignature,
FileText,
Globe,
HardDrive,
FileUp,
Inbox,
Key,
LayoutDashboard,
ListChecks,
Mail,
Palette,
MailPlus,
Paintbrush,
ScrollText,
Search,
Send,
Server,
Settings,
Shield,
Sliders,
Ship,
SlidersHorizontal,
Sparkles,
Tag,
Upload,
TrendingUp,
Users,
UsersRound,
Webhook,
X,
} from 'lucide-react';
@@ -83,7 +90,7 @@ const GROUPS: AdminGroup[] = [
href: 'invitations',
label: 'Invitations',
description: 'Send invitations, track pending invites, and resend or revoke them.',
icon: Mail,
icon: MailPlus,
},
{
href: 'roles',
@@ -108,19 +115,19 @@ const GROUPS: AdminGroup[] = [
label: 'EOI signing service',
description:
'API credentials, EOI template, and default in-app vs external signing pathway.',
icon: FileText,
icon: FileSignature,
},
{
href: 'reminders',
label: 'Reminders',
description: 'Default reminder behaviour and the daily-digest delivery window.',
icon: Bell,
icon: BellRing,
},
{
href: 'branding',
label: 'Branding',
description: 'App name, logo, primary color, and email header/footer HTML.',
icon: Palette,
icon: Paintbrush,
},
{
href: 'settings',
@@ -183,7 +190,7 @@ const GROUPS: AdminGroup[] = [
href: 'forms',
label: 'Forms',
description: 'Form templates used by client-facing inquiry and intake flows.',
icon: Sliders,
icon: ClipboardList,
},
{
href: 'templates',
@@ -195,7 +202,7 @@ const GROUPS: AdminGroup[] = [
href: 'email-templates',
label: 'Email Templates',
description: 'Customize subject lines for transactional emails (portal, inquiry, invite).',
icon: Mail,
icon: FilePen,
},
{
href: 'tags',
@@ -214,7 +221,7 @@ const GROUPS: AdminGroup[] = [
href: 'custom-fields',
label: 'Custom Fields',
description: 'Tenant-defined fields for clients, yachts, and reservations.',
icon: Key,
icon: SlidersHorizontal,
},
],
},
@@ -233,19 +240,19 @@ const GROUPS: AdminGroup[] = [
href: 'sends',
label: 'Send Log',
description: 'Brochure and per-berth PDF sends, with delivery failures surfaced for retry.',
icon: Mail,
icon: Send,
},
{
href: 'duplicates',
label: 'Duplicates',
description: 'Review queue of suspected duplicate clients flagged by the dedup engine.',
icon: UsersRound,
icon: CopyCheck,
},
{
href: 'import',
label: 'Bulk Import',
description: 'CSV-driven imports for clients, yachts, and reservations.',
icon: Upload,
icon: FileUp,
},
{
href: 'audit',
@@ -263,26 +270,26 @@ const GROUPS: AdminGroup[] = [
href: 'reports',
label: 'Reports',
description: 'Saved analytics views and ad-hoc query results.',
icon: LayoutDashboard,
icon: BarChart3,
},
{
href: 'monitoring',
label: 'Queue Monitoring',
description: 'BullMQ queue health, throughput, and retry diagnostics.',
icon: Database,
icon: Activity,
},
{
href: 'backup',
label: 'Backup & Restore',
description: 'Backup posture + retention policy (read-only).',
icon: HardDrive,
icon: DatabaseBackup,
},
{
href: 'storage',
label: 'Storage Backend',
description:
'Choose between S3-compatible object store or local filesystem; migrate between them.',
icon: HardDrive,
icon: Server,
},
],
},
@@ -294,14 +301,14 @@ const GROUPS: AdminGroup[] = [
href: 'ports',
label: 'Ports',
description: 'Manage the marinas/ports this installation serves.',
icon: Briefcase,
icon: Ship,
},
{
href: 'onboarding',
label: 'Onboarding checklist',
description:
'Step-by-step setup checklist for fresh ports — auto-detects what youve configured and lets you mark manual steps complete.',
icon: LayoutDashboard,
icon: ListChecks,
},
],
},
@@ -313,22 +320,28 @@ const GROUPS: AdminGroup[] = [
href: 'ai',
label: 'AI configuration',
description:
'Master switch + provider credentials shared by every AI surface (OCR, berth-PDF parser, future recommender embeddings).',
icon: ScrollText,
keywords: ['openai', 'anthropic', 'gpt', 'claude', 'llm', 'api key', 'embeddings'],
},
{
href: 'ocr',
label: 'Receipt OCR (per-feature)',
description: 'Provider, model, and confidence thresholds for the receipt scanner.',
icon: ScrollText,
keywords: ['receipt', 'scan', 'tesseract', 'expense scanner', 'confidence'],
'Master switch, provider credentials, and the Receipt OCR settings in one place. Per-feature pages (berth-PDF parser, recommender) link out from here.',
icon: Sparkles,
keywords: [
'openai',
'anthropic',
'gpt',
'claude',
'llm',
'api key',
'embeddings',
'receipt',
'scan',
'tesseract',
'ocr',
'expense scanner',
],
},
{
href: 'website-analytics',
label: 'Website analytics (Umami)',
description: 'Per-port Umami URL, API token, and Website ID.',
icon: Globe,
icon: TrendingUp,
keywords: ['umami', 'analytics', 'traffic', 'visitors', 'marketing', 'pageviews'],
},
{
@@ -336,9 +349,17 @@ const GROUPS: AdminGroup[] = [
label: 'Residential pipeline stages',
description:
'Configure stages residential interests flow through. Removing a stage with active interests prompts for reassignment.',
icon: ScrollText,
icon: ListChecks,
keywords: ['stages', 'pipeline', 'residential funnel', 'reassign'],
},
{
href: 'qualification-criteria',
label: 'Qualification criteria',
description:
'Checklist reps complete to qualify an enquiry. Enable/disable/reorder per port; affects the soft "ready to qualify" hint on the interest detail.',
icon: ListChecks,
keywords: ['qualification', 'criteria', 'checklist', 'qualify', 'enquiry', 'sales gate'],
},
],
},
];

View File

@@ -314,17 +314,19 @@ function SettingsBlockBody({
);
}
export function OcrSettingsForm() {
export function OcrSettingsForm({ embedded = false }: { embedded?: boolean } = {}) {
const { isSuperAdmin } = usePermissions();
return (
<div className="space-y-6">
<PageHeader
title="Receipt OCR"
eyebrow="Admin"
description="Receipts are scanned on-device by default. Optionally configure an AI provider for higher-accuracy parsing on tricky receipts."
variant="gradient"
/>
{embedded ? null : (
<PageHeader
title="Receipt OCR"
eyebrow="Admin"
description="Receipts are scanned on-device by default. Optionally configure an AI provider for higher-accuracy parsing on tricky receipts."
variant="gradient"
/>
)}
<SettingsBlock
scope="port"

View File

@@ -0,0 +1,341 @@
'use client';
import { useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Plus, Trash2, ChevronUp, ChevronDown, Save } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { apiFetch } from '@/lib/api/client';
import { toastError } from '@/lib/api/toast-error';
import { cn } from '@/lib/utils';
interface CriterionRow {
id: string;
key: string;
label: string;
description: string | null;
enabled: boolean;
displayOrder: number;
}
interface ListResponse {
data: CriterionRow[];
}
/**
* Per-port qualification-criteria admin. Lists current criteria, add via
* the dialog, toggle enabled inline, drag-style reorder via up/down buttons
* (keeps the UI simple for v1; can swap to a real DnD later if reps want it).
*/
export function QualificationCriteriaAdmin() {
const queryClient = useQueryClient();
const [createOpen, setCreateOpen] = useState(false);
const { data, isLoading } = useQuery<ListResponse>({
queryKey: ['qualification-criteria'],
queryFn: () => apiFetch('/api/v1/admin/qualification-criteria'),
});
const criteria = data?.data ?? [];
const toggleEnabled = useMutation({
mutationFn: async (vars: { id: string; enabled: boolean }) =>
apiFetch(`/api/v1/admin/qualification-criteria/${vars.id}`, {
method: 'PATCH',
body: { enabled: vars.enabled },
}),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['qualification-criteria'] }),
onError: (err) => toastError(err),
});
const reorder = useMutation({
mutationFn: async (vars: { id: string; displayOrder: number }) =>
apiFetch(`/api/v1/admin/qualification-criteria/${vars.id}`, {
method: 'PATCH',
body: { displayOrder: vars.displayOrder },
}),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['qualification-criteria'] }),
onError: (err) => toastError(err),
});
const deleteCriterion = useMutation({
mutationFn: async (id: string) =>
apiFetch(`/api/v1/admin/qualification-criteria/${id}`, { method: 'DELETE' }),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['qualification-criteria'] }),
onError: (err) => toastError(err),
});
if (isLoading) {
return <div className="text-sm text-muted-foreground">Loading criteria</div>;
}
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<p className="text-sm text-muted-foreground">
{criteria.length} criteria configured · {criteria.filter((c) => c.enabled).length} enabled
</p>
<Button size="sm" onClick={() => setCreateOpen(true)} className="gap-1.5">
<Plus className="size-4" aria-hidden />
Add criterion
</Button>
</div>
{criteria.length === 0 ? (
<div className="rounded-lg border border-dashed bg-muted/20 p-8 text-center">
<p className="text-sm font-medium">No criteria configured yet.</p>
<p className="mt-1 text-xs text-muted-foreground">
Add the first criterion the rep needs to confirm before a deal can be qualified.
</p>
</div>
) : (
<ul className="divide-y divide-border rounded-lg border">
{criteria.map((c, idx) => {
const isFirst = idx === 0;
const isLast = idx === criteria.length - 1;
return (
<li key={c.id} className="flex items-start gap-3 p-3">
<div className="flex flex-col gap-0.5">
<button
type="button"
aria-label="Move up"
disabled={isFirst || reorder.isPending}
onClick={() =>
reorder.mutate({ id: c.id, displayOrder: Math.max(0, c.displayOrder - 1) })
}
className={cn(
'rounded p-0.5 text-muted-foreground hover:text-foreground disabled:opacity-30',
)}
>
<ChevronUp className="size-3.5" aria-hidden />
</button>
<button
type="button"
aria-label="Move down"
disabled={isLast || reorder.isPending}
onClick={() => reorder.mutate({ id: c.id, displayOrder: c.displayOrder + 1 })}
className={cn(
'rounded p-0.5 text-muted-foreground hover:text-foreground disabled:opacity-30',
)}
>
<ChevronDown className="size-3.5" aria-hidden />
</button>
</div>
<CriterionEditableRow
criterion={c}
onToggleEnabled={(enabled) => toggleEnabled.mutate({ id: c.id, enabled })}
/>
<button
type="button"
aria-label="Delete criterion"
disabled={deleteCriterion.isPending}
onClick={() => {
if (
confirm(
`Delete criterion "${c.label}"? Per-interest state rows for this key will become orphaned (hidden from the UI but kept in audit history).`,
)
) {
deleteCriterion.mutate(c.id);
}
}}
className="ml-auto rounded p-1.5 text-muted-foreground hover:bg-destructive/10 hover:text-destructive"
>
<Trash2 className="size-4" aria-hidden />
</button>
</li>
);
})}
</ul>
)}
<CreateCriterionDialog open={createOpen} onOpenChange={setCreateOpen} />
</div>
);
}
function CriterionEditableRow({
criterion,
onToggleEnabled,
}: {
criterion: CriterionRow;
onToggleEnabled: (enabled: boolean) => void;
}) {
const queryClient = useQueryClient();
const [label, setLabel] = useState(criterion.label);
const [description, setDescription] = useState(criterion.description ?? '');
const isDirty =
label.trim() !== criterion.label || (description.trim() || null) !== criterion.description;
const save = useMutation({
mutationFn: async () =>
apiFetch(`/api/v1/admin/qualification-criteria/${criterion.id}`, {
method: 'PATCH',
body: { label: label.trim(), description: description.trim() || null },
}),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['qualification-criteria'] }),
onError: (err) => toastError(err),
});
return (
<div className="flex-1 space-y-1.5">
<div className="flex items-center gap-2">
<Input
value={label}
onChange={(e) => setLabel(e.target.value)}
className="h-7 max-w-md text-sm font-medium"
/>
<code className="rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">
{criterion.key}
</code>
<div className="ml-auto flex items-center gap-2">
{isDirty ? (
<Button
size="sm"
variant="outline"
className="h-7 px-2 text-xs"
disabled={save.isPending || label.trim().length === 0}
onClick={() => save.mutate()}
>
<Save className="size-3" aria-hidden />
Save
</Button>
) : null}
<Switch
checked={criterion.enabled}
onCheckedChange={onToggleEnabled}
aria-label={criterion.enabled ? 'Disable' : 'Enable'}
/>
</div>
</div>
<Textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
rows={2}
placeholder="Optional helper text shown under the checkbox on the interest detail page."
className="text-xs"
/>
</div>
);
}
function CreateCriterionDialog({
open,
onOpenChange,
}: {
open: boolean;
onOpenChange: (v: boolean) => void;
}) {
const queryClient = useQueryClient();
const [key, setKey] = useState('');
const [label, setLabel] = useState('');
const [description, setDescription] = useState('');
const [enabled, setEnabled] = useState(true);
const mutation = useMutation({
mutationFn: async () =>
apiFetch('/api/v1/admin/qualification-criteria', {
method: 'POST',
body: {
key: key.trim(),
label: label.trim(),
description: description.trim() || null,
enabled,
displayOrder: 999,
},
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['qualification-criteria'] });
onOpenChange(false);
setKey('');
setLabel('');
setDescription('');
setEnabled(true);
},
onError: (err) => toastError(err),
});
const canSubmit =
key.trim().length > 0 &&
/^[a-z][a-z0-9_]*$/.test(key.trim()) &&
label.trim().length > 0 &&
!mutation.isPending;
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Add qualification criterion</DialogTitle>
<DialogDescription>
The <strong>key</strong> is a stable identifier code references (lowercase alphanumeric
+ underscores). It can&apos;t be changed once created per-interest state rows
reference it.
</DialogDescription>
</DialogHeader>
<div className="space-y-3 py-1">
<div className="space-y-1">
<Label htmlFor="qc-key">Key</Label>
<Input
id="qc-key"
value={key}
onChange={(e) => setKey(e.target.value.toLowerCase())}
placeholder="e.g. budget_confirmed"
/>
{key && !/^[a-z][a-z0-9_]*$/.test(key) ? (
<p className="text-[11px] text-rose-700">
Must start with a letter; lowercase alphanumeric and underscores only.
</p>
) : null}
</div>
<div className="space-y-1">
<Label htmlFor="qc-label">Label</Label>
<Input
id="qc-label"
value={label}
onChange={(e) => setLabel(e.target.value)}
placeholder="e.g. Budget confirmed"
/>
</div>
<div className="space-y-1">
<Label htmlFor="qc-desc">Description (optional)</Label>
<Textarea
id="qc-desc"
value={description}
onChange={(e) => setDescription(e.target.value)}
rows={2}
placeholder="Shown under the checkbox on the interest detail page."
/>
</div>
<label className="flex items-center gap-2 text-sm">
<Switch checked={enabled} onCheckedChange={setEnabled} />
Enabled by default
</label>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => onOpenChange(false)}
disabled={mutation.isPending}
>
Cancel
</Button>
<Button disabled={!canSubmit} onClick={() => mutation.mutate()}>
{mutation.isPending ? 'Adding…' : 'Add criterion'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}

View File

@@ -74,15 +74,13 @@ const KNOWN_SETTINGS: Array<{
description: 'Probability weights for revenue forecast by pipeline stage (JSON)',
type: 'json',
defaultValue: {
open: 0.05,
details_sent: 0.1,
in_communication: 0.2,
eoi_sent: 0.4,
eoi_signed: 0.6,
deposit_10pct: 0.75,
contract_sent: 0.85,
contract_signed: 0.95,
completed: 1.0,
enquiry: 0.05,
qualified: 0.15,
nurturing: 0.15,
eoi: 0.4,
reservation: 0.7,
deposit_paid: 0.85,
contract: 0.95,
},
},
{
@@ -92,6 +90,14 @@ const KNOWN_SETTINGS: Array<{
type: 'json',
defaultValue: [],
},
{
key: 'default_new_interest_owner',
label: 'Default New-Interest Owner',
description:
'User ID to auto-assign as the deal owner when a new interest is created. Stored as { "userId": "..." }. Leave blank to have new interests unassigned by default — the rep can pick an owner from the interest detail header.',
type: 'json',
defaultValue: { userId: null },
},
{
key: 'inquiry_contact_email',
label: 'Inquiry Contact Email',