const prisma = require('../config/db'); const { safeErrorMessage } = require('../utils/errorUtils'); const { logAdminAction } = require('../utils/adminAudit'); const { getClientIp } = require('../utils/requestUtils'); const { describeBundleItems, isBundleAvailable } = require('../utils/bundles'); const { previewBundles } = require('../utils/coupons'); const { basketBuyer, priceBasket } = require('./couponController'); const round2 = (n) => Math.round(Number(n || 0) * 100) / 100; const optionalDate = (v, label) => { if (v === undefined) return undefined; if (v === null || v === '') return null; const d = new Date(v); if (isNaN(d.getTime())) throw new Error(`${label} is not a valid date`); return d; }; /** What the items would cost separately at the standard (variant or option) price. */ function standardValue(bundle) { return round2((bundle.items || []).reduce((s, i) => { const variant = i.variantId ? (i.eventOption?.variants || []).find(v => v.id === i.variantId) : null; const price = variant && variant.price !== null && variant.price !== undefined ? variant.price : i.eventOption?.price; return s + Number(price || 0) * Number(i.quantity || 0); }, 0)); } // Items with prices loaded, so the standard value can be shown next to the bundle price. const DETAIL_INCLUDE = { items: { include: { eventOption: { select: { id: true, name: true, price: true, variants: { select: { id: true, price: true } } } }, variant: { select: { id: true, name: true } }, }, }, }; function shape(bundle, now = new Date()) { const items = (bundle.items || []).map(({ eventOption, ...i }) => ({ ...i, eventOption: eventOption ? { id: eventOption.id, name: eventOption.name, price: eventOption.price } : null })); return { ...bundle, items, summary: describeBundleItems(bundle), standardValue: standardValue(bundle), available: isBundleAvailable(bundle, now), }; } /** * Turns a create/update body into Prisma data plus the item list. On update, fields * left out of the body are left alone. Items must belong to the bundle's event. */ async function parseBundleBody(body, eventId, { isCreate }) { const data = {}; if (isCreate || body.name !== undefined) { const name = String(body.name || '').trim(); if (!name) throw new Error('Give the bundle a name'); if (name.length > 80) throw new Error('Keep the name under 80 characters'); data.name = name; } if (isCreate || body.price !== undefined) { const price = Number(body.price); if (!(price >= 0) || body.price === '' || body.price === null) throw new Error('Enter the bundle price (zero or more)'); data.price = round2(price); } if (body.description !== undefined) data.description = body.description ? String(body.description).trim() : null; if (body.isActive !== undefined) data.isActive = !!body.isActive; if (body.order !== undefined) data.order = Number.isInteger(Number(body.order)) ? Number(body.order) : 0; const validFrom = optionalDate(body.validFrom, 'Available from'); const validUntil = optionalDate(body.validUntil, 'Available until'); if (validFrom !== undefined) data.validFrom = validFrom; if (validUntil !== undefined) data.validUntil = validUntil; if (validFrom && validUntil && validFrom > validUntil) throw new Error('"Available from" must be before "Available until"'); let items; if (isCreate || body.items !== undefined) { const raw = Array.isArray(body.items) ? body.items : []; // The same option/variant twice becomes one line. const merged = new Map(); for (const it of raw) { if (!it || !it.eventOptionId) continue; const quantity = Number(it.quantity); if (!Number.isInteger(quantity) || quantity < 1) throw new Error('Each item needs a quantity of at least 1'); const key = `${it.eventOptionId}::${it.variantId || ''}`; const prev = merged.get(key); merged.set(key, { eventOptionId: it.eventOptionId, variantId: it.variantId || null, quantity: (prev?.quantity || 0) + quantity }); } items = [...merged.values()]; if (items.length === 0) throw new Error('Add at least one ticket to the bundle'); if (items.reduce((s, i) => s + i.quantity, 0) < 2) throw new Error('A bundle needs at least two tickets in it'); const options = await prisma.eventOption.findMany({ where: { id: { in: items.map(i => i.eventOptionId) } }, select: { id: true, eventId: true, variants: { select: { id: true } } }, }); for (const i of items) { const row = options.find(o => o.id === i.eventOptionId); if (!row || row.eventId !== eventId) throw new Error('One of the tickets isn\'t part of this event'); if (i.variantId && !row.variants.some(v => v.id === i.variantId)) throw new Error('One of the selected variants no longer exists'); } } return { data, items }; } async function eventOr404(eventId, res) { const event = await prisma.event.findUnique({ where: { id: eventId }, select: { id: true, title: true } }); if (!event) { res.status(404); throw new Error('Event not found'); } return event; } // @desc An event's bundle deals with how often each has been used // @route GET /api/bundles/event/:eventId // @access Private/Supervisor const listBundles = async (req, res) => { try { await eventOr404(req.params.eventId, res); const bundles = await prisma.eventBundle.findMany({ where: { eventId: req.params.eventId }, include: DETAIL_INCLUDE, orderBy: [{ order: 'asc' }, { createdAt: 'asc' }], }); const totals = await prisma.registrationBundle.groupBy({ by: ['bundleId'], where: { bundle: { eventId: req.params.eventId }, registration: { status: { not: 'cancelled' } } }, _sum: { count: true, discount: true }, _count: { _all: true }, }); const byId = Object.fromEntries(totals.map(t => [t.bundleId, t])); const now = new Date(); res.json(bundles.map(b => ({ ...shape(b, now), uses: byId[b.id]?._sum?.count || 0, registrations: byId[b.id]?._count?._all || 0, totalDiscount: round2(byId[b.id]?._sum?.discount || 0), }))); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Create a bundle deal // @route POST /api/bundles/event/:eventId // @access Private/Supervisor const createBundle = async (req, res) => { try { const event = await eventOr404(req.params.eventId, res); let parsed; try { parsed = await parseBundleBody(req.body || {}, event.id, { isCreate: true }); } catch (e) { res.status(400); throw e; } const bundle = await prisma.eventBundle.create({ data: { ...parsed.data, eventId: event.id, items: { create: parsed.items } }, include: DETAIL_INCLUDE, }); logAdminAction({ actorId: req.user.id, actorRole: req.user.role, action: 'bundle_created', targetType: 'EventBundle', targetId: bundle.id, metadata: { eventId: event.id, name: bundle.name, price: bundle.price, items: describeBundleItems(bundle) }, ip: getClientIp(req), }); res.status(201).json({ ...shape(bundle), uses: 0, registrations: 0, totalDiscount: 0 }); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Edit a bundle deal (including switching it off). Registrations that already // have it are only re-worked when their own items change. // @route PUT /api/bundles/:id // @access Private/Supervisor const updateBundle = async (req, res) => { try { const existing = await prisma.eventBundle.findUnique({ where: { id: req.params.id } }); if (!existing) { res.status(404); throw new Error('Bundle not found'); } let parsed; try { parsed = await parseBundleBody(req.body || {}, existing.eventId, { isCreate: false }); } catch (e) { res.status(400); throw e; } const bundle = await prisma.eventBundle.update({ where: { id: existing.id }, data: { ...parsed.data, ...(parsed.items ? { items: { deleteMany: {}, create: parsed.items } } : {}) }, include: DETAIL_INCLUDE, }); logAdminAction({ actorId: req.user.id, actorRole: req.user.role, action: 'bundle_updated', targetType: 'EventBundle', targetId: bundle.id, metadata: { eventId: bundle.eventId, name: bundle.name, changedKeys: Object.keys(req.body || {}) }, ip: getClientIp(req), }); res.json(shape(bundle)); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc Delete a bundle deal. One that's been used is switched off instead, so the // registrations that got it keep their price. // @route DELETE /api/bundles/:id // @access Private/Supervisor const deleteBundle = async (req, res) => { try { const existing = await prisma.eventBundle.findUnique({ where: { id: req.params.id } }); if (!existing) { res.status(404); throw new Error('Bundle not found'); } const used = await prisma.registrationBundle.count({ where: { bundleId: existing.id } }); if (used > 0) { await prisma.eventBundle.update({ where: { id: existing.id }, data: { isActive: false } }); } else { await prisma.eventBundle.delete({ where: { id: existing.id } }); } logAdminAction({ actorId: req.user.id, actorRole: req.user.role, action: 'bundle_deleted', targetType: 'EventBundle', targetId: existing.id, metadata: { eventId: existing.eventId, name: existing.name, switchedOff: used > 0 }, ip: getClientIp(req), }); res.json(used > 0 ? { switchedOff: true, message: 'This bundle has been used, so it\'s been switched off instead. Registrations that got it keep their price.' } : { deleted: true, message: 'Bundle deleted' }); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; // @desc The bundle deals a basket gets, before any coupon code // @route POST /api/bundles/quote // @access Public. Body: { eventId, options: [{ eventOptionId, variantId?, quantity }], forUserId? } const quoteBundles = async (req, res) => { try { const { eventId, options, forUserId } = req.body || {}; if (!eventId || !Array.isArray(options)) { res.status(400); throw new Error('eventId and options are required'); } const event = await prisma.event.findUnique({ where: { id: eventId }, include: { eventOptions: { include: { earlyBirdTiers: true, variants: true } } }, }); if (!event) { res.status(404); throw new Error('Event not found'); } const { buyer } = await basketBuyer(req, forUserId); const lines = await priceBasket(event, options, buyer); const subtotal = round2(lines.reduce((s, l) => s + l.unitPrice * l.quantity, 0)); const bundles = await previewBundles(eventId, lines); res.json({ subtotal, bundleDiscount: bundles.discount, bundles: bundles.applications, total: round2(Math.max(0, subtotal - bundles.discount)), }); } catch (error) { res.status(res.statusCode === 200 ? 400 : res.statusCode).json({ message: safeErrorMessage(error) }); } }; /** The deals to show on an event's page: available bundles, without internal fields. */ async function publicBundlesForEvent(eventId) { const bundles = await prisma.eventBundle.findMany({ where: { eventId, isActive: true }, include: DETAIL_INCLUDE, orderBy: [{ order: 'asc' }, { createdAt: 'asc' }], }); const now = new Date(); return bundles.filter(b => isBundleAvailable(b, now)).map(b => { const s = shape(b, now); return { id: s.id, name: s.name, description: s.description, price: s.price, validUntil: s.validUntil, summary: s.summary, standardValue: s.standardValue, items: s.items.map(i => ({ eventOptionId: i.eventOptionId, variantId: i.variantId, quantity: i.quantity })), }; }); } module.exports = { listBundles, createBundle, updateBundle, deleteBundle, quoteBundles, publicBundlesForEvent };