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:
@@ -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),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user