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:
@@ -22,6 +22,8 @@ import { db } from '@/lib/db';
|
||||
import { interestBerths, type InterestBerth } from '@/lib/db/schema/interests';
|
||||
import { berths } from '@/lib/db/schema/berths';
|
||||
|
||||
type DbOrTx = typeof db | Parameters<Parameters<typeof db.transaction>[0]>[0];
|
||||
|
||||
// ─── Reads ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface PrimaryBerthRef {
|
||||
@@ -156,40 +158,54 @@ export async function upsertInterestBerth(
|
||||
opts: AddOrUpdateOpts = {},
|
||||
): Promise<InterestBerth> {
|
||||
return db.transaction(async (tx) => {
|
||||
if (opts.isPrimary === true) {
|
||||
await tx
|
||||
.update(interestBerths)
|
||||
.set({ isPrimary: false })
|
||||
.where(and(eq(interestBerths.interestId, interestId), eq(interestBerths.isPrimary, true)));
|
||||
}
|
||||
const setForUpdate: Partial<InterestBerth> = {};
|
||||
if (opts.isPrimary !== undefined) setForUpdate.isPrimary = opts.isPrimary;
|
||||
if (opts.isSpecificInterest !== undefined)
|
||||
setForUpdate.isSpecificInterest = opts.isSpecificInterest;
|
||||
if (opts.isInEoiBundle !== undefined) setForUpdate.isInEoiBundle = opts.isInEoiBundle;
|
||||
if (opts.addedBy !== undefined) setForUpdate.addedBy = opts.addedBy;
|
||||
if (opts.notes !== undefined) setForUpdate.notes = opts.notes;
|
||||
|
||||
const [row] = await tx
|
||||
.insert(interestBerths)
|
||||
.values({
|
||||
interestId,
|
||||
berthId,
|
||||
isPrimary: opts.isPrimary ?? false,
|
||||
isSpecificInterest: opts.isSpecificInterest ?? true,
|
||||
isInEoiBundle: opts.isInEoiBundle ?? false,
|
||||
addedBy: opts.addedBy,
|
||||
notes: opts.notes,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [interestBerths.interestId, interestBerths.berthId],
|
||||
set: setForUpdate,
|
||||
})
|
||||
.returning();
|
||||
return row!;
|
||||
return upsertInterestBerthTx(tx, interestId, berthId, opts);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transaction-bound variant of {@link upsertInterestBerth}. Use this when the
|
||||
* junction write must roll back together with another write (e.g. inserting
|
||||
* the parent interest row in the same transaction).
|
||||
*/
|
||||
export async function upsertInterestBerthTx(
|
||||
tx: DbOrTx,
|
||||
interestId: string,
|
||||
berthId: string,
|
||||
opts: AddOrUpdateOpts = {},
|
||||
): Promise<InterestBerth> {
|
||||
if (opts.isPrimary === true) {
|
||||
await tx
|
||||
.update(interestBerths)
|
||||
.set({ isPrimary: false })
|
||||
.where(and(eq(interestBerths.interestId, interestId), eq(interestBerths.isPrimary, true)));
|
||||
}
|
||||
const setForUpdate: Partial<InterestBerth> = {};
|
||||
if (opts.isPrimary !== undefined) setForUpdate.isPrimary = opts.isPrimary;
|
||||
if (opts.isSpecificInterest !== undefined)
|
||||
setForUpdate.isSpecificInterest = opts.isSpecificInterest;
|
||||
if (opts.isInEoiBundle !== undefined) setForUpdate.isInEoiBundle = opts.isInEoiBundle;
|
||||
if (opts.addedBy !== undefined) setForUpdate.addedBy = opts.addedBy;
|
||||
if (opts.notes !== undefined) setForUpdate.notes = opts.notes;
|
||||
|
||||
const [row] = await tx
|
||||
.insert(interestBerths)
|
||||
.values({
|
||||
interestId,
|
||||
berthId,
|
||||
isPrimary: opts.isPrimary ?? false,
|
||||
isSpecificInterest: opts.isSpecificInterest ?? true,
|
||||
isInEoiBundle: opts.isInEoiBundle ?? false,
|
||||
addedBy: opts.addedBy,
|
||||
notes: opts.notes,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [interestBerths.interestId, interestBerths.berthId],
|
||||
set: setForUpdate,
|
||||
})
|
||||
.returning();
|
||||
return row!;
|
||||
}
|
||||
|
||||
/** Promote a single berth to primary for the interest. Demotes any prior primary. */
|
||||
export async function setPrimaryBerth(interestId: string, berthId: string): Promise<void> {
|
||||
await upsertInterestBerth(interestId, berthId, { isPrimary: true });
|
||||
|
||||
Reference in New Issue
Block a user