Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
141 lines
7.3 KiB
TypeScript
141 lines
7.3 KiB
TypeScript
/**
|
|
* Scanning sections and planned event costs (1.51.8). Moved from sectionController.js and
|
|
* costController.js so v1 and /api/v2 share them.
|
|
*/
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { prisma } from '../v2/legacy';
|
|
import { fail } from './errors';
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any -- the JS helpers below aren't typed yet */
|
|
const cashup = require('../utils/cashupUtils') as { assertEventOpen(eventId: string): Promise<unknown>; ALL_METHODS: string[] };
|
|
|
|
const openEvent = async (eventId: string) => {
|
|
try {
|
|
await cashup.assertEventOpen(eventId);
|
|
} catch (e: any) {
|
|
fail(e.message === 'Event not found' ? 404 : 400, e.message);
|
|
}
|
|
};
|
|
|
|
// ─── Sections (which ticket types may come in at which door) ─────────────────
|
|
|
|
const SECTION_INCLUDE = { allowedOptions: { include: { eventOption: true } } } as const;
|
|
|
|
/** Sections, for one event or every event the caller may see (`scopeWhere` from campus scope). */
|
|
export function listSections(eventId: string | undefined, scopeWhere: object) {
|
|
const where = { AND: [eventId && eventId !== 'all' ? { eventId } : {}, scopeWhere] };
|
|
return prisma.section.findMany({ where, include: { ...SECTION_INCLUDE, event: { select: { id: true, title: true } } }, orderBy: { name: 'asc' } });
|
|
}
|
|
|
|
export async function createSection(input: { eventId?: string; name?: string; allowedOptionIds?: string[] }) {
|
|
if (!input.eventId || !input.name) return fail(400, 'Event and section name are required');
|
|
const event = await prisma.event.findUnique({ where: { id: input.eventId }, select: { id: true } });
|
|
if (!event) return fail(404, 'Event not found');
|
|
const section = await prisma.section.create({ data: { id: uuidv4(), eventId: input.eventId, name: String(input.name), updatedAt: new Date() } });
|
|
if (Array.isArray(input.allowedOptionIds) && input.allowedOptionIds.length > 0) {
|
|
await prisma.sectionOption.createMany({
|
|
data: input.allowedOptionIds.map(optionId => ({ id: uuidv4(), sectionId: section.id, eventOptionId: optionId })),
|
|
skipDuplicates: true,
|
|
});
|
|
}
|
|
return prisma.section.findUniqueOrThrow({ where: { id: section.id }, include: SECTION_INCLUDE });
|
|
}
|
|
|
|
/** Renames a section and/or replaces its ticket types (when `allowedOptionIds` is sent). */
|
|
export async function updateSection(sectionId: string, input: { name?: string; allowedOptionIds?: string[] }) {
|
|
const section = await prisma.section.findUnique({ where: { id: sectionId } });
|
|
if (!section) return fail(404, 'Section not found');
|
|
await prisma.section.update({ where: { id: sectionId }, data: { name: input.name || section.name, updatedAt: new Date() } });
|
|
if (Array.isArray(input.allowedOptionIds)) {
|
|
await prisma.sectionOption.deleteMany({ where: { sectionId } });
|
|
if (input.allowedOptionIds.length > 0) {
|
|
await prisma.sectionOption.createMany({ data: input.allowedOptionIds.map(optionId => ({ id: uuidv4(), sectionId, eventOptionId: optionId })) });
|
|
}
|
|
}
|
|
return prisma.section.findUniqueOrThrow({ where: { id: sectionId }, include: SECTION_INCLUDE });
|
|
}
|
|
|
|
export async function deleteSection(sectionId: string) {
|
|
const section = await prisma.section.findUnique({ where: { id: sectionId }, select: { id: true } });
|
|
if (!section) return fail(404, 'Section not found');
|
|
await prisma.section.delete({ where: { id: sectionId } });
|
|
}
|
|
|
|
// ─── Planned costs (for cashup and the profit report) ────────────────────────
|
|
|
|
const COST_INCLUDE = { eventOption: { select: { id: true, name: true } } } as const;
|
|
|
|
function paidFromMethod(value: unknown): string | null | undefined {
|
|
if (value === undefined) return undefined;
|
|
if (value === null || value === '') return null;
|
|
if (!cashup.ALL_METHODS.includes(String(value))) fail(400, "paidFromMethod must be one of 'cash', 'card', 'eft', 'other', or null");
|
|
return String(value);
|
|
}
|
|
|
|
export function listCosts(eventId: string) {
|
|
return prisma.eventCost.findMany({ where: { eventId }, include: COST_INCLUDE, orderBy: { createdAt: 'asc' } });
|
|
}
|
|
|
|
export type CostInput = { label?: string; costType?: string; amount?: number | string; eventOptionId?: string | null; notes?: string | null; paidFromMethod?: string | null };
|
|
|
|
/** A cost per event ("hall hire") or per ticket sold ("T-shirt per camper"). */
|
|
export async function createCost(eventId: string, input: CostInput) {
|
|
await openEvent(eventId);
|
|
if (!input.label || !String(input.label).trim()) fail(400, 'Label is required');
|
|
if (input.costType !== 'once_off' && input.costType !== 'per_item') fail(400, "costType must be 'once_off' or 'per_item'");
|
|
const amount = parseFloat(String(input.amount));
|
|
if (!(amount >= 0)) fail(400, 'Amount must be a non-negative number');
|
|
if (input.costType === 'per_item') {
|
|
if (!input.eventOptionId) fail(400, 'eventOptionId is required for per-item costs');
|
|
const option = await prisma.eventOption.findUnique({ where: { id: input.eventOptionId! }, select: { eventId: true } });
|
|
if (!option || option.eventId !== eventId) fail(404, 'Ticket type not found for this event');
|
|
}
|
|
return prisma.eventCost.create({
|
|
data: {
|
|
id: uuidv4(), eventId, label: String(input.label).trim(), costType: input.costType as 'once_off' | 'per_item', amount,
|
|
eventOptionId: input.costType === 'per_item' ? input.eventOptionId! : null,
|
|
paidFromMethod: (paidFromMethod(input.paidFromMethod) ?? null) as any, notes: input.notes || null,
|
|
},
|
|
include: COST_INCLUDE,
|
|
});
|
|
}
|
|
|
|
export async function updateCost(costId: string, input: CostInput) {
|
|
const existing = await prisma.eventCost.findUnique({ where: { id: costId } });
|
|
if (!existing) return fail(404, 'Cost not found');
|
|
await openEvent(existing.eventId);
|
|
const costType = input.costType !== undefined ? input.costType : existing.costType;
|
|
if (costType !== 'once_off' && costType !== 'per_item') fail(400, "costType must be 'once_off' or 'per_item'");
|
|
const eventOptionId = costType === 'per_item' ? (input.eventOptionId !== undefined ? input.eventOptionId : existing.eventOptionId) : null;
|
|
if (costType === 'per_item') {
|
|
if (!eventOptionId) fail(400, 'eventOptionId is required for per-item costs');
|
|
const option = await prisma.eventOption.findUnique({ where: { id: eventOptionId! }, select: { eventId: true } });
|
|
if (!option || option.eventId !== existing.eventId) fail(404, 'Ticket type not found for this event');
|
|
}
|
|
let amount = existing.amount;
|
|
if (input.amount !== undefined) {
|
|
amount = parseFloat(String(input.amount));
|
|
if (!(amount >= 0)) fail(400, 'Amount must be a non-negative number');
|
|
}
|
|
const paid = paidFromMethod(input.paidFromMethod);
|
|
return prisma.eventCost.update({
|
|
where: { id: costId },
|
|
data: {
|
|
label: input.label !== undefined ? String(input.label).trim() : existing.label,
|
|
costType: costType as 'once_off' | 'per_item',
|
|
amount,
|
|
eventOptionId,
|
|
paidFromMethod: (paid !== undefined ? paid : existing.paidFromMethod) as any,
|
|
notes: input.notes !== undefined ? input.notes : existing.notes,
|
|
},
|
|
include: COST_INCLUDE,
|
|
});
|
|
}
|
|
|
|
export async function deleteCost(costId: string) {
|
|
const existing = await prisma.eventCost.findUnique({ where: { id: costId }, select: { eventId: true } });
|
|
if (!existing) return fail(404, 'Cost not found');
|
|
await openEvent(existing.eventId);
|
|
await prisma.eventCost.delete({ where: { id: costId } });
|
|
}
|