feat(yachts): atomic transferOwnership with partial-unique guard

This commit is contained in:
Matt Ciaccio
2026-04-23 23:58:20 +02:00
parent d0ab4b8102
commit 8a5cd1ef0e
3 changed files with 159 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import { and, eq } from 'drizzle-orm';
import { and, eq, sql } from 'drizzle-orm';
import { db } from '@/lib/db';
import { yachts, yachtOwnershipHistory, clients } from '@/lib/db/schema';
import { companies } from '@/lib/db/schema/companies';
@@ -8,7 +8,11 @@ import { emitToRoom } from '@/lib/socket/server';
import { diffEntity } from '@/lib/entity-diff';
import { withTransaction } from '@/lib/db/utils';
import type { z } from 'zod';
import type { createYachtSchema, UpdateYachtInput } from '@/lib/validators/yachts';
import type {
createYachtSchema,
UpdateYachtInput,
TransferOwnershipInput,
} from '@/lib/validators/yachts';
type CreateYachtInput = z.input<typeof createYachtSchema>;
@@ -184,3 +188,78 @@ export async function archiveYacht(id: string, portId: string, meta: AuditMeta)
emitToRoom(`port:${portId}`, 'yacht:archived', { yachtId: id });
}
export async function transferOwnership(
yachtId: string,
portId: string,
data: TransferOwnershipInput,
meta: AuditMeta,
) {
return await withTransaction(async (tx) => {
const yacht = await tx.query.yachts.findFirst({
where: and(eq(yachts.id, yachtId), eq(yachts.portId, portId)),
});
if (!yacht) throw new NotFoundError('Yacht');
if (
yacht.currentOwnerType === data.newOwner.type &&
yacht.currentOwnerId === data.newOwner.id
) {
throw new ValidationError('same owner — nothing to transfer');
}
await assertOwnerExists(portId, data.newOwner, tx);
// Close the currently-active history row
await tx
.update(yachtOwnershipHistory)
.set({ endDate: data.effectiveDate })
.where(
and(
eq(yachtOwnershipHistory.yachtId, yachtId),
sql`${yachtOwnershipHistory.endDate} IS NULL`,
),
);
// Open new row
await tx.insert(yachtOwnershipHistory).values({
yachtId,
ownerType: data.newOwner.type,
ownerId: data.newOwner.id,
startDate: data.effectiveDate,
endDate: null,
transferReason: data.transferReason ?? null,
transferNotes: data.transferNotes ?? null,
createdBy: meta.userId,
});
// Update denormalized current-owner columns
const [updated] = await tx
.update(yachts)
.set({
currentOwnerType: data.newOwner.type,
currentOwnerId: data.newOwner.id,
updatedAt: new Date(),
})
.where(eq(yachts.id, yachtId))
.returning();
void createAuditLog({
userId: meta.userId,
portId,
action: 'update',
entityType: 'yacht',
entityId: yachtId,
newValue: { ownerTransferTo: data.newOwner, reason: data.transferReason },
ipAddress: meta.ipAddress,
userAgent: meta.userAgent,
});
emitToRoom(`port:${portId}`, 'yacht:ownership_transferred', {
yachtId,
newOwner: data.newOwner,
});
return updated!;
});
}

View File

@@ -81,6 +81,10 @@ export interface ServerToClientEvents {
'yacht:created': (payload: { yachtId: string }) => void;
'yacht:updated': (payload: { yachtId: string; changedFields: string[] }) => void;
'yacht:archived': (payload: { yachtId: string }) => void;
'yacht:ownership_transferred': (payload: {
yachtId: string;
newOwner: { type: 'client' | 'company'; id: string };
}) => void;
// Document events
'document:created': (payload: { documentId: string; type?: string; interestId?: string }) => void;