refactor(interests): drop pipelineStage='completed' sentinel convention
`outcome` is the canonical terminal-state signal. Pre-2026-05-14
`setInterestOutcome` also forced `pipelineStage='completed'` (a value
outside the 7-stage canon) which:
- broke `safeStage()` (silently coerced to 'enquiry' downstream)
- prevented analytics from answering "what stage was the deal at when
it closed?" because every closed deal looked identical
- forced belt-and-suspenders filters everywhere ('outcome=won' AND
'pipeline_stage=completed') that became redundant after migration 0062
Changes:
- `setInterestOutcome` no longer touches pipelineStage. Deal stays at
whatever stage it was on when the outcome was recorded; outcome is
the terminal signal. Audit log + websocket emit now carry
`stageAtOutcome` instead of the stale `oldStage`.
- `clearInterestOutcome` smarter reopen-stage logic: if current stage
is the legacy 'completed' sentinel (pre-existing rows from before
this commit), default to 'qualified'. Otherwise preserve the stage
the deal was at, so reopening drops the rep back where they were.
Explicit data.reopenStage still wins.
- `/api/v1/admin/dashboard-stats` route reworked: per-stage breakdown
now filters `outcome IS NULL` (only active rows count per stage);
`closedTotal` derives from a new `outcome IS NOT NULL` count query;
`completed30d` switches from `pipelineStage='completed' AND updatedAt`
to `outcome IS NOT NULL AND outcomeAt` (avoids long-closed deals
leaking into the window on unrelated edits).
- `berth-interests-tab.tsx` "active" filter switches from
`pipelineStage !== 'completed'` to `!outcome && !archivedAt` — the
legacy check stopped matching post-refactor.
- Socket event type `interest:outcomeSet` renames `oldStage` →
`stageAtOutcome` with a doc-comment explaining the semantics shift.
PIPELINE_STAGES canon is now the only valid pipeline_stage value range
for newly-set outcomes. Legacy rows still carry 'completed' until they
naturally churn through reopen + re-close, at which point they enter
the new convention.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -986,13 +986,13 @@ export async function advanceStageIfBehind(
|
||||
|
||||
// ─── Set Outcome (Won / Lost) ────────────────────────────────────────────────
|
||||
//
|
||||
// Records a terminal outcome for the interest and moves the pipelineStage to
|
||||
// `completed` so the funnel/kanban reflect the final state. The outcome
|
||||
// distinguishes won deals (they made it through) from lost variants - funnel
|
||||
// math and reports key off the `outcome` column to compute true conversion.
|
||||
//
|
||||
// Both the stage advance and the outcome write happen in one transaction so
|
||||
// the timeline doesn't end up showing one without the other.
|
||||
// Records a terminal outcome for the interest. The `outcome` column is the
|
||||
// canonical terminal-state signal; `pipelineStage` stays where it was so
|
||||
// reports can answer "what stage was this deal at when it closed?". Prior to
|
||||
// 2026-05-14 this method forced pipelineStage='completed' — a sentinel
|
||||
// outside the 7-stage canon that broke type narrowing + downstream stage
|
||||
// label lookups. Active-interest queries filter by `outcome IS NULL` so
|
||||
// the rep-facing kanban still hides closed deals.
|
||||
export async function setInterestOutcome(
|
||||
id: string,
|
||||
portId: string,
|
||||
@@ -1005,7 +1005,7 @@ export async function setInterestOutcome(
|
||||
if (!existing) throw new NotFoundError('Interest');
|
||||
|
||||
const oldOutcome = existing.outcome;
|
||||
const oldStage = existing.pipelineStage;
|
||||
const stageAtOutcome = existing.pipelineStage;
|
||||
|
||||
const now = new Date();
|
||||
await db
|
||||
@@ -1014,7 +1014,6 @@ export async function setInterestOutcome(
|
||||
outcome: data.outcome,
|
||||
outcomeReason: data.reason ?? null,
|
||||
outcomeAt: now,
|
||||
pipelineStage: 'completed',
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(and(eq(interests.id, id), eq(interests.portId, portId)));
|
||||
@@ -1025,9 +1024,9 @@ export async function setInterestOutcome(
|
||||
action: 'update',
|
||||
entityType: 'interest',
|
||||
entityId: id,
|
||||
oldValue: { outcome: oldOutcome, pipelineStage: oldStage },
|
||||
newValue: { outcome: data.outcome, pipelineStage: 'completed', reason: data.reason },
|
||||
metadata: { type: 'outcome_set' },
|
||||
oldValue: { outcome: oldOutcome, pipelineStage: stageAtOutcome },
|
||||
newValue: { outcome: data.outcome, pipelineStage: stageAtOutcome, reason: data.reason },
|
||||
metadata: { type: 'outcome_set', stageAtOutcome },
|
||||
ipAddress: meta.ipAddress,
|
||||
userAgent: meta.userAgent,
|
||||
});
|
||||
@@ -1035,7 +1034,7 @@ export async function setInterestOutcome(
|
||||
emitToRoom(`port:${portId}`, 'interest:outcomeSet', {
|
||||
interestId: id,
|
||||
outcome: data.outcome,
|
||||
oldStage,
|
||||
stageAtOutcome,
|
||||
});
|
||||
|
||||
// G-C4: fire interest_completed berth-rule for any non-null outcome
|
||||
@@ -1063,12 +1062,18 @@ export async function clearInterestOutcome(
|
||||
throw new ValidationError('Interest has no outcome to clear');
|
||||
}
|
||||
|
||||
// Default reopen stage = qualified (closest analog of the legacy
|
||||
// 'in_communication' under the 7-stage pipeline; rep can override
|
||||
// via data.reopenStage). The legacy default was silently invalid
|
||||
// post-migration 0062 — reopened interests landed in a non-canonical
|
||||
// stage that fell through safeStage() to 'enquiry'.
|
||||
const reopenStage = data.reopenStage ?? 'qualified';
|
||||
// Reopen-stage logic:
|
||||
// - If the caller passed `data.reopenStage`, honor it (rep override).
|
||||
// - Else if the current stage is the legacy 'completed' sentinel,
|
||||
// default to 'qualified' (closest analog of the pre-refactor
|
||||
// 'in_communication' which would have lived there).
|
||||
// - Else preserve the current stage — post-refactor setOutcome stops
|
||||
// touching pipelineStage, so the deal already knows where it was
|
||||
// when the rep closed it. Reopening should drop the rep back into
|
||||
// that same column on the kanban.
|
||||
const reopenStage =
|
||||
data.reopenStage ??
|
||||
(existing.pipelineStage === 'completed' ? 'qualified' : existing.pipelineStage);
|
||||
const now = new Date();
|
||||
await db
|
||||
.update(interests)
|
||||
|
||||
@@ -57,7 +57,11 @@ export interface ServerToClientEvents {
|
||||
'interest:outcomeSet': (payload: {
|
||||
interestId: string;
|
||||
outcome: string;
|
||||
oldStage: string;
|
||||
/** Stage the deal was on when the outcome was recorded. Renamed
|
||||
* from `oldStage` after the 2026-05-14 sentinel-stage cleanup —
|
||||
* the value is now the actual stage where the deal closed, not
|
||||
* a stale "what was it before we set 'completed'" marker. */
|
||||
stageAtOutcome: string;
|
||||
}) => void;
|
||||
'interest:outcomeCleared': (payload: { interestId: string }) => void;
|
||||
'interest:noteAdded': (payload: {
|
||||
|
||||
Reference in New Issue
Block a user