Billing income bands up to R10m, 12-month event income in the heartbeat (2.0.2)

- Heartbeat income bands widened from "over R100 000" to 14 bands up to
  "over R10 000 000"; every licence-tier edge is a band edge (tested).
- New income.eventIncome12m: last 12 months' event income without donations,
  sponsorships or donation legs, which the licence tier is billed on.
- Fix: donation legs were counted twice in the heartbeat income bands.
- Protocol doc, roadmap pricing decisions, changelog, what's new, handover.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-03 12:18:01 +02:00
co-authored by Claude Opus 5.5
parent cebfe52be5
commit aabf3cbe07
13 changed files with 95 additions and 28 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "event-management-backend",
"version": "2.0.1",
"version": "2.0.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "event-management-backend",
"version": "2.0.1",
"version": "2.0.2",
"hasInstallScript": true,
"dependencies": {
"@cj-tech-master/excelts": "^10.2.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "event-management-backend",
"version": "2.0.1",
"version": "2.0.2",
"description": "Event Management System Backend",
"main": "src/index.js",
"scripts": {
+28 -13
View File
@@ -13,21 +13,29 @@ const DAY = 24 * 60 * 60 * 1000;
const APP_VERSION = require('../../package.json').version;
const STARTED_AT = Date.now();
const INCOME_BANDS = [
[1000, 'R0–R1 000'],
[5000, 'R1 001–R5 000'],
[20000, 'R5 001–R20 000'],
[50000, 'R20 001–R50 000'],
[100000, 'R50 001–R100 000'],
];
// The tops of the income bands, in rand (docs/hub-protocol.md §3). Cross Code's licence tiers
// are billed on the 12-month event-income band, so every tier edge (R50 000, R150 000, R400 000,
// R1m, R2.5m, R5m, R10m) is also a band edge: change them together.
const BAND_TOPS = [1000, 5000, 20000, 50000, 100000, 150000, 250000, 400000, 700000, 1000000, 2500000, 5000000, 10000000];
/** The range an amount falls in: "R5 001–R20 000", or "over R100 000". */
const randText = n => `R${String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ' ')}`;
const INCOME_BANDS = BAND_TOPS.map((top, i) => [top, `${randText(i ? BAND_TOPS[i - 1] + 1 : 0)}–${randText(top)}`]);
const TOP_BAND = `over ${randText(BAND_TOPS[BAND_TOPS.length - 1])}`;
/** The range an amount falls in: "R5 001–R20 000", or "over R10 000 000". */
function incomeBand(amount) {
const rand = Math.max(0, Math.round(Number(amount) || 0));
for (const [max, label] of INCOME_BANDS) if (rand <= max) return label;
return 'over R100 000';
return TOP_BAND;
}
// Applying an earlier donation to a registration adds a "leg" payment that isn't new money
// (cashupUtils.isDonationLeg), so every money total leaves it out.
const NOT_DONATION_LEG = { NOT: { AND: [{ isDonation: false }, { originalPaymentId: { not: null } }, { amount: { gt: 0 } }] } };
// Event income, what the licence tier is billed on: payments less refunds, without donations or
// sponsorships (both isDonation) and without the legs that apply a donation to a registration.
const EVENT_INCOME = { isDonation: false, ...NOT_DONATION_LEG };
async function postgresVersion() {
try {
const rows = await prisma.$queryRawUnsafe('SHOW server_version');
@@ -98,8 +106,9 @@ async function health() {
async function stats(now = new Date()) {
const { campusesEnabled } = require('../utils/campuses');
const since = new Date(now.getTime() - 30 * DAY);
const yearAgo = new Date(now.getTime() - 365 * DAY);
const realUser = { isActive: true, NOT: [{ email: { endsWith: '@deleted.invalid' } }, { email: { endsWith: '@guest.local' } }] };
const [users, activeEvents, upcomingEvents, totalEvents, regs30, regsTotal, tickets30, ticketsTotal, checkIns30, income30, incomeAll, providers] = await Promise.all([
const [users, activeEvents, upcomingEvents, totalEvents, regs30, regsTotal, tickets30, ticketsTotal, checkIns30, income30, incomeAll, eventIncome12m, providers] = await Promise.all([
prisma.user.count({ where: realUser }),
prisma.event.count({ where: { isActive: true, startDate: { lte: now }, endDate: { gte: now } } }),
prisma.event.count({ where: { isActive: true, startDate: { gt: now } } }),
@@ -109,8 +118,9 @@ async function stats(now = new Date()) {
prisma.ticket.count({ where: { createdAt: { gte: since } } }),
prisma.ticket.count(),
prisma.ticketUsage.count({ where: { scannedAt: { gte: since } } }),
prisma.payment.aggregate({ where: { createdAt: { gte: since } }, _sum: { amount: true } }),
prisma.payment.aggregate({ _sum: { amount: true } }),
prisma.payment.aggregate({ where: { createdAt: { gte: since }, ...NOT_DONATION_LEG }, _sum: { amount: true } }),
prisma.payment.aggregate({ where: NOT_DONATION_LEG, _sum: { amount: true } }),
prisma.payment.aggregate({ where: { createdAt: { gte: yearAgo }, ...EVENT_INCOME }, _sum: { amount: true } }),
require('../payments').availableProviders().then(list => list.map(p => p.id)).catch(() => []),
]);
const campusesOn = await campusesEnabled().catch(() => false);
@@ -126,7 +136,12 @@ async function stats(now = new Date()) {
paymentProviders: providers,
campuses: campusesOn,
campusCount,
income: { last30d: incomeBand(income30._sum.amount), allTime: incomeBand(incomeAll._sum.amount) },
income: {
last30d: incomeBand(income30._sum.amount),
allTime: incomeBand(incomeAll._sum.amount),
// 2.0.2+: event income over the last 12 months, what the licence tier is billed on.
eventIncome12m: incomeBand(eventIncome12m._sum.amount),
},
};
}
+15 -1
View File
@@ -72,9 +72,23 @@ describe('heartbeat report', () => {
expect(incomeBand(1001)).toBe('R1 001–R5 000');
expect(incomeBand(19999)).toBe('R5 001–R20 000');
expect(incomeBand(100000)).toBe('R50 001–R100 000');
expect(incomeBand(250000)).toBe('over R100 000');
expect(incomeBand(100001)).toBe('R100 001–R150 000');
expect(incomeBand(250000)).toBe('R150 001–R250 000');
expect(incomeBand(400001)).toBe('R400 001–R700 000');
expect(incomeBand(1000000)).toBe('R700 001–R1 000 000');
expect(incomeBand(1000001)).toBe('R1 000 001–R2 500 000');
expect(incomeBand(5000000)).toBe('R2 500 001–R5 000 000');
expect(incomeBand(9999999)).toBe('R5 000 001–R10 000 000');
expect(incomeBand(10000001)).toBe('over R10 000 000');
expect(incomeBand(null)).toBe('R0–R1 000');
});
test('every licence tier edge is a band edge', () => {
// Cross Code bills the tiers on these (crosscode-site src/data/pricing.mjs).
for (const edge of [50000, 150000, 400000, 1000000, 2500000, 5000000, 10000000]) {
expect(incomeBand(edge)).not.toBe(incomeBand(edge + 1));
}
});
});
describe('read-only mode', () => {