"Copy" on each event card opens the event editor filled in from that event (campus, sessions, options, variants, early birds, sections, form, recipients, feedback questions, certificates, reminders). "New start" moves every date with it. Allocation groups and, for admins, planned costs are copied by POST /api/events/:id/copy-from. Registrations, coupons, attachments, kids check-in and volunteer roles are not copied. Roadmap: event series and cloning ships as 1.47.0-1.47.3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
4.0 KiB
JavaScript
94 lines
4.0 KiB
JavaScript
/**
|
|
* Event cloning (1.47.0). The copy itself is made by the event form in the browser — it opens
|
|
* "New event" filled in from the source event (details, sessions, options, variants, early-bird
|
|
* tiers, form, feedback form, sections, notification recipients), so everything can be changed
|
|
* before saving. This file copies the parts that form doesn't hold, once the new event exists:
|
|
* allocation sets with their groups, and (admins only) the planned costs.
|
|
*
|
|
* Never copied: registrations and everything people did, coupons, attachments, bundles, the kids
|
|
* check-in setup (kids ticket and rooms) and volunteer roles.
|
|
*/
|
|
|
|
const prisma = require('../config/db');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
const COST_FIELDS = ['label', 'costType', 'amount', 'paidFromMethod', 'notes'];
|
|
|
|
/**
|
|
* Costs for the new event. A cost tied to a ticket option moves to the new event's option with
|
|
* the same name; when there's none it stays on the event as a whole.
|
|
* @param {object[]} costs the source event's EventCost rows (with `eventOption.name`)
|
|
* @param {object[]} newOptions the new event's options ({ id, name })
|
|
*/
|
|
function costCopies(costs, newOptions) {
|
|
const byName = new Map(newOptions.map(o => [String(o.name).trim().toLowerCase(), o.id]));
|
|
return costs.map(c => {
|
|
const data = {};
|
|
for (const f of COST_FIELDS) data[f] = c[f] ?? null;
|
|
const name = c.eventOption?.name ? String(c.eventOption.name).trim().toLowerCase() : null;
|
|
data.eventOptionId = (name && byName.get(name)) || null;
|
|
return data;
|
|
});
|
|
}
|
|
|
|
/** The source's allocation sets worth copying: all but the kids rooms. */
|
|
function setsToCopy(sets, kidsRoomSetId) {
|
|
return sets.filter(s => s.id !== kidsRoomSetId);
|
|
}
|
|
|
|
function cloneError(message, statusCode = 400) {
|
|
const err = new Error(message);
|
|
err.statusCode = statusCode;
|
|
return err;
|
|
}
|
|
|
|
/**
|
|
* Copies allocation sets and groups (and costs when `withCosts`) from `fromId` to the just-made
|
|
* event `toId`. Refused once the new event has registrations or allocation sets, so it can only
|
|
* fill a fresh copy.
|
|
* @returns {Promise<{ sets: number, groups: number, costs: number }>}
|
|
*/
|
|
async function copyEventSetup(fromId, toId, { withCosts = false } = {}) {
|
|
if (fromId === toId) throw cloneError("An event can't be copied onto itself");
|
|
const [from, to] = await Promise.all([
|
|
prisma.event.findUnique({
|
|
where: { id: fromId },
|
|
select: {
|
|
id: true, kidsRoomSetId: true,
|
|
allocationSets: { orderBy: { order: 'asc' }, include: { groups: { orderBy: { order: 'asc' } } } },
|
|
costs: { orderBy: { createdAt: 'asc' }, include: { eventOption: { select: { name: true } } } },
|
|
},
|
|
}),
|
|
prisma.event.findUnique({
|
|
where: { id: toId },
|
|
select: { id: true, eventOptions: { select: { id: true, name: true } }, _count: { select: { registrations: true, allocationSets: true } } },
|
|
}),
|
|
]);
|
|
if (!from || !to) throw cloneError('Event not found', 404);
|
|
if (to._count.registrations > 0 || to._count.allocationSets > 0) {
|
|
throw cloneError('Setup can only be copied into a new event (this one already has registrations or groups)', 409);
|
|
}
|
|
|
|
const sets = setsToCopy(from.allocationSets, from.kidsRoomSetId);
|
|
const costs = withCosts ? costCopies(from.costs, to.eventOptions) : [];
|
|
let groups = 0;
|
|
await prisma.$transaction(async tx => {
|
|
for (const s of sets) {
|
|
const set = await tx.allocationSet.create({ data: { id: uuidv4(), eventId: toId, name: s.name, showOnTicket: s.showOnTicket, order: s.order } });
|
|
for (const g of s.groups) {
|
|
await tx.allocationGroup.create({
|
|
data: {
|
|
id: uuidv4(), setId: set.id, name: g.name, capacity: g.capacity, gender: g.gender,
|
|
leaderUserId: g.leaderUserId, order: g.order, minAge: g.minAge, maxAge: g.maxAge, ratio: g.ratio,
|
|
},
|
|
});
|
|
groups++;
|
|
}
|
|
}
|
|
for (const c of costs) await tx.eventCost.create({ data: { id: uuidv4(), eventId: toId, ...c } });
|
|
});
|
|
return { sets: sets.length, groups, costs: costs.length };
|
|
}
|
|
|
|
module.exports = { copyEventSetup, costCopies, setsToCopy };
|