Files
pn-new-crm/src/lib/services/berth-heat.service.ts
Matt 66869c9a90 feat(dashboard): berth-heat widget + investor-default surfacing
Step 6 minimal-but-functional per PRE-DEPLOY-PLAN § 1.6.

Berth Heat — new widget showing top 15 berths by active interest
count via the interest_berths junction (non-primary links included so
multi-berth deals warm every berth in their bundle). Investor-friendly
demand-pressure view; the ranked-table shape exports cleanly to PDF/
CSV. Future heatmap viz reads the same shape via /api/v1/dashboard/
berth-heat.

Defaults flipped for investor-friendliness:
- kpi_pipeline_value → defaultVisible (currency-aware headline number).
- source_conversion → defaultVisible (conversion funnel by source;
  reads the inquiry → client linkage from Step 3).
- berth_heat → defaultVisible.

Pipeline-velocity-over-time + true heatmap viz deferred. pipeline_funnel
covers snapshot stage breakdowns; over-time velocity warrants its own
design pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 15:47:49 +02:00

61 lines
2.0 KiB
TypeScript

/**
* Per-berth interest-count rankings — investor-facing analytics surface.
* For each berth in the port, returns the count of active interests
* (archived_at IS NULL AND outcome IS NULL) currently linked via the
* primary `interest_berths` row.
*
* Drives the BerthHeatWidget on the dashboard (ranked table view).
* A future heatmap-style visualization can read the same shape.
*/
import { and, count, desc, eq, isNull } from 'drizzle-orm';
import { db } from '@/lib/db';
import { berths } from '@/lib/db/schema/berths';
import { interests, interestBerths } from '@/lib/db/schema/interests';
export interface BerthHeatRow {
berthId: string;
mooringNumber: string;
area: string | null;
status: string;
/** Count of active (non-terminal, non-archived) interests linked to
* this berth via interest_berths. Treats every interest-berth link
* equally (no is_primary requirement) so a multi-berth deal warms
* every berth in its bundle. */
activeInterestCount: number;
}
export async function getBerthHeatRanking(portId: string, limit = 20): Promise<BerthHeatRow[]> {
const rows = await db
.select({
berthId: berths.id,
mooringNumber: berths.mooringNumber,
area: berths.area,
status: berths.status,
activeInterestCount: count(interests.id),
})
.from(berths)
.leftJoin(interestBerths, eq(interestBerths.berthId, berths.id))
.leftJoin(
interests,
and(
eq(interests.id, interestBerths.interestId),
eq(interests.portId, portId),
isNull(interests.archivedAt),
isNull(interests.outcome),
),
)
.where(and(eq(berths.portId, portId), isNull(berths.archivedAt)))
.groupBy(berths.id, berths.mooringNumber, berths.area, berths.status)
.orderBy(desc(count(interests.id)), berths.mooringNumber)
.limit(limit);
return rows.map((r) => ({
berthId: r.berthId,
mooringNumber: r.mooringNumber,
area: r.area,
status: r.status,
activeInterestCount: Number(r.activeInterestCount ?? 0),
}));
}