Archived
A pass sells a ticket at each of several events in a series for one price. Supervisors set passes up on the Event series page (events and ticket per event; fixed once sold). Buying on the series page registers the buyer at every event at once with one checkout (free passes give tickets straight away; Pay now if online payment isn't possible). The price is split over the events in proportion to their normal prices: each registration line is priced at its share and the webhook records one Payment per event, so cashups stay per event. Pass registrations aren't repriced, take no coupons and can't be edited. Migration 20261028150000_season_pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
127 lines
6.2 KiB
JavaScript
127 lines
6.2 KiB
JavaScript
const prisma = require('../config/db');
|
|
const { logAdminAction } = require('../utils/adminAudit');
|
|
const { getClientIp } = require('../utils/requestUtils');
|
|
const { cleanSeriesInput, publicEventWhere, splitEvents, follow, unfollow, compareSeries } = require('../utils/series');
|
|
const { scopeFor, canView, eventWhere } = require('../utils/campusScope');
|
|
const { EVENT_CAMPUS_SELECT } = require('../utils/campuses');
|
|
|
|
const fail = (res, error) => res.status(error.statusCode || (error.code === 'P2002' ? 409 : 400)).json({
|
|
message: error.code === 'P2002' ? 'Another series already uses that web address' : error.message,
|
|
});
|
|
|
|
const audit = (req, action, series, metadata = {}) => logAdminAction({
|
|
actorId: req.user.id, actorRole: req.user.role, action, targetType: 'EventSeries', targetId: series.id,
|
|
metadata: { name: series.name, ...metadata }, ip: getClientIp(req),
|
|
});
|
|
|
|
// @desc Every series, with its events (the ones this person may see) and number of followers
|
|
// @route GET /api/series
|
|
// @access Private/Supervisor
|
|
const listSeries = async (req, res) => {
|
|
try {
|
|
const scope = await scopeFor(req);
|
|
const rows = await prisma.eventSeries.findMany({
|
|
orderBy: { name: 'asc' },
|
|
include: {
|
|
events: { orderBy: { startDate: 'desc' }, select: { id: true, title: true, startDate: true, endDate: true, isActive: true, isHidden: true, campusId: true, campus: { select: EVENT_CAMPUS_SELECT } } },
|
|
_count: { select: { followers: true, events: true } },
|
|
},
|
|
});
|
|
// A series spans campuses; campus-scoped staff see the series, but only their campuses' events in it.
|
|
res.json(rows.map(({ _count, events, ...s }) => ({
|
|
...s,
|
|
followers: _count.followers,
|
|
eventCount: _count.events,
|
|
events: events.filter(e => canView(scope, e.campusId)),
|
|
})));
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @desc Create a series
|
|
// @route POST /api/series
|
|
// @access Private/Supervisor
|
|
const createSeries = async (req, res) => {
|
|
try {
|
|
const series = await prisma.eventSeries.create({ data: cleanSeriesInput(req.body) });
|
|
audit(req, 'series_created', series);
|
|
res.status(201).json(series);
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @desc Rename a series or change its page
|
|
// @route PUT /api/series/:id
|
|
// @access Private/Supervisor
|
|
const updateSeries = async (req, res) => {
|
|
try {
|
|
const current = await prisma.eventSeries.findUnique({ where: { id: req.params.id } });
|
|
if (!current) return res.status(404).json({ message: 'Series not found' });
|
|
const data = cleanSeriesInput(req.body, current);
|
|
const series = await prisma.eventSeries.update({ where: { id: current.id }, data });
|
|
audit(req, 'series_updated', series, { changedKeys: Object.keys(data) });
|
|
res.json(series);
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @desc Delete a series (its events stay, just without a series; followers are dropped)
|
|
// @route DELETE /api/series/:id
|
|
// @access Private/Admin
|
|
const deleteSeries = async (req, res) => {
|
|
try {
|
|
const current = await prisma.eventSeries.findUnique({ where: { id: req.params.id }, include: { _count: { select: { events: true, followers: true } } } });
|
|
if (!current) return res.status(404).json({ message: 'Series not found' });
|
|
if (await prisma.seasonPassPurchase.count({ where: { pass: { seriesId: current.id } } })) {
|
|
return res.status(409).json({ message: 'People have bought season passes for this series, so it can\'t be deleted.' });
|
|
}
|
|
await prisma.eventSeries.delete({ where: { id: current.id } });
|
|
audit(req, 'series_deleted', current, { events: current._count.events, followers: current._count.followers });
|
|
res.json({ ok: true });
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @desc A series' public page: its details, upcoming and past public events, and whether the
|
|
// signed-in person follows it
|
|
// @route GET /api/series/by-slug/:slug
|
|
// @access Public
|
|
const getSeriesBySlug = async (req, res) => {
|
|
try {
|
|
const series = await prisma.eventSeries.findUnique({
|
|
where: { slug: String(req.params.slug) },
|
|
select: { id: true, name: true, slug: true, description: true, picture: true },
|
|
});
|
|
if (!series) return res.status(404).json({ message: 'Series not found' });
|
|
const events = await prisma.event.findMany({
|
|
where: { ...publicEventWhere(), seriesId: series.id },
|
|
select: { id: true, title: true, startDate: true, endDate: true, location: true, campus: { select: EVENT_CAMPUS_SELECT } },
|
|
});
|
|
const { upcoming, past } = splitEvents(events);
|
|
const following = req.user ? !!(await prisma.seriesFollow.findUnique({ where: { seriesId_userId: { seriesId: series.id, userId: req.user.id } } })) : false;
|
|
// Season passes switched on (1.47.3); `unavailable` says why one can't be bought now.
|
|
const { passView, PASS_INCLUDE } = require('../utils/seasonPass');
|
|
const passes = (await prisma.seasonPass.findMany({ where: { seriesId: series.id, isActive: true }, orderBy: { createdAt: 'asc' }, include: PASS_INCLUDE }))
|
|
.map(p => passView(p))
|
|
.map(({ items, ...p }) => ({ ...p, items: items.map(({ campusId, ...i }) => i) }));
|
|
res.json({ ...series, upcoming, past, following, passes });
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @desc Year-on-year: registrations, tickets, income and attendance per event in the series
|
|
// (the events this person may see), oldest first, with the change from the one before
|
|
// @route GET /api/series/:id/compare
|
|
// @access Private/Supervisor
|
|
const compare = async (req, res) => {
|
|
try {
|
|
res.json(await compareSeries(req.params.id, { scopeWhere: eventWhere(await scopeFor(req)) }));
|
|
} catch (error) { fail(res, error); }
|
|
};
|
|
|
|
// @route POST /api/series/:id/follow · DELETE /api/series/:id/follow
|
|
// @access Private
|
|
const followSeries = async (req, res) => {
|
|
try { await follow(req.params.id, req.user.id); res.json({ following: true }); } catch (error) { fail(res, error); }
|
|
};
|
|
const unfollowSeries = async (req, res) => {
|
|
try { await unfollow(req.params.id, req.user.id); res.json({ following: false }); } catch (error) { fail(res, error); }
|
|
};
|
|
|
|
module.exports = { listSeries, createSeries, updateSeries, deleteSeries, getSeriesBySlug, followSeries, unfollowSeries, compare };
|