Add coupon codes and staff pricing
Percentage, fixed-amount and comp codes, limited by event, ticket option or variant, dates, use counts, minimum quantity, and people, roles or an email domain. The discount is locked in on the registration (mirrored on Registration.discountTotal so every total sees it), shows on invoices, receipts, emails and the Yoco checkout, and is released on cancellation. Use limits are enforced under an advisory lock. Ticket options and variants can also carry a staff price that Staff and above get automatically. Adds a Supervisor > Coupon codes page, code fields on registration, My Events, manual registration, at the door and the kiosk, a Discounts report, and audit log entries. Also fixes Yoco line items and the free-registration check for lines spanning several price tranches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,10 @@
|
||||
* - computeRegistrationTotalDue: sums quantity*priceSnapshot across each RegistrationOption's
|
||||
* tranches (falling back to the legacy single-priceSnapshot/getEffectiveUnitPrice path for
|
||||
* rows created before tranches existed).
|
||||
* - Staff price: an option (or variant) can carry a staffPrice that Staff and above get
|
||||
* automatically when it's lower than the price they'd otherwise pay.
|
||||
* - Coupon discounts are stored on Registration.discountTotal (see utils/coupons.js) and
|
||||
* subtracted by computeRegistrationTotalDue, never below zero.
|
||||
* - refreshPricingForRegistration: re-evaluates only the newest, not-yet-paid-for tranche of
|
||||
* each RegistrationOption; if its tier has expired or lost stock, resolves the next
|
||||
* applicable tier/price for just that tranche. Older tranches — already priced-in — are
|
||||
@@ -26,6 +30,24 @@
|
||||
|
||||
const prisma = require('../config/db');
|
||||
|
||||
const STAFF_ROLES = ['staff', 'supervisor', 'admin'];
|
||||
|
||||
/** The staff price this user gets for an option/variant, or null if none applies. */
|
||||
function staffPriceFor(option, variantId, user) {
|
||||
if (!user || !STAFF_ROLES.includes(user.role)) return null;
|
||||
const variant = variantId ? (option?.variants || []).find(v => v.id === variantId) : null;
|
||||
const price = variant && variant.staffPrice !== null && variant.staffPrice !== undefined
|
||||
? variant.staffPrice
|
||||
: option?.staffPrice;
|
||||
return price === null || price === undefined ? null : Number(price);
|
||||
}
|
||||
|
||||
// Staff price wins only when it's cheaper than the resolved (possibly early-bird) price.
|
||||
function withStaffPrice(resolved, staffPrice) {
|
||||
if (staffPrice === null || !(staffPrice < resolved.price)) return resolved;
|
||||
return { price: staffPrice, tierId: null };
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute effective unit price for an event option at a given time considering early-bird tiers.
|
||||
* Only checks deadline (not stock). Used for display and as a legacy fallback.
|
||||
@@ -65,9 +87,14 @@ function getEffectiveUnitPrice(eventOption, referenceTime, atTime) {
|
||||
*
|
||||
* @param {object} option - EventOption with price, earlyBirdTiers[]
|
||||
* @param {number} requestedQty - quantity being purchased
|
||||
* @param {object|null} user - buyer ({ role }); Staff and above may get the staff price
|
||||
* @returns {Promise<{ price: number, tierId: string|null }>}
|
||||
*/
|
||||
async function resolveOptionPrice(option, requestedQty = 1) {
|
||||
async function resolveOptionPrice(option, requestedQty = 1, user = null) {
|
||||
return withStaffPrice(await resolveOptionTierPrice(option, requestedQty), staffPriceFor(option, null, user));
|
||||
}
|
||||
|
||||
async function resolveOptionTierPrice(option, requestedQty) {
|
||||
const now = new Date();
|
||||
// Only option-level tiers (no variant association)
|
||||
const tiers = (Array.isArray(option.earlyBirdTiers) ? option.earlyBirdTiers : [])
|
||||
@@ -115,9 +142,14 @@ async function resolveOptionPrice(option, requestedQty = 1) {
|
||||
* @param {object} option - EventOption with price, earlyBirdTiers[], variants[]
|
||||
* @param {string} variantId
|
||||
* @param {number} requestedQty
|
||||
* @param {object|null} user - buyer ({ role }); Staff and above may get the staff price
|
||||
* @returns {Promise<{ price: number, tierId: string|null }>}
|
||||
*/
|
||||
async function resolveVariantTierPrice(option, variantId, requestedQty = 1) {
|
||||
async function resolveVariantTierPrice(option, variantId, requestedQty = 1, user = null) {
|
||||
return withStaffPrice(await resolveVariantOnlyTierPrice(option, variantId, requestedQty), staffPriceFor(option, variantId, user));
|
||||
}
|
||||
|
||||
async function resolveVariantOnlyTierPrice(option, variantId, requestedQty) {
|
||||
const now = new Date();
|
||||
const variant = (option.variants || []).find(v => v.id === variantId);
|
||||
const basePrice = (variant && variant.price !== null && variant.price !== undefined)
|
||||
@@ -172,6 +204,7 @@ async function refreshPricingForRegistration(registrationId) {
|
||||
}
|
||||
},
|
||||
payments: true,
|
||||
user: { select: { role: true } },
|
||||
}
|
||||
});
|
||||
if (!registration) return { changed: false };
|
||||
@@ -215,8 +248,8 @@ async function refreshPricingForRegistration(registrationId) {
|
||||
|
||||
// Deadline has passed (or tier record missing) — resolve the next applicable tier
|
||||
const resolved = ro.variantId
|
||||
? await resolveVariantTierPrice(ro.eventOption, ro.variantId, newest.quantity)
|
||||
: await resolveOptionPrice(ro.eventOption, newest.quantity);
|
||||
? await resolveVariantTierPrice(ro.eventOption, ro.variantId, newest.quantity, registration.user)
|
||||
: await resolveOptionPrice(ro.eventOption, newest.quantity, registration.user);
|
||||
|
||||
const tierChanged = resolved.tierId !== newest.appliedTierId;
|
||||
const priceChanged = Math.abs(resolved.price - newest.priceSnapshot) > 0.001;
|
||||
@@ -273,7 +306,8 @@ function computeOptionLineTotal(ro, lastPaymentAt, atTime) {
|
||||
* Compute total due for a registration at a given time.
|
||||
*
|
||||
* Sums computeOptionLineTotal() across each RegistrationOption (tranche-aware when
|
||||
* `.tranches` is included, legacy priceSnapshot/getEffectiveUnitPrice fallback otherwise).
|
||||
* `.tranches` is included, legacy priceSnapshot/getEffectiveUnitPrice fallback otherwise),
|
||||
* less the registration's coupon discount (`discountTotal`), never below zero.
|
||||
*
|
||||
* @param {object} registration - includes registrationOptions[].{priceSnapshot, quantity, eventOption, tranches?}
|
||||
* and optionally payments[]
|
||||
@@ -291,6 +325,13 @@ function computeRegistrationTotalDue(registration, atTime) {
|
||||
}
|
||||
} catch {}
|
||||
|
||||
const subtotal = computeRegistrationSubtotal(registration, lastPaymentAt, atTime);
|
||||
return Math.max(0, subtotal - Number(registration.discountTotal || 0));
|
||||
}
|
||||
|
||||
/** Sum of the item lines before any coupon discount. */
|
||||
function computeRegistrationSubtotal(registration, lastPaymentAt, atTime) {
|
||||
if (!registration || !Array.isArray(registration.registrationOptions)) return 0;
|
||||
return registration.registrationOptions.reduce((sum, ro) => sum + computeOptionLineTotal(ro, lastPaymentAt, atTime), 0);
|
||||
}
|
||||
|
||||
@@ -317,6 +358,7 @@ function attachComputedTotals(registration) {
|
||||
ro.lineTotal = computeOptionLineTotal(ro, lastPaymentAt, now);
|
||||
}
|
||||
}
|
||||
registration.subtotalComputed = computeRegistrationSubtotal(registration, lastPaymentAt, now);
|
||||
registration.totalDueComputed = computeRegistrationTotalDue(registration, now);
|
||||
return registration;
|
||||
}
|
||||
@@ -332,6 +374,8 @@ module.exports = {
|
||||
refreshPricingForRegistration,
|
||||
computeOptionLineTotal,
|
||||
computeRegistrationTotalDue,
|
||||
computeRegistrationSubtotal,
|
||||
staffPriceFor,
|
||||
attachComputedTotals,
|
||||
attachComputedTotalsToList,
|
||||
};
|
||||
Reference in New Issue
Block a user