import { Prisma, type PrismaClient } from '@prisma/client'; import { badRequest, conflict, notFound } from '../../lib/errors.js'; import { validateEquipmentSpec } from '../../lib/equipmentSpec.js'; import { isPrismaUnique } from '../../lib/prisma.js'; import { removeEquipmentPhotoFiles } from '../../lib/uploads.js'; import { toPrismaJson, type DayAvailability } from '../me.js'; import { assertGymOwner } from './core.js'; import { gymEquipmentInclude } from './includes.js'; import { announceException } from '../announcements.js'; export interface CreateGymExceptionInput { /** ISO date (YYYY-MM-DD) the exception applies to. */ date: string; /** null = closed, { start, end } = different hours. */ availability: DayAvailability | null; } /** List a gym's upcoming schedule exceptions (owner only). */ export async function listOwnedGymExceptions(prisma: PrismaClient, gymId: string, userId: string) { await assertGymOwner(prisma, gymId, userId); const exceptions = await prisma.gymException.findMany({ where: { gymId }, orderBy: { date: 'asc' }, }); return { exceptions }; } /** Add a one-off schedule exception for a specific date (owner only). */ export async function createOwnedGymException( prisma: PrismaClient, gymId: string, userId: string, data: CreateGymExceptionInput, ) { const gym = await assertGymOwner(prisma, gymId, userId); const date = new Date(data.date); if (Number.isNaN(date.getTime())) throw badRequest('Invalid date'); try { const exception = await prisma.gymException.create({ data: { gymId, date, availability: toPrismaJson(data.availability), }, }); await announceException(prisma, gymId, gym.name, data.date, data.availability ?? null); return { exception }; } catch (e) { if (isPrismaUnique(e)) throw conflict('An exception already exists for this date'); throw e; } } /** Remove a schedule exception (owner only). */ export async function deleteOwnedGymException( prisma: PrismaClient, gymId: string, userId: string, exceptionId: string, ) { await assertGymOwner(prisma, gymId, userId); const exception = await prisma.gymException.findUnique({ where: { id: exceptionId } }); if (!exception || exception.gymId !== gymId) throw notFound('Exception not found'); await prisma.gymException.delete({ where: { id: exceptionId } }); return { exception }; } /** * Tworzy NOWĄ instancję wyposażenia siłowni (właściciel). Wielokrotne dodanie * tego samego typu = kolejne instancje (różne parametry/ilości), nie błąd. * `spec` = nadpisanie specyfikacji katalogowej, walidowane względem `kind`. */ export async function addEquipment( prisma: PrismaClient, gymId: string, userId: string, equipmentId: string, quantity = 1, spec?: unknown, ) { await assertGymOwner(prisma, gymId, userId); const item = await prisma.equipment.findUnique({ where: { id: equipmentId } }); if (!item) throw notFound('Equipment not found'); // Brak / null = instancja dziedziczy spec z katalogu (bez walidacji); // podana wartość musi pasować do `kind`. const validated = spec === undefined || spec === null ? ({ ok: true, spec: null } as const) : validateEquipmentSpec(item.kind, spec); if (!validated.ok) throw badRequest(validated.error); const created = await prisma.gymEquipment.create({ data: { gymId, equipmentId, quantity, spec: toPrismaJson(validated.spec) }, }); const updated = await prisma.gym.findUnique({ where: { id: gymId }, include: gymEquipmentInclude, }); return { gym: updated, instance: created }; } /** * Zmiana ilości / specyfikacji instancji wyposażenia (właściciel). * `spec: null` czyści nadpisanie (instancja dziedziczy spec z katalogu); * brak klucza `spec` = bez zmian. */ export async function updateEquipment( prisma: PrismaClient, gymId: string, userId: string, instanceId: string, data: { quantity?: number; spec?: unknown }, specProvided: boolean, ) { const gym = await assertGymOwner(prisma, gymId, userId); const link = gym.equipment.find((e) => e.id === instanceId); if (!link) throw badRequest('Equipment not found in this gym'); const payload: Prisma.GymEquipmentUpdateInput = {}; if (data.quantity !== undefined) payload.quantity = data.quantity; if (specProvided) { if (data.spec === null || data.spec === undefined) { payload.spec = Prisma.DbNull; } else { const item = await prisma.equipment.findUnique({ where: { id: link.equipmentId }, select: { kind: true }, }); if (!item) throw notFound('Equipment not found'); const validated = validateEquipmentSpec(item.kind, data.spec); if (!validated.ok) throw badRequest(validated.error); payload.spec = toPrismaJson(validated.spec); } } await prisma.gymEquipment.update({ where: { id: instanceId }, data: payload }); const updated = await prisma.gym.findUnique({ where: { id: gymId }, include: gymEquipmentInclude, }); return { gym: updated, equipmentId: link.equipmentId }; } /** Usuwa instancję wyposażenia wraz z plikami zdjęć (właściciel). */ export async function removeEquipment( prisma: PrismaClient, gymId: string, userId: string, instanceId: string, ) { const gym = await assertGymOwner(prisma, gymId, userId); const link = gym.equipment.find((e) => e.id === instanceId); if (!link) throw badRequest('Equipment not found in this gym'); await prisma.gymEquipment.delete({ where: { id: instanceId } }); removeEquipmentPhotoFiles(gymId, instanceId); const updated = await prisma.gym.findUnique({ where: { id: gymId }, include: gymEquipmentInclude, }); return { gym: updated, equipmentId: link.equipmentId }; } /** List a gym's upcoming schedule exceptions. */ export async function listGymExceptions(prisma: PrismaClient, gymId: string) { const gym = await prisma.gym.findUnique({ where: { id: gymId } }); if (!gym) throw notFound('Gym not found'); const exceptions = await prisma.gymException.findMany({ where: { gymId }, orderBy: { date: 'asc' }, }); return { exceptions }; } /** Add a one-off schedule exception for a specific date. */ export async function createGymException( prisma: PrismaClient, gymId: string, data: CreateGymExceptionInput, ) { const gym = await prisma.gym.findUnique({ where: { id: gymId } }); if (!gym) throw notFound('Gym not found'); const date = new Date(data.date); if (Number.isNaN(date.getTime())) throw badRequest('Invalid date'); try { const exception = await prisma.gymException.create({ data: { gymId, date, availability: toPrismaJson(data.availability), }, }); await announceException(prisma, gymId, gym.name, data.date, data.availability ?? null); return { exception }; } catch (e) { if (isPrismaUnique(e)) throw conflict('An exception already exists for this date'); throw e; } } /** Remove a schedule exception. */ export async function deleteGymException(prisma: PrismaClient, gymId: string, exceptionId: string) { const exception = await prisma.gymException.findUnique({ where: { id: exceptionId } }); if (!exception || exception.gymId !== gymId) throw notFound('Exception not found'); await prisma.gymException.delete({ where: { id: exceptionId } }); return { exception }; }