refactor(interests): migrate callers to interest_berths junction + drop berth_id
Phase 2b of the berth-recommender refactor (plan §3.4). Every caller of the legacy `interests.berth_id` column now reads / writes through the `interest_berths` junction via the helper service introduced in Phase 2a; the column itself is dropped in a final migration. Service-layer changes - interests.service: filter `?berthId=X` becomes EXISTS-against-junction; list enrichment uses `getPrimaryBerthsForInterests`; create/update/ linkBerth/unlinkBerth all dispatch through the junction helpers, with createInterest's row insert + junction write sharing a single transaction. - clients / dashboard / report-generators / search: leftJoin chains pivot through `interest_berths` filtered by `is_primary=true`. - eoi-context / document-templates / berth-rules-engine / portal / record-export / queue worker: read primary via `getPrimaryBerth(...)`. - interest-scoring: berthLinked is now derived from any junction row count. - dedup/migration-apply + public interest route: write a primary junction row alongside the interest insert when a berth is provided. API contract preserved: list/detail responses still emit `berthId` and `berthMooringNumber`, derived from the primary junction row, so frontend consumers (interest-form, interest-detail-header) need no changes. Schema + migration - Drop `interestsRelations.berth` and `idx_interests_berth`. - Replace `berthsRelations.interests` with `interestBerths`. - Migration 0029_puzzling_romulus drops `interests.berth_id` + the index. - Tests that previously inserted `interests.berthId` now seed a primary junction row alongside the interest. Verified: vitest 995 passing (1 unrelated pre-existing flake in maintenance-cleanup.test.ts), tsc clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { and, eq, count, inArray, isNull, desc } from 'drizzle-orm';
|
||||
import { db } from '@/lib/db';
|
||||
import { clients } from '@/lib/db/schema/clients';
|
||||
import { interests } from '@/lib/db/schema/interests';
|
||||
import { getPrimaryBerthsForInterests } from '@/lib/services/interest-berths.service';
|
||||
import { documents, files } from '@/lib/db/schema/documents';
|
||||
import { invoices } from '@/lib/db/schema/financial';
|
||||
import { berths } from '@/lib/db/schema/berths';
|
||||
@@ -123,7 +124,6 @@ export async function getClientInterests(
|
||||
id: interests.id,
|
||||
pipelineStage: interests.pipelineStage,
|
||||
leadCategory: interests.leadCategory,
|
||||
berthId: interests.berthId,
|
||||
eoiStatus: interests.eoiStatus,
|
||||
contractStatus: interests.contractStatus,
|
||||
dateFirstContact: interests.dateFirstContact,
|
||||
@@ -133,31 +133,39 @@ export async function getClientInterests(
|
||||
.where(and(eq(interests.clientId, clientId), eq(interests.portId, portId)))
|
||||
.orderBy(interests.createdAt);
|
||||
|
||||
// Fetch berth details for interests that have a berth
|
||||
const berthIds = rows.flatMap((r) => (r.berthId ? [r.berthId] : []));
|
||||
const berthMap = new Map<string, { mooringNumber: string; area: string | null }>();
|
||||
// Resolve each interest's primary berth via the junction (plan §3.4) -
|
||||
// single round-trip for the whole list.
|
||||
const primaryBerthMap = await getPrimaryBerthsForInterests(rows.map((r) => r.id));
|
||||
const primaryBerthIds = Array.from(
|
||||
new Set(Array.from(primaryBerthMap.values(), (b) => b.berthId)),
|
||||
);
|
||||
|
||||
if (berthIds.length > 0) {
|
||||
const berthMap = new Map<string, { mooringNumber: string; area: string | null }>();
|
||||
if (primaryBerthIds.length > 0) {
|
||||
const berthRows = await db
|
||||
.select({ id: berths.id, mooringNumber: berths.mooringNumber, area: berths.area })
|
||||
.from(berths)
|
||||
.where(eq(berths.portId, portId));
|
||||
.where(and(eq(berths.portId, portId), inArray(berths.id, primaryBerthIds)));
|
||||
for (const b of berthRows) {
|
||||
berthMap.set(b.id, { mooringNumber: b.mooringNumber, area: b.area });
|
||||
}
|
||||
}
|
||||
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
pipelineStage: r.pipelineStage,
|
||||
leadCategory: r.leadCategory,
|
||||
berthMooringNumber: r.berthId ? (berthMap.get(r.berthId)?.mooringNumber ?? null) : null,
|
||||
berthArea: r.berthId ? (berthMap.get(r.berthId)?.area ?? null) : null,
|
||||
eoiStatus: r.eoiStatus,
|
||||
contractStatus: r.contractStatus,
|
||||
dateFirstContact: r.dateFirstContact,
|
||||
createdAt: r.createdAt,
|
||||
}));
|
||||
return rows.map((r) => {
|
||||
const primary = primaryBerthMap.get(r.id);
|
||||
const berthMeta = primary ? (berthMap.get(primary.berthId) ?? null) : null;
|
||||
return {
|
||||
id: r.id,
|
||||
pipelineStage: r.pipelineStage,
|
||||
leadCategory: r.leadCategory,
|
||||
berthMooringNumber: berthMeta?.mooringNumber ?? null,
|
||||
berthArea: berthMeta?.area ?? null,
|
||||
eoiStatus: r.eoiStatus,
|
||||
contractStatus: r.contractStatus,
|
||||
dateFirstContact: r.dateFirstContact,
|
||||
createdAt: r.createdAt,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Documents ────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user