Files
pn-new-crm/src/lib/services/yachts.service.ts

187 lines
5.4 KiB
TypeScript
Raw Normal View History

import { and, eq } from 'drizzle-orm';
import { db } from '@/lib/db';
import { yachts, yachtOwnershipHistory, clients } from '@/lib/db/schema';
import { companies } from '@/lib/db/schema/companies';
import { createAuditLog } from '@/lib/audit';
import { NotFoundError, ValidationError } from '@/lib/errors';
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';
type CreateYachtInput = z.input<typeof createYachtSchema>;
interface AuditMeta {
userId: string;
portId: string;
ipAddress: string;
userAgent: string;
}
async function assertOwnerExists(
portId: string,
owner: { type: 'client' | 'company'; id: string },
tx: typeof db,
): Promise<void> {
if (owner.type === 'client') {
const client = await tx.query.clients.findFirst({
where: and(eq(clients.id, owner.id), eq(clients.portId, portId)),
});
if (!client) throw new ValidationError('owner not found');
} else {
const company = await tx.query.companies.findFirst({
where: and(eq(companies.id, owner.id), eq(companies.portId, portId)),
});
if (!company) throw new ValidationError('owner not found');
}
}
export async function createYacht(portId: string, data: CreateYachtInput, meta: AuditMeta) {
return await withTransaction(async (tx) => {
await assertOwnerExists(portId, data.owner, tx);
const [yacht] = await tx
.insert(yachts)
.values({
portId,
name: data.name,
hullNumber: data.hullNumber ?? null,
registration: data.registration ?? null,
flag: data.flag ?? null,
yearBuilt: data.yearBuilt ?? null,
builder: data.builder ?? null,
model: data.model ?? null,
hullMaterial: data.hullMaterial ?? null,
lengthFt: data.lengthFt ?? null,
widthFt: data.widthFt ?? null,
draftFt: data.draftFt ?? null,
lengthM: data.lengthM ?? null,
widthM: data.widthM ?? null,
draftM: data.draftM ?? null,
currentOwnerType: data.owner.type,
currentOwnerId: data.owner.id,
status: data.status ?? 'active',
notes: data.notes ?? null,
})
.returning();
await tx.insert(yachtOwnershipHistory).values({
yachtId: yacht!.id,
ownerType: data.owner.type,
ownerId: data.owner.id,
startDate: new Date(),
endDate: null,
createdBy: meta.userId,
});
void createAuditLog({
userId: meta.userId,
portId,
action: 'create',
entityType: 'yacht',
entityId: yacht!.id,
newValue: { name: yacht!.name, owner: data.owner },
ipAddress: meta.ipAddress,
userAgent: meta.userAgent,
});
emitToRoom(`port:${portId}`, 'yacht:created', { yachtId: yacht!.id });
return yacht!;
});
}
export async function getYachtById(id: string, portId: string) {
const yacht = await db.query.yachts.findFirst({
where: and(eq(yachts.id, id), eq(yachts.portId, portId)),
});
if (!yacht) throw new NotFoundError('Yacht');
return yacht;
}
export async function updateYacht(
id: string,
portId: string,
data: UpdateYachtInput,
meta: AuditMeta,
) {
// Defense-in-depth: owner changes must go through /transfer, not PATCH.
const dataRecord = data as Record<string, unknown>;
if (
Object.prototype.hasOwnProperty.call(dataRecord, 'currentOwnerType') ||
Object.prototype.hasOwnProperty.call(dataRecord, 'currentOwnerId')
) {
throw new ValidationError('use /transfer to change ownership');
}
const existing = await db.query.yachts.findFirst({
where: eq(yachts.id, id),
});
if (!existing || existing.portId !== portId) {
throw new NotFoundError('Yacht');
}
const { diff } = diffEntity(
existing as unknown as Record<string, unknown>,
data as Record<string, unknown>,
);
const [updated] = await db
.update(yachts)
.set({ ...data, updatedAt: new Date() })
.where(and(eq(yachts.id, id), eq(yachts.portId, portId)))
.returning();
void createAuditLog({
userId: meta.userId,
portId,
action: 'update',
entityType: 'yacht',
entityId: id,
oldValue: diff as Record<string, unknown>,
newValue: data as Record<string, unknown>,
ipAddress: meta.ipAddress,
userAgent: meta.userAgent,
});
emitToRoom(`port:${portId}`, 'yacht:updated', {
yachtId: id,
changedFields: Object.keys(diff),
});
return updated!;
}
export async function archiveYacht(id: string, portId: string, meta: AuditMeta) {
const existing = await db.query.yachts.findFirst({
where: eq(yachts.id, id),
});
if (!existing || existing.portId !== portId) {
throw new NotFoundError('Yacht');
}
// NOTE: bypassing the shared `softDelete(...)` util: it sets the raw
// column key `archived_at`, which Drizzle does not recognise (the JS
// key is `archivedAt`) and therefore emits an empty SET clause. Until
// the utility is fixed, do the update inline.
await db
.update(yachts)
.set({ archivedAt: new Date() })
.where(and(eq(yachts.id, id), eq(yachts.portId, portId)));
void createAuditLog({
userId: meta.userId,
portId,
action: 'archive',
entityType: 'yacht',
entityId: id,
ipAddress: meta.ipAddress,
userAgent: meta.userAgent,
});
emitToRoom(`port:${portId}`, 'yacht:archived', { yachtId: id });
}