Files
pn-new-crm/src/components/interests/interest-outcome-dialog.tsx

157 lines
4.7 KiB
TypeScript
Raw Normal View History

feat(sales): EOI queue route + invoice→deposit auto-advance + won/lost outcomes Three independent strengthenings of the sales spine that the prior coherence sweep made it possible to do cleanly. 1. EOI queue page - Sidebar entry under Documents → "EOI queue". - Route /[port]/documents/eoi renders DocumentsHub with the existing eoi_queue tab pre-selected (filters in-flight EOIs only). - .gitignore: tightened root-only `eoi/` ignore so the documents/eoi route is no longer silently excluded. 2. Invoice ↔ deposit link - invoices.interestId (FK, ON DELETE SET NULL) + invoices.kind ('general' | 'deposit'). Indexed on (port_id, interest_id). - createInvoiceSchema requires interestId when kind === 'deposit'; the service validates the linked interest belongs to the same port before insert. - recordPayment auto-advances pipelineStage to deposit_10pct (via advanceStageIfBehind) when a paid invoice is kind=deposit and has an interestId. No-op if the interest is already further along. - "Create deposit invoice" link added to the Deposit milestone on the interest detail. Links to /invoices/new?interestId=…&kind=deposit; the form prefills the billing entity from the linked interest's client and shows a context banner. 3. Won / lost terminal outcomes - interests.outcome ('won' | 'lost_other_marina' | 'lost_unqualified' | 'lost_no_response' | 'cancelled') + outcomeReason text + outcomeAt timestamp. Indexed on (port_id, outcome). - setInterestOutcome / clearInterestOutcome services + POST/DELETE /api/v1/interests/:id/outcome endpoints (gated by change_stage permission). Setting an outcome moves the interest to `completed` in the same write; clearing reopens to `in_communication` (or a caller-specified stage). - Mark Won / Mark Lost icon buttons on the interest detail header, plus an outcome badge that replaces the stage pill once a terminal outcome is set, plus a Reopen button. - Funnel + dashboard math updated to exclude lost/cancelled outcomes from active calculations (KPIs.activeInterests, pipelineValueUsd, getPipelineCounts, computePipelineFunnel, getRevenueForecast). The funnel now also returns a `lost` summary so callers can surface leakage without polluting conversion percentages. Schema changes shipped via 0019_lazy_vampiro.sql; applied to dev DB manually via psql because drizzle-kit push hits a pre-existing zod parsing issue on the companies index. Dev server may need a restart to flush prepared-statement caches. tsc clean. vitest 832/832 pass. ESLint clean on every file touched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 00:01:33 +02:00
'use client';
import { useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { Loader2, Trophy, XCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { apiFetch } from '@/lib/api/client';
import { type InterestOutcome } from '@/lib/validators/interests';
const OUTCOME_LABELS: Record<InterestOutcome, string> = {
won: 'Won',
lost_other_marina: 'Lost — went to another marina',
lost_unqualified: 'Lost — unqualified',
lost_no_response: 'Lost — no response',
cancelled: 'Cancelled',
};
const LOST_OUTCOMES: InterestOutcome[] = [
'lost_other_marina',
'lost_unqualified',
'lost_no_response',
'cancelled',
];
interface Props {
interestId: string;
open: boolean;
onOpenChange: (open: boolean) => void;
/** Determines which outcomes are offered. 'won' opens with just the Won option preselected. */
mode: 'won' | 'lost';
}
export function InterestOutcomeDialog({ interestId, open, onOpenChange, mode }: Props) {
const queryClient = useQueryClient();
const choices: InterestOutcome[] = mode === 'won' ? ['won'] : LOST_OUTCOMES;
const [outcome, setOutcome] = useState<InterestOutcome>(choices[0]!);
const [reason, setReason] = useState('');
const mutation = useMutation({
mutationFn: () =>
apiFetch(`/api/v1/interests/${interestId}/outcome`, {
method: 'POST',
body: { outcome, reason: reason || undefined },
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['interests', interestId] });
queryClient.invalidateQueries({ queryKey: ['interests'] });
onOpenChange(false);
setReason('');
},
});
function handleOpenChange(next: boolean) {
if (!next) {
setReason('');
setOutcome(choices[0]!);
}
onOpenChange(next);
}
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
{mode === 'won' ? (
<Trophy className="h-4 w-4 text-emerald-600" />
) : (
<XCircle className="h-4 w-4 text-rose-600" />
)}
{mode === 'won' ? 'Mark interest as won' : 'Close interest as lost'}
</DialogTitle>
</DialogHeader>
<div className="space-y-4 py-2">
{mode === 'lost' ? (
<div className="space-y-1">
<Label>Reason</Label>
<Select value={outcome} onValueChange={(v) => setOutcome(v as InterestOutcome)}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
{LOST_OUTCOMES.map((o) => (
<SelectItem key={o} value={o}>
{OUTCOME_LABELS[o]}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
) : null}
<div className="space-y-1">
<Label htmlFor="outcome-reason">Notes (optional)</Label>
<Textarea
id="outcome-reason"
value={reason}
onChange={(e) => setReason(e.target.value)}
placeholder={
mode === 'won'
? 'Anything notable about the win? (visible in timeline + reports)'
: 'What happened? (visible in timeline + reports)'
}
rows={3}
/>
</div>
<p className="text-xs text-muted-foreground">
This will move the interest to <strong>Completed</strong> and stamp the outcome. You can
reopen it later.
</p>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => handleOpenChange(false)}
disabled={mutation.isPending}
>
Cancel
</Button>
<Button
onClick={() => mutation.mutate()}
disabled={mutation.isPending}
className={
mode === 'won'
? 'bg-emerald-600 hover:bg-emerald-700'
: 'bg-rose-600 hover:bg-rose-700'
}
>
{mutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{mode === 'won' ? 'Mark as won' : 'Close as lost'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}