|
|
|
@@ -0,0 +1,365 @@
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
import type { Request } from 'express';
|
|
|
|
|
import { defineRoute, type ApiUser } from '../route';
|
|
|
|
|
import { single } from '../paging';
|
|
|
|
|
import { absoluteUrl, id, idParams, isoDate } from '../schemas';
|
|
|
|
|
import * as series from '../../services/series';
|
|
|
|
|
import * as deals from '../../services/deals';
|
|
|
|
|
import * as campuses from '../../services/campuses';
|
|
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any -- stored records are passed through */
|
|
|
|
|
const campusScope = require('../../utils/campusScope') as {
|
|
|
|
|
scopeFor(req: unknown): Promise<any>;
|
|
|
|
|
listWhere(req: unknown, path?: string): Promise<object>;
|
|
|
|
|
scopeEvent(opts?: { need?: 'view' | 'manage'; resolve?: string; from?: (req: any) => unknown }): any;
|
|
|
|
|
};
|
|
|
|
|
const plain = <T>(v: T): any => JSON.parse(JSON.stringify(v));
|
|
|
|
|
const ctxOf = async (req: Request) => {
|
|
|
|
|
const { getClientIp } = require('../../utils/requestUtils') as { getClientIp(req: unknown): string };
|
|
|
|
|
return { scope: await campusScope.scopeFor(req), ip: getClientIp(req) };
|
|
|
|
|
};
|
|
|
|
|
const actorOf = (user: ApiUser | null) => ({ id: user!.id, role: user!.role, email: user!.email });
|
|
|
|
|
const deleted = single(z.object({ id, deleted: z.literal(true) }));
|
|
|
|
|
const SPANS = 'campusIndependent (a series spans campuses; events are checked when they join it)';
|
|
|
|
|
|
|
|
|
|
// ─── Series ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const seriesRecord = z.looseObject({ id, name: z.string(), slug: z.string(), description: z.string().nullable(), picture: z.string().nullable() });
|
|
|
|
|
const seriesBody = z.object({
|
|
|
|
|
name: z.string().trim().min(1).max(120),
|
|
|
|
|
slug: z.string().trim().max(80).optional().describe('The web address part (/series/<slug>); made from the name when left out'),
|
|
|
|
|
description: z.string().max(5000).nullable().optional(),
|
|
|
|
|
picture: z.string().max(500).nullable().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const listSeries = defineRoute({
|
|
|
|
|
method: 'get', path: '/series', tag: 'Series', summary: 'Every series',
|
|
|
|
|
description: 'With its events (the ones you may see — a series spans campuses), followers and event count.',
|
|
|
|
|
access: 'supervisor', campusScope: 'filters each series\' events by campus (listSeries)',
|
|
|
|
|
response: z.object({ data: z.array(seriesRecord.extend({ followers: z.number().int(), eventCount: z.number().int(), events: z.array(z.looseObject({ id, title: z.string(), startDate: isoDate })) })) }),
|
|
|
|
|
async handler({ req }) { return { data: plain(await series.listSeries(await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const createSeries = defineRoute({
|
|
|
|
|
method: 'post', path: '/series', tag: 'Series', summary: 'Create a series',
|
|
|
|
|
access: 'supervisor', campusScope: SPANS, status: 201, body: seriesBody, response: single(seriesRecord),
|
|
|
|
|
async handler({ body, user, req }) { return { data: plain(await series.createSeries(body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateSeries = defineRoute({
|
|
|
|
|
method: 'patch', path: '/series/:id', tag: 'Series', summary: 'Change a series',
|
|
|
|
|
access: 'supervisor', campusScope: SPANS, params: idParams, body: seriesBody.partial(), response: single(seriesRecord),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await series.updateSeries(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deleteSeries = defineRoute({
|
|
|
|
|
method: 'delete', path: '/series/:id', tag: 'Series', summary: 'Delete a series',
|
|
|
|
|
description: 'Admins. Its events stay (without a series) and followers are dropped. Not once season passes have been sold.',
|
|
|
|
|
access: 'admin', params: idParams, response: deleted,
|
|
|
|
|
async handler({ params, user, req }) { await series.deleteSeries(params.id, actorOf(user), await ctxOf(req)); return { data: { id: params.id, deleted: true as const } }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const seriesPage = defineRoute({
|
|
|
|
|
method: 'get', path: '/series/by-slug/:slug', tag: 'Series', summary: 'A series\' public page',
|
|
|
|
|
description: 'Upcoming and past public events, season passes on sale, and whether you follow it.',
|
|
|
|
|
access: 'optional', params: z.object({ slug: z.string().min(1).max(80) }),
|
|
|
|
|
response: single(seriesRecord.extend({ upcoming: z.array(z.looseObject({ id })), past: z.array(z.looseObject({ id })), following: z.boolean(), passes: z.array(z.looseObject({ id, name: z.string(), price: z.number() })) })),
|
|
|
|
|
async handler({ params, user, req }) {
|
|
|
|
|
const page = plain(await series.seriesPage(params.slug, user?.id ?? null));
|
|
|
|
|
return { data: { ...page, picture: absoluteUrl(req, page.picture) } };
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const compareSeries = defineRoute({
|
|
|
|
|
method: 'get', path: '/series/:id/compare', tag: 'Series', summary: 'Year on year',
|
|
|
|
|
description: 'Registrations, tickets, income and attendance for each event in the series that you may see, oldest first.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeList (eventWhere in compareSeries)', params: idParams, response: single(z.looseObject({})),
|
|
|
|
|
async handler({ params, req }) { return { data: plain(await series.compareSeries(params.id, await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const follow = defineRoute({
|
|
|
|
|
method: 'put', path: '/series/:id/follow', tag: 'Series', summary: 'Follow a series',
|
|
|
|
|
description: 'Hear about new events in it (News & announcements).',
|
|
|
|
|
access: 'user', params: idParams, response: single(z.object({ following: z.boolean() })),
|
|
|
|
|
async handler({ params, user }) { return { data: await series.setFollowing(params.id, user!.id, true) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const unfollow = defineRoute({
|
|
|
|
|
method: 'delete', path: '/series/:id/follow', tag: 'Series', summary: 'Stop following a series',
|
|
|
|
|
access: 'user', params: idParams, response: single(z.object({ following: z.boolean() })),
|
|
|
|
|
async handler({ params, user }) { return { data: await series.setFollowing(params.id, user!.id, false) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── Season passes ───────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const pass = z.looseObject({ id, seriesId: id, name: z.string(), price: z.number(), isActive: z.boolean(), items: z.array(z.looseObject({ eventId: id })) });
|
|
|
|
|
const passBody = z.looseObject({
|
|
|
|
|
seriesId: id,
|
|
|
|
|
name: z.string().trim().min(1).max(120),
|
|
|
|
|
description: z.string().max(5000).nullable().optional(),
|
|
|
|
|
price: z.union([z.number().min(0), z.string().max(20)]),
|
|
|
|
|
saleEndsAt: z.string().max(40).nullable().optional(),
|
|
|
|
|
isActive: z.boolean().optional(),
|
|
|
|
|
items: z.array(z.object({ eventId: id, eventOptionId: id, variantId: id.nullable().optional() })).min(1).max(100),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const listPasses = defineRoute({
|
|
|
|
|
method: 'get', path: '/season-passes', tag: 'Series', summary: 'Season passes',
|
|
|
|
|
description: 'With their events, how the price is shared between them, and how many were sold or are unpaid.',
|
|
|
|
|
access: 'supervisor', campusScope: SPANS, query: z.object({ seriesId: id.optional() }),
|
|
|
|
|
response: z.object({ data: z.array(pass.extend({ sold: z.number().int(), unpaid: z.number().int() })) }),
|
|
|
|
|
async handler({ query }) { return { data: plain(await series.listPasses(query.seriesId)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const createPass = defineRoute({
|
|
|
|
|
method: 'post', path: '/season-passes', tag: 'Series', summary: 'Create a season pass',
|
|
|
|
|
description: 'One price for several events in a series. Campus-scoped supervisors can only include their own campuses\' events.',
|
|
|
|
|
access: 'supervisor', campusScope: 'checked in createPass (its events\' campuses)', status: 201, body: passBody, response: single(pass),
|
|
|
|
|
async handler({ body, user, req }) { return { data: plain(await series.createPass(body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updatePass = defineRoute({
|
|
|
|
|
method: 'patch', path: '/season-passes/:id', tag: 'Series', summary: 'Change a season pass',
|
|
|
|
|
description: 'Its events and price can\'t change once somebody has bought it (switch it off and make a new one).',
|
|
|
|
|
access: 'supervisor', campusScope: 'checked in updatePass (its events\' campuses)', params: idParams, body: passBody.partial(), response: single(pass),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await series.updatePass(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deletePass = defineRoute({
|
|
|
|
|
method: 'delete', path: '/season-passes/:id', tag: 'Series', summary: 'Delete a season pass',
|
|
|
|
|
description: 'Only one nobody has bought — otherwise switch it off.',
|
|
|
|
|
access: 'supervisor', campusScope: 'checked in deletePass (its events\' campuses)', params: idParams, response: deleted,
|
|
|
|
|
async handler({ params, user, req }) { await series.deletePass(params.id, actorOf(user), await ctxOf(req)); return { data: { id: params.id, deleted: true as const } }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const buyPass = defineRoute({
|
|
|
|
|
method: 'post', path: '/season-passes/:id/buy', tag: 'Series', summary: 'Buy a season pass',
|
|
|
|
|
description: 'Registers you at every event on it, then answers with a checkout to pay (or issues the tickets when it\'s free).',
|
|
|
|
|
access: 'user', status: 201, params: idParams,
|
|
|
|
|
body: z.object({ quantity: z.number().int().min(1).max(50).default(1), provider: z.string().max(40).optional() }),
|
|
|
|
|
response: single(z.looseObject({})),
|
|
|
|
|
async handler({ params, body, user }) { return { data: plain(await series.buyPass(params.id, user!.id, body)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const payPass = defineRoute({
|
|
|
|
|
method: 'post', path: '/season-pass-purchases/:id/checkout', tag: 'Series', summary: 'Pay what\'s owed on a pass',
|
|
|
|
|
access: 'user', params: idParams, body: z.object({ provider: z.string().max(40).optional() }), response: single(z.looseObject({})),
|
|
|
|
|
async handler({ params, body, user }) { return { data: plain(await series.payPassPurchase(params.id, user!.id, body)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const myPasses = defineRoute({
|
|
|
|
|
method: 'get', path: '/me/season-passes', tag: 'Me', summary: 'My season passes',
|
|
|
|
|
access: 'user',
|
|
|
|
|
response: z.object({ data: z.array(z.looseObject({ id, quantity: z.number().int(), total: z.number(), status: z.string(), createdAt: isoDate })) }),
|
|
|
|
|
async handler({ user }) { return { data: plain(await series.myPassPurchases(user!.id)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── Bundle deals ────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const basketBody = z.object({
|
|
|
|
|
eventId: id,
|
|
|
|
|
options: z.array(z.object({ eventOptionId: id, variantId: id.nullish(), quantity: z.number().int().min(1).max(1000), sessionIds: z.array(id).max(200).optional() })).max(50),
|
|
|
|
|
forUserId: id.nullable().optional().describe('Staff: price it for this person (null = a walk-in without an account)'),
|
|
|
|
|
});
|
|
|
|
|
const bundle = z.looseObject({ id, eventId: id, name: z.string(), kind: z.enum(['exact', 'mix', 'free', 'group']), price: z.number().nullable(), isActive: z.boolean(), summary: z.string(), headline: z.string(), available: z.boolean() });
|
|
|
|
|
const bundleBody = z.looseObject({
|
|
|
|
|
name: z.string().trim().min(1).max(80),
|
|
|
|
|
kind: z.enum(['exact', 'mix', 'free', 'group']).optional().describe('exact = this set; mix = any N; free = buy N get M free; group = a price for groups'),
|
|
|
|
|
price: z.union([z.number(), z.string().max(20)]).nullable().optional(),
|
|
|
|
|
setSize: z.number().int().nullable().optional(),
|
|
|
|
|
freeCount: z.number().int().nullable().optional(),
|
|
|
|
|
groupPricing: z.enum(['per_ticket', 'percent']).nullable().optional(),
|
|
|
|
|
items: z.array(z.object({ eventOptionId: id, variantId: id.nullish(), quantity: z.number().int().min(1).optional() })).max(50).optional(),
|
|
|
|
|
description: z.string().max(2000).nullable().optional(),
|
|
|
|
|
isActive: z.boolean().optional(),
|
|
|
|
|
validFrom: z.string().max(40).nullable().optional(),
|
|
|
|
|
validUntil: z.string().max(40).nullable().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const listBundles = defineRoute({
|
|
|
|
|
method: 'get', path: '/events/:id/bundles', tag: 'Deals', summary: 'An event\'s bundle deals',
|
|
|
|
|
description: 'With how often each has been used and the discount given.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [campusScope.scopeEvent()], params: idParams,
|
|
|
|
|
response: z.object({ data: z.array(bundle.extend({ uses: z.number(), registrations: z.number().int(), totalDiscount: z.number() })) }),
|
|
|
|
|
async handler({ params }) { return { data: plain(await deals.listBundles(params.id)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const createBundle = defineRoute({
|
|
|
|
|
method: 'post', path: '/events/:id/bundles', tag: 'Deals', summary: 'Add a bundle deal',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [campusScope.scopeEvent({ need: 'manage' })], status: 201,
|
|
|
|
|
params: idParams, body: bundleBody, response: single(bundle),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await deals.createBundle(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateBundle = defineRoute({
|
|
|
|
|
method: 'patch', path: '/bundles/:id', tag: 'Deals', summary: 'Change a bundle deal',
|
|
|
|
|
description: 'Only what\'s sent changes; when a pricing field is sent the whole deal is checked again.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeEvent(bundle, manage)', middleware: [campusScope.scopeEvent({ resolve: 'bundle', need: 'manage' })],
|
|
|
|
|
params: idParams, body: bundleBody.partial(), response: single(bundle),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await deals.updateBundle(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deleteBundle = defineRoute({
|
|
|
|
|
method: 'delete', path: '/bundles/:id', tag: 'Deals', summary: 'Remove a bundle deal',
|
|
|
|
|
description: 'One that\'s been used is switched off instead (`switchedOff`), so registrations that got it keep their price.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeEvent(bundle, manage)', middleware: [campusScope.scopeEvent({ resolve: 'bundle', need: 'manage' })],
|
|
|
|
|
params: idParams, response: single(z.object({ deleted: z.literal(true).optional(), switchedOff: z.literal(true).optional(), message: z.string() })),
|
|
|
|
|
async handler({ params, user, req }) { return { data: await deals.deleteBundle(params.id, actorOf(user), await ctxOf(req)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const quote = defineRoute({
|
|
|
|
|
method: 'post', path: '/bundles/quote', tag: 'Deals', summary: 'The bundle deals a basket gets',
|
|
|
|
|
description: 'Before any coupon code — what the register page shows as you choose tickets.',
|
|
|
|
|
access: 'optional', body: basketBody,
|
|
|
|
|
response: single(z.object({ subtotal: z.number(), bundleDiscount: z.number(), bundles: z.array(z.looseObject({})), total: z.number() })),
|
|
|
|
|
async handler({ body, user }) { return { data: plain(await deals.quoteBundles(body, user ? actorOf(user) : null)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── Coupon codes ────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const coupon = z.looseObject({ id, code: z.string(), type: z.enum(['percent', 'fixed', 'comp']), value: z.number(), active: z.boolean(), summary: z.string() });
|
|
|
|
|
const couponBody = z.looseObject({
|
|
|
|
|
code: z.string().trim().min(3).max(32),
|
|
|
|
|
type: z.enum(['percent', 'fixed', 'comp']).describe('comp = free'),
|
|
|
|
|
value: z.union([z.number(), z.string().max(20)]).optional(),
|
|
|
|
|
description: z.string().max(2000).nullable().optional(),
|
|
|
|
|
active: z.boolean().optional(),
|
|
|
|
|
stackEarlyBird: z.boolean().optional(),
|
|
|
|
|
validFrom: z.string().max(40).nullable().optional(),
|
|
|
|
|
validUntil: z.string().max(40).nullable().optional(),
|
|
|
|
|
maxUses: z.union([z.number().int(), z.string()]).nullable().optional(),
|
|
|
|
|
maxUsesPerUser: z.union([z.number().int(), z.string()]).nullable().optional(),
|
|
|
|
|
minQuantity: z.union([z.number().int(), z.string()]).nullable().optional(),
|
|
|
|
|
restrictRoles: z.array(z.enum(['user', 'staff', 'supervisor', 'admin'])).optional(),
|
|
|
|
|
restrictDomain: z.string().max(120).nullable().optional(),
|
|
|
|
|
eventIds: z.array(id).max(200).optional().describe('Empty = every event (not for campus-scoped supervisors)'),
|
|
|
|
|
options: z.array(z.object({ eventOptionId: id, variantId: id.nullish() })).max(200).optional(),
|
|
|
|
|
userIds: z.array(id).max(1000).optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const listCoupons = defineRoute({
|
|
|
|
|
method: 'get', path: '/coupons', tag: 'Deals', summary: 'Coupon codes',
|
|
|
|
|
description: 'With how often each has been used and the discount given. Campus-scoped supervisors see codes for their campuses\' events.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeList (couponAllowed in listCoupons)',
|
|
|
|
|
response: z.object({ data: z.array(coupon.extend({ uses: z.number().int(), totalDiscount: z.number() })) }),
|
|
|
|
|
async handler({ req }) { return { data: plain(await deals.listCoupons(await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const redemptions = defineRoute({
|
|
|
|
|
method: 'get', path: '/coupons/redemptions', tag: 'Deals', summary: 'Coupon and bundle uses',
|
|
|
|
|
description: 'For the Discounts report, newest first.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeList (listWhere)',
|
|
|
|
|
query: z.object({ eventIds: z.string().max(4000).optional().describe('Comma-separated'), from: z.string().max(40).optional(), to: z.string().max(40).optional() }),
|
|
|
|
|
response: z.object({ data: z.array(z.looseObject({ id, kind: z.enum(['coupon', 'bundle']), uses: z.number(), discountAmount: z.number(), createdAt: isoDate })) }),
|
|
|
|
|
async handler({ query, req }) {
|
|
|
|
|
const date = (v?: string) => { if (!v) return null; const d = new Date(v); return isNaN(d.getTime()) ? null : d; };
|
|
|
|
|
const eventIds = String(query.eventIds || '').split(',').map(s => s.trim()).filter(Boolean);
|
|
|
|
|
return { data: plain(await deals.listRedemptions({ eventIds, from: date(query.from), to: date(query.to) }, await campusScope.listWhere(req, 'event'))) };
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const getCoupon = defineRoute({
|
|
|
|
|
method: 'get', path: '/coupons/:id', tag: 'Deals', summary: 'One coupon code with its uses',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeList (couponAllowed in getCoupon)', params: idParams,
|
|
|
|
|
response: single(coupon.extend({ redemptions: z.array(z.looseObject({ id })) })),
|
|
|
|
|
async handler({ params, req }) { return { data: plain(await deals.getCoupon(params.id, await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const createCoupon = defineRoute({
|
|
|
|
|
method: 'post', path: '/coupons', tag: 'Deals', summary: 'Create a coupon code',
|
|
|
|
|
access: 'supervisor', campusScope: 'checked in createCoupon (eventIdsAllowed)', status: 201, body: couponBody, response: single(coupon),
|
|
|
|
|
async handler({ body, user, req }) { return { data: plain(await deals.createCoupon(body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateCoupon = defineRoute({
|
|
|
|
|
method: 'patch', path: '/coupons/:id', tag: 'Deals', summary: 'Change a coupon code',
|
|
|
|
|
description: 'Only what\'s sent changes. Discounts already given keep their amount; a used code can\'t be renamed.',
|
|
|
|
|
access: 'supervisor', campusScope: 'checked in updateCoupon (couponAllowed)', params: idParams, body: couponBody.partial(), response: single(coupon),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await deals.updateCoupon(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const checkCode = defineRoute({
|
|
|
|
|
method: 'post', path: '/coupons/check', tag: 'Deals', summary: 'Check a coupon code against a basket',
|
|
|
|
|
description: 'Always a 200: `valid` with the discount and total, or `valid: false` with why. Restricted codes need you signed in.',
|
|
|
|
|
access: 'optional', campusScope: 'scopeEvent(body.eventId)', middleware: [campusScope.scopeEvent({ from: req => req.body?.eventId })],
|
|
|
|
|
body: basketBody.extend({ code: z.string().trim().min(1).max(40) }),
|
|
|
|
|
response: single(z.discriminatedUnion('valid', [
|
|
|
|
|
z.object({ valid: z.literal(true), code: z.string(), type: z.string(), value: z.number(), summary: z.string(), description: z.string().nullable(), discount: z.number(), bundleDiscount: z.number(), bundles: z.array(z.looseObject({})), subtotal: z.number(), total: z.number() }),
|
|
|
|
|
z.object({ valid: z.literal(false), message: z.string() }),
|
|
|
|
|
])),
|
|
|
|
|
async handler({ body, user }) { return { data: plain(await deals.checkCode(body, user ? actorOf(user) : null)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── Campuses ────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const publicCampus = z.looseObject({ id, name: z.string(), slug: z.string() });
|
|
|
|
|
const adminCampus = publicCampus.extend({ archivedAt: isoDate.nullable(), order: z.number().int(), _count: z.looseObject({ events: z.number().int() }) });
|
|
|
|
|
const campusBody = z.looseObject({
|
|
|
|
|
name: z.string().trim().min(1).max(120),
|
|
|
|
|
slug: z.string().trim().max(80).optional(),
|
|
|
|
|
colour: z.string().max(20).nullable().optional(),
|
|
|
|
|
notifyRecipientIds: z.array(id).max(100).optional().describe('Who gets this campus\'s daily summary (staff only)'),
|
|
|
|
|
archived: z.boolean().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const listCampuses = defineRoute({
|
|
|
|
|
method: 'get', path: '/campuses', tag: 'Campuses', summary: 'The active campuses',
|
|
|
|
|
description: '`enabled` false means campuses are switched off (the list is then empty).',
|
|
|
|
|
access: 'public', response: single(z.object({ enabled: z.boolean(), campuses: z.array(publicCampus) })),
|
|
|
|
|
async handler() { return { data: plain(await campuses.publicCampuses()) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const adminList = defineRoute({
|
|
|
|
|
method: 'get', path: '/campuses/admin', tag: 'Campuses', summary: 'Every campus (Site settings)',
|
|
|
|
|
description: 'Archived ones last, with event counts and summary recipients.',
|
|
|
|
|
access: 'admin', response: single(z.object({ enabled: z.boolean(), campuses: z.array(adminCampus) })),
|
|
|
|
|
async handler() { return { data: plain(await campuses.adminCampuses()) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const campusBySlug = defineRoute({
|
|
|
|
|
method: 'get', path: '/campuses/by-slug/:slug', tag: 'Campuses', summary: 'A campus landing page',
|
|
|
|
|
access: 'public', params: z.object({ slug: z.string().min(1).max(80) }), response: single(publicCampus),
|
|
|
|
|
async handler({ params }) { return { data: plain(await campuses.campusBySlug(params.slug)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const setEnabled = defineRoute({
|
|
|
|
|
method: 'put', path: '/campuses/enabled', tag: 'Campuses', summary: 'Switch campuses on or off',
|
|
|
|
|
description: 'Off hides campus fields and filters; nothing is deleted.',
|
|
|
|
|
access: 'admin', body: z.object({ enabled: z.boolean() }), response: single(z.object({ enabled: z.boolean() })),
|
|
|
|
|
async handler({ body, user, req }) { return { data: await campuses.setCampusesEnabled(body.enabled, actorOf(user), await ctxOf(req)) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const reorder = defineRoute({
|
|
|
|
|
method: 'put', path: '/campuses/order', tag: 'Campuses', summary: 'Put the campuses in order',
|
|
|
|
|
access: 'admin', body: z.object({ ids: z.array(id).min(1).max(200) }), response: single(z.object({ ids: z.array(id) })),
|
|
|
|
|
async handler({ body }) { await campuses.reorderCampuses(body.ids); return { data: { ids: body.ids } }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const createCampus = defineRoute({
|
|
|
|
|
method: 'post', path: '/campuses', tag: 'Campuses', summary: 'Add a campus',
|
|
|
|
|
access: 'admin', status: 201, body: campusBody, response: single(adminCampus),
|
|
|
|
|
async handler({ body, user, req }) { return { data: plain(await campuses.createCampus(body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateCampus = defineRoute({
|
|
|
|
|
method: 'patch', path: '/campuses/:id', tag: 'Campuses', summary: 'Change a campus',
|
|
|
|
|
description: 'Details, summary recipients, or `archived`.',
|
|
|
|
|
access: 'admin', params: idParams, body: campusBody.partial(), response: single(adminCampus),
|
|
|
|
|
async handler({ params, body, user, req }) { return { data: plain(await campuses.updateCampus(params.id, body, actorOf(user), await ctxOf(req))) }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deleteCampus = defineRoute({
|
|
|
|
|
method: 'delete', path: '/campuses/:id', tag: 'Campuses', summary: 'Delete a campus',
|
|
|
|
|
description: 'Only one with no events and no staff limited to it — otherwise archive it (CONFLICT, `reason` CAMPUS_HAS_EVENTS or CAMPUS_HAS_SCOPED_STAFF).',
|
|
|
|
|
access: 'admin', params: idParams, response: deleted,
|
|
|
|
|
async handler({ params, user, req }) { await campuses.deleteCampus(params.id, actorOf(user), await ctxOf(req)); return { data: { id: params.id, deleted: true as const } }; },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const audience = defineRoute({
|
|
|
|
|
method: 'get', path: '/campuses/:id/audience', tag: 'Campuses', summary: 'Everyone at a campus',
|
|
|
|
|
description: 'For the email and WhatsApp composers: people whose home campus it is, plus (`registrants=true`) people registered for its events in the last 12 months. News consent is checked when sending.',
|
|
|
|
|
access: 'supervisor', campusScope: 'scopeList (own campuses only, in campusAudience)', params: idParams,
|
|
|
|
|
query: z.object({ registrants: z.enum(['true', 'false']).default('false').transform(v => v === 'true') }),
|
|
|
|
|
response: single(z.object({ campus: z.object({ id, name: z.string() }), users: z.array(z.object({ id, name: z.string(), email: z.string(), phoneNumber: z.string().nullable() })) })),
|
|
|
|
|
async handler({ params, query, req }) { return { data: plain(await campuses.campusAudience(params.id, query.registrants, await ctxOf(req))) }; },
|
|
|
|
|
});
|