Files
pn-new-crm/src/lib/services/berth-heat.service.ts
Matt 221ae5784e chore(autonomous-session): consolidate uncommitted work from prior session
Bundles the prior autonomous-session output that was sitting unstaged:

- Em-dash sweep across src/ + tests/ (en-dash/em-dash to hyphen, ~2280 instances)
- country-flag-icons rollout (CountryFlag component, replaces emoji glyphs that
  never rendered on Windows; lazy-loads the 3x2 SVG index as a single chunk
  after the per-subpath dynamic-import approach silently failed in webpack)
- Admin IA Phase 1+2: 7-domain regroup, 41 to 38 pages, /admin/berths index,
  redirects (ocr to ai, reports to dashboard, invitations to users),
  docs/admin-ia-proposal.md
- Per-template email tester (registry + endpoint + UI on Email admin page)
- Cancel-document mode picker (delete-from-Documenso vs keep-for-audit)
- Dashboard PDF report: 25 widgets, SVG charts, date-range picker, 11 resolvers
- Customize-widgets per-region sortables at xl+ (charts/rails/feed); single
  flat sortable below xl when the layout stacks; per-viewport saved orders
- Audit doc updates capturing each shipped item
- Lint fixes: react-compiler immutability in DonutChart (reduce instead of
  let-reassign), set-state-in-effect disables in CountryFlag and
  UploadForSigning preview-bytes effect, unused 'confirm' destructures in
  interest contract + reservation tabs, unescaped apostrophe in test-template
  card copy
2026-05-23 00:52:59 +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),
}));
}