36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
|
|
import { and, eq, isNull, type SQL } from 'drizzle-orm';
|
||
|
|
|
||
|
|
import { interests } from '@/lib/db/schema/interests';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Canonical "active interest" predicate for SQL WHERE clauses.
|
||
|
|
*
|
||
|
|
* An interest is **active** when it is:
|
||
|
|
* - scoped to the given `portId`,
|
||
|
|
* - not soft-archived (`archived_at IS NULL`), and
|
||
|
|
* - not yet terminal (`outcome IS NULL` — i.e. not won, lost, or
|
||
|
|
* cancelled).
|
||
|
|
*
|
||
|
|
* "Won" deals are explicitly **not active** under this definition: a won
|
||
|
|
* deal is closed business, distinct from in-flight pipeline. The 7-stage
|
||
|
|
* pipeline carries deals through `contract` as the final motion stage;
|
||
|
|
* setting an `outcome` (won/lost/cancelled) terminates the deal and
|
||
|
|
* removes it from the active pool everywhere KPIs, kanban, hot deals,
|
||
|
|
* pipeline value, and revenue forecast are computed.
|
||
|
|
*
|
||
|
|
* Pre-2026-05-14 the dashboard used a broader `outcome IS NULL OR
|
||
|
|
* outcome = 'won'` predicate; the new pipeline overhaul collapsed
|
||
|
|
* intermediate completion states into explicit stages, making the
|
||
|
|
* stricter definition the correct one across every surface.
|
||
|
|
*
|
||
|
|
* Always combine with the table's own filters; this helper is a leaf
|
||
|
|
* predicate, not a complete WHERE.
|
||
|
|
*/
|
||
|
|
export function activeInterestsWhere(portId: string): SQL {
|
||
|
|
return and(
|
||
|
|
eq(interests.portId, portId),
|
||
|
|
isNull(interests.archivedAt),
|
||
|
|
isNull(interests.outcome),
|
||
|
|
) as SQL;
|
||
|
|
}
|