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>
This commit is contained in:
Matt Ciaccio
2026-05-02 00:01:33 +02:00
parent 886119cbde
commit ba5fb6db5e
21 changed files with 10995 additions and 112 deletions

View File

@@ -1,8 +1,10 @@
'use client';
import Link from 'next/link';
import { useParams } from 'next/navigation';
import { format, formatDistanceToNowStrict } from 'date-fns';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { CheckCircle2, Circle, FileSignature, Send, Wallet } from 'lucide-react';
import { CheckCircle2, Circle, FileSignature, Plus, Send, Wallet } from 'lucide-react';
import type { DetailTab } from '@/components/shared/detail-layout';
import { Button } from '@/components/ui/button';
@@ -122,6 +124,8 @@ interface MilestoneSectionProps {
status: string | null;
onAdvance: (stage: string) => void;
isPending: boolean;
/** Extra nodes (e.g. "Create deposit invoice" link) rendered below the steps. */
footer?: React.ReactNode;
}
/**
@@ -139,6 +143,7 @@ function MilestoneSection({
status,
onAdvance,
isPending,
footer,
}: MilestoneSectionProps) {
const firstUnsetIdx = steps.findIndex((s) => !s.date);
@@ -209,6 +214,7 @@ function MilestoneSection({
);
})}
</ol>
{footer ? <div className="mt-3 border-t pt-3 text-xs">{footer}</div> : null}
</section>
);
}
@@ -220,6 +226,8 @@ function OverviewTab({
interestId: string;
interest: InterestTabsOptions['interest'];
}) {
const params = useParams<{ portSlug: string }>();
const portSlug = params?.portSlug ?? '';
const mutation = useInterestPatch(interestId);
const stageMutation = useStageMutation(interestId);
const save = (field: InterestPatchField) => async (next: string | null) => {
@@ -271,6 +279,17 @@ function OverviewTab({
actionLabel: 'Mark deposit received',
},
]}
footer={
!interest.dateDepositReceived ? (
<Link
href={`/${portSlug}/invoices/new?interestId=${interestId}&kind=deposit`}
className="inline-flex items-center gap-1.5 text-foreground/80 hover:text-foreground"
>
<Plus className="size-3.5" />
Create deposit invoice
</Link>
) : null
}
/>
<MilestoneSection
title="Contract"