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,7 +3,10 @@ import { beforeAll, describe, expect, it } from 'vitest';
|
||||
import { db } from '@/lib/db';
|
||||
import { documentTemplates } from '@/lib/db/schema/documents';
|
||||
import { clientAddresses, clientContacts, clients as clientsTable } from '@/lib/db/schema/clients';
|
||||
import { interests as interestsTable } from '@/lib/db/schema/interests';
|
||||
import {
|
||||
interests as interestsTable,
|
||||
interestBerths as interestBerthsTable,
|
||||
} from '@/lib/db/schema/interests';
|
||||
import { getMergeFields, resolveTemplate } from '@/lib/services/document-templates';
|
||||
|
||||
import { makeBerth, makeClient, makeCompany, makePort, makeYacht } from '../helpers/factories';
|
||||
@@ -44,12 +47,22 @@ async function insertInterest(args: {
|
||||
portId: args.portId,
|
||||
clientId: args.clientId,
|
||||
yachtId: args.yachtId ?? null,
|
||||
berthId: args.berthId ?? null,
|
||||
pipelineStage: args.pipelineStage ?? 'open',
|
||||
leadCategory: args.leadCategory ?? null,
|
||||
notes: args.notes ?? null,
|
||||
})
|
||||
.returning();
|
||||
// Plan §3.4: legacy interest.berth_id was replaced by the
|
||||
// interest_berths junction. Tests that pass berthId now materialise a
|
||||
// primary junction row alongside the interest.
|
||||
if (args.berthId) {
|
||||
await db.insert(interestBerthsTable).values({
|
||||
interestId: row!.id,
|
||||
berthId: args.berthId,
|
||||
isPrimary: true,
|
||||
isSpecificInterest: true,
|
||||
});
|
||||
}
|
||||
return row!;
|
||||
}
|
||||
|
||||
@@ -299,10 +312,15 @@ describe('resolveTemplate — company-owned yacht', () => {
|
||||
portId: port.id,
|
||||
clientId: client.id,
|
||||
yachtId: yacht.id,
|
||||
berthId: berth.id,
|
||||
pipelineStage: 'open',
|
||||
})
|
||||
.returning();
|
||||
await db.insert(interestBerthsTable).values({
|
||||
interestId: interest!.id,
|
||||
berthId: berth.id,
|
||||
isPrimary: true,
|
||||
isSpecificInterest: true,
|
||||
});
|
||||
|
||||
const [tmpl] = await db
|
||||
.insert(documentTemplates)
|
||||
@@ -385,11 +403,16 @@ describe('resolveTemplate — legacy fallback (no interestId)', () => {
|
||||
portId: port.id,
|
||||
clientId: client.id,
|
||||
yachtId: null,
|
||||
berthId: berth.id,
|
||||
pipelineStage: 'open',
|
||||
leadCategory: 'casual',
|
||||
})
|
||||
.returning();
|
||||
await db.insert(interestBerthsTable).values({
|
||||
interestId: interest!.id,
|
||||
berthId: berth.id,
|
||||
isPrimary: true,
|
||||
isSpecificInterest: true,
|
||||
});
|
||||
|
||||
const [tmpl] = await db
|
||||
.insert(documentTemplates)
|
||||
|
||||
Reference in New Issue
Block a user