feat(factories): add makeMembership, makeReservation, makeOwnershipTransfer
This commit is contained in:
@@ -10,12 +10,19 @@
|
||||
* — return in-memory objects suitable for unit tests with mocked `db`.
|
||||
*/
|
||||
|
||||
import { and, eq, sql } from 'drizzle-orm';
|
||||
import { db } from '@/lib/db';
|
||||
import { ports, type NewPort, type Port } from '@/lib/db/schema/ports';
|
||||
import { clients, type NewClient, type Client } from '@/lib/db/schema/clients';
|
||||
import { berths, type NewBerth, type Berth } from '@/lib/db/schema/berths';
|
||||
import { yachts, yachtOwnershipHistory, type NewYacht, type Yacht } from '@/lib/db/schema/yachts';
|
||||
import { companies, type NewCompany, type Company } from '@/lib/db/schema/companies';
|
||||
import {
|
||||
companies,
|
||||
companyMemberships,
|
||||
type NewCompany,
|
||||
type Company,
|
||||
} from '@/lib/db/schema/companies';
|
||||
import { berthReservations, type BerthReservation } from '@/lib/db/schema/reservations';
|
||||
|
||||
// ─── Port ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -119,6 +126,125 @@ export async function makeCompany(args: {
|
||||
return company!;
|
||||
}
|
||||
|
||||
// ─── Company Membership ──────────────────────────────────────────────────────
|
||||
|
||||
export async function makeMembership(args: {
|
||||
companyId: string;
|
||||
clientId: string;
|
||||
role?: string;
|
||||
roleDetail?: string;
|
||||
startDate?: Date;
|
||||
endDate?: Date | null;
|
||||
isPrimary?: boolean;
|
||||
notes?: string;
|
||||
}) {
|
||||
const [row] = await db
|
||||
.insert(companyMemberships)
|
||||
.values({
|
||||
companyId: args.companyId,
|
||||
clientId: args.clientId,
|
||||
role: args.role ?? 'director',
|
||||
roleDetail: args.roleDetail ?? null,
|
||||
startDate: args.startDate ?? new Date(),
|
||||
endDate: args.endDate ?? null,
|
||||
isPrimary: args.isPrimary ?? false,
|
||||
notes: args.notes ?? null,
|
||||
})
|
||||
.returning();
|
||||
if (!row) throw new Error('Failed to create membership');
|
||||
return row;
|
||||
}
|
||||
|
||||
// ─── Berth Reservation ───────────────────────────────────────────────────────
|
||||
|
||||
export async function makeReservation(args: {
|
||||
berthId: string;
|
||||
portId: string;
|
||||
clientId: string;
|
||||
yachtId: string;
|
||||
status: 'pending' | 'active' | 'ended' | 'cancelled';
|
||||
startDate?: Date;
|
||||
endDate?: Date | null;
|
||||
tenureType?: 'permanent' | 'fixed_term' | 'seasonal';
|
||||
interestId?: string;
|
||||
createdBy?: string;
|
||||
notes?: string;
|
||||
}): Promise<BerthReservation> {
|
||||
const [row] = await db
|
||||
.insert(berthReservations)
|
||||
.values({
|
||||
berthId: args.berthId,
|
||||
portId: args.portId,
|
||||
clientId: args.clientId,
|
||||
yachtId: args.yachtId,
|
||||
interestId: args.interestId ?? null,
|
||||
status: args.status,
|
||||
startDate: args.startDate ?? new Date(),
|
||||
endDate: args.endDate ?? null,
|
||||
tenureType: args.tenureType ?? 'permanent',
|
||||
contractFileId: null,
|
||||
notes: args.notes ?? null,
|
||||
createdBy: args.createdBy ?? 'seed-user',
|
||||
})
|
||||
.returning();
|
||||
if (!row) throw new Error('Failed to create reservation');
|
||||
return row;
|
||||
}
|
||||
|
||||
// ─── Yacht Ownership Transfer ────────────────────────────────────────────────
|
||||
|
||||
export async function makeOwnershipTransfer(args: {
|
||||
yachtId: string;
|
||||
newOwner: { type: 'client' | 'company'; id: string };
|
||||
effectiveDate?: Date;
|
||||
transferReason?: string;
|
||||
transferNotes?: string;
|
||||
createdBy?: string;
|
||||
}) {
|
||||
const effective = args.effectiveDate ?? new Date();
|
||||
const createdBy = args.createdBy ?? 'seed-user';
|
||||
return await db.transaction(async (tx) => {
|
||||
// Close current open row
|
||||
await tx
|
||||
.update(yachtOwnershipHistory)
|
||||
.set({ endDate: effective })
|
||||
.where(
|
||||
and(
|
||||
eq(yachtOwnershipHistory.yachtId, args.yachtId),
|
||||
sql`${yachtOwnershipHistory.endDate} IS NULL`,
|
||||
),
|
||||
);
|
||||
|
||||
// Insert new open row
|
||||
const [newHistory] = await tx
|
||||
.insert(yachtOwnershipHistory)
|
||||
.values({
|
||||
yachtId: args.yachtId,
|
||||
ownerType: args.newOwner.type,
|
||||
ownerId: args.newOwner.id,
|
||||
startDate: effective,
|
||||
endDate: null,
|
||||
transferReason: args.transferReason ?? null,
|
||||
transferNotes: args.transferNotes ?? null,
|
||||
createdBy,
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Update yacht's denormalized current owner
|
||||
const [updated] = await tx
|
||||
.update(yachts)
|
||||
.set({
|
||||
currentOwnerType: args.newOwner.type,
|
||||
currentOwnerId: args.newOwner.id,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(yachts.id, args.yachtId))
|
||||
.returning();
|
||||
|
||||
return { history: newHistory!, yacht: updated! };
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Webhook ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface WebhookData {
|
||||
|
||||
Reference in New Issue
Block a user