88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
import { Prisma, type PrismaClient } from '@prisma/client';
|
|
import { isPrismaNotFound, isPrismaUnique } from '../lib/prisma.js';
|
|
import { badRequest, conflict, notFound } from '../lib/errors.js';
|
|
import { validateEquipmentSpec, type EquipmentKind, type EquipmentSpec } from '../lib/equipmentSpec.js';
|
|
|
|
/** `spec` is validated against `kind` inside the service (unknown at the boundary). */
|
|
export interface EquipmentInput {
|
|
name: string;
|
|
description?: string | null;
|
|
kind: EquipmentKind;
|
|
spec?: unknown;
|
|
}
|
|
|
|
export interface EquipmentUpdateInput {
|
|
name?: string;
|
|
description?: string | null;
|
|
kind?: EquipmentKind;
|
|
spec?: unknown;
|
|
}
|
|
|
|
/** Convert a spec payload to a Prisma JSON value (null → JsonNull). */
|
|
function toPrismaSpec(spec: EquipmentSpec) {
|
|
return spec === null ? Prisma.JsonNull : (spec as Prisma.InputJsonValue);
|
|
}
|
|
|
|
/** List the equipment (machine/device) catalog. */
|
|
export async function listEquipment(prisma: PrismaClient) {
|
|
const equipment = await prisma.equipment.findMany({ orderBy: { name: 'asc' } });
|
|
return { equipment };
|
|
}
|
|
|
|
/** Create an equipment catalog entry (kind determines the spec shape). */
|
|
export async function createEquipment(prisma: PrismaClient, data: EquipmentInput) {
|
|
const checked = validateEquipmentSpec(data.kind, data.spec);
|
|
if (!checked.ok) throw badRequest(checked.error);
|
|
try {
|
|
const item = await prisma.equipment.create({
|
|
data: {
|
|
name: data.name,
|
|
description: data.description ?? null,
|
|
kind: data.kind,
|
|
spec: toPrismaSpec(checked.spec),
|
|
},
|
|
});
|
|
return { equipment: item };
|
|
} catch (e) {
|
|
if (isPrismaUnique(e)) throw conflict('Equipment with this name already exists');
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an equipment catalog entry.
|
|
* `spec` is validated against the resulting `kind` (existing value kept when not provided).
|
|
*/
|
|
export async function updateEquipment(
|
|
prisma: PrismaClient,
|
|
id: string,
|
|
data: EquipmentUpdateInput,
|
|
) {
|
|
const existing = await prisma.equipment.findUnique({ where: { id } });
|
|
if (!existing) throw notFound('Equipment not found');
|
|
const kind = data.kind ?? existing.kind;
|
|
const spec = data.spec !== undefined ? data.spec : existing.spec;
|
|
const checked = validateEquipmentSpec(kind, spec);
|
|
if (!checked.ok) throw badRequest(checked.error);
|
|
const item = await prisma.equipment.update({
|
|
where: { id },
|
|
data: {
|
|
name: data.name,
|
|
description: data.description,
|
|
kind,
|
|
spec: toPrismaSpec(checked.spec),
|
|
},
|
|
});
|
|
return { equipment: item };
|
|
}
|
|
|
|
/** Delete an equipment catalog entry (unlinking it from exercises and gyms). */
|
|
export async function deleteEquipment(prisma: PrismaClient, id: string) {
|
|
try {
|
|
await prisma.equipment.delete({ where: { id } });
|
|
} catch (e) {
|
|
if (isPrismaNotFound(e)) throw notFound('Equipment not found');
|
|
throw e;
|
|
}
|
|
}
|