const prisma = require('../config/db'); const { v4: uuidv4 } = require('uuid'); const { safeErrorMessage } = require('../utils/errorUtils'); const { assertEventOpen, ALL_METHODS } = require('../utils/cashupUtils'); function normalizePaidFromMethod(value) { if (value === undefined) return undefined; if (value === null || value === '') return null; if (!ALL_METHODS.includes(value)) { throw new Error("paidFromMethod must be one of 'cash', 'card', 'eft', 'other', or null"); } return value; } // @desc List costs for an event // @route GET /api/events/:eventId/costs // @access Private/Supervisor const getEventCosts = async (req, res) => { try { const costs = await prisma.eventCost.findMany({ where: { eventId: req.params.eventId }, include: { eventOption: { select: { id: true, name: true } } }, orderBy: { createdAt: 'asc' } }); res.json(costs); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Create a cost for an event // @route POST /api/events/:eventId/costs // @access Private/Supervisor const createEventCost = async (req, res) => { try { const { eventId } = req.params; const { label, costType, amount, eventOptionId, notes, paidFromMethod } = req.body; await assertEventOpen(eventId, res); if (!label || !String(label).trim()) { res.status(400); throw new Error('Label is required'); } if (costType !== 'once_off' && costType !== 'per_item') { res.status(400); throw new Error("costType must be 'once_off' or 'per_item'"); } const amt = parseFloat(amount); if (!(amt >= 0)) { res.status(400); throw new Error('Amount must be a non-negative number'); } if (costType === 'per_item' && !eventOptionId) { res.status(400); throw new Error('eventOptionId is required for per-item costs'); } if (costType === 'per_item') { const option = await prisma.eventOption.findUnique({ where: { id: eventOptionId } }); if (!option || option.eventId !== eventId) { res.status(404); throw new Error('Ticket type not found for this event'); } } const cost = await prisma.eventCost.create({ data: { id: uuidv4(), eventId, label: String(label).trim(), costType, amount: amt, eventOptionId: costType === 'per_item' ? eventOptionId : null, paidFromMethod: normalizePaidFromMethod(paidFromMethod) ?? null, notes: notes || null }, include: { eventOption: { select: { id: true, name: true } } } }); res.status(201).json(cost); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Update a cost // @route PUT /api/costs/:id // @access Private/Supervisor const updateEventCost = async (req, res) => { try { const existing = await prisma.eventCost.findUnique({ where: { id: req.params.id } }); if (!existing) { res.status(404); throw new Error('Cost not found'); } await assertEventOpen(existing.eventId, res); const { label, costType, amount, eventOptionId, notes, paidFromMethod } = req.body; const nextCostType = costType !== undefined ? costType : existing.costType; if (nextCostType !== 'once_off' && nextCostType !== 'per_item') { res.status(400); throw new Error("costType must be 'once_off' or 'per_item'"); } const nextEventOptionId = nextCostType === 'per_item' ? (eventOptionId !== undefined ? eventOptionId : existing.eventOptionId) : null; if (nextCostType === 'per_item') { if (!nextEventOptionId) { res.status(400); throw new Error('eventOptionId is required for per-item costs'); } const option = await prisma.eventOption.findUnique({ where: { id: nextEventOptionId } }); if (!option || option.eventId !== existing.eventId) { res.status(404); throw new Error('Ticket type not found for this event'); } } let nextAmount = existing.amount; if (amount !== undefined) { const amt = parseFloat(amount); if (!(amt >= 0)) { res.status(400); throw new Error('Amount must be a non-negative number'); } nextAmount = amt; } const nextPaidFromMethod = normalizePaidFromMethod(paidFromMethod); const cost = await prisma.eventCost.update({ where: { id: req.params.id }, data: { label: label !== undefined ? String(label).trim() : existing.label, costType: nextCostType, amount: nextAmount, eventOptionId: nextEventOptionId, paidFromMethod: nextPaidFromMethod !== undefined ? nextPaidFromMethod : existing.paidFromMethod, notes: notes !== undefined ? notes : existing.notes }, include: { eventOption: { select: { id: true, name: true } } } }); res.json(cost); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Delete a cost // @route DELETE /api/costs/:id // @access Private/Supervisor const deleteEventCost = async (req, res) => { try { const existing = await prisma.eventCost.findUnique({ where: { id: req.params.id } }); if (!existing) { res.status(404); throw new Error('Cost not found'); } await assertEventOpen(existing.eventId, res); await prisma.eventCost.delete({ where: { id: req.params.id } }); res.json({ message: 'Cost deleted' }); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; module.exports = { getEventCosts, createEventCost, updateEventCost, deleteEventCost };