Hub 2.0 step 5: the church's address

Own domain R0, Cross Code address R10, domain included R25 (custom price
for other endings), once per invoice and never discounted; renewal date
with a reminder 30 days before, once per renewal, and Renewed to move it on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 16:24:10 +02:00
co-authored by Claude Opus 5.5
parent fff1c6f4ef
commit fbbf72dcd8
9 changed files with 229 additions and 11 deletions
+24 -2
View File
@@ -20,7 +20,7 @@
*/
const {
tierNamed, reviewTier, planPrice, extraStorageBlocks, STORAGE, BUNDLE_PERCENT, YEARLY_MONTHS, FOUNDING_PERCENT,
tierNamed, reviewTier, planPrice, extraStorageBlocks, STORAGE, ADDRESS, BUNDLE_PERCENT, YEARLY_MONTHS, FOUNDING_PERCENT,
} = require('./tiers');
const { ymd, day, addDays, addMonths, periodLabel, shortDate } = require('./dates');
const { PRODUCTS, PRODUCT_NAMES, BILLABLE } = require('../products');
@@ -126,6 +126,24 @@ function reviewLicence(licence, product) {
};
}
/** The monthly price of the customer's address in cents (a custom price wins). */
function addressMonthlyCents(customer) {
if (customer.addressCustomCents != null && customer.addressCustomCents >= 0) return customer.addressCustomCents;
return (ADDRESS[customer.addressType || 'own_domain']?.monthly || 0) * 100;
}
/** The address line for a period, or null (own domain, or free). 12 months' price when yearly. */
function addressLine(customer, { start, end, cycle }) {
const cents = addressMonthlyCents(customer);
if (!cents) return null;
const option = ADDRESS[customer.addressType] || ADDRESS.own_domain;
const m = monthsIn(cycle);
return {
description: `${option.label}${customer.domain ? ` (${customer.domain})` : ''}, ${m === 12 ? '12 months' : periodLabel(start, end)}`,
quantity: m, unitCents: cents, amountCents: m * cents,
};
}
/**
* A customer's invoice lines for a period: each billed product (with a prorated line for days
* owed before the period), its founding discount, the bundle discount, then add-ons. Pure.
@@ -221,6 +239,10 @@ function customerInvoiceLines({ customer, licences, period, reviewed }) {
}
lines.push(...addOns);
// The church's address: once per invoice with any product on it, never discounted.
const address = addressLine(customer, { start, end, cycle });
if (address && products.length) lines.push(address);
const plan = plans.length === 1 && plans[0].product === 'events'
? plans[0].plan
: plans.map(p => (p.product === 'website' ? PRODUCT_NAMES.website : `${PRODUCT_NAMES[p.product]} ${p.plan}`)).join(' · ') || null;
@@ -334,6 +356,6 @@ function statement({ invoices, payments, from, to }) {
}
module.exports = {
recipientFor, contactInstall, cleanLines, totals, planLines, productLabel, customerInvoiceLines, draftCustomerInvoice,
recipientFor, contactInstall, cleanLines, totals, planLines, productLabel, customerInvoiceLines, draftCustomerInvoice, addressMonthlyCents,
planChangesOf, invoiceState, statement, invoiceDescription, anchorDayOf, monthsIn,
};
+31 -3
View File
@@ -10,6 +10,8 @@
* 2. Sends approved invoices on their date, from billing_send_hour (South African time).
* 3. Emails Joshua once when an invoice's date has come and it still isn't approved.
* 4. Applies tier changes on their effective date (the licence's plan).
* 5. Hub 2.0: reminds Joshua 30 days before a domain he registered for a church (address
* "domain included") is due for renewal, once per renewal date.
*/
const { Prisma } = require('@prisma/client');
@@ -250,15 +252,41 @@ async function applyDueLicenceChanges(now = new Date()) {
return due.length;
}
const DOMAIN_REMINDER_DAYS = 30;
/**
* Emails Joshua about included domains due for renewal within 30 days (or overdue), once per
* renewal date. "Renewed" on the customer page moves the date on a year.
*/
async function remindDomainRenewals(now, settings) {
if (!settings.alert_email) return 0;
const due = (await prisma.customer.findMany({
where: { addressType: 'included', domainRenewsOn: { lte: addDays(today(now), DOMAIN_REMINDER_DAYS) } },
})).filter(c => !c.domainRemindedFor || ymd(c.domainRemindedFor) !== ymd(c.domainRenewsOn));
if (!due.length) return 0;
const content = layout(settings, {
heading: due.length === 1 ? 'A church\'s domain is due for renewal' : `${due.length} churches' domains are due for renewal`,
paragraphs: ['You registered these domains for the churches (address: domain included). Renew them, then press Renewed on the church\'s page so the date moves on a year.'],
rows: due.map(c => [`${c.name}`, `${c.domain || '(no domain recorded)'}: ${longDate(c.domainRenewsOn)}${ymd(c.domainRenewsOn) < ymd(today(now)) ? ' (overdue)' : ''}`]),
button: { label: 'Open billing', url: hubLink('/billing') },
signoff: false,
});
const res = await sendBillingEmail(settings, { kind: 'reminder', to: settings.alert_email, subject: `Domain renewal${due.length === 1 ? '' : 's'} due: ${due.map(c => c.domain || c.name).join(', ')}`, content });
if (res.ok) for (const c of due) await prisma.customer.update({ where: { id: c.id }, data: { domainRemindedFor: c.domainRenewsOn } });
return due.length;
}
/** The background job. */
async function runBilling(now = new Date()) {
const settings = await getSettings();
if (String(settings.billing_enabled) !== 'true') return { drafted: 0, sent: 0, reminded: 0, applied: 0 };
// Domain renewals matter even while invoicing is switched off.
const domains = await remindDomainRenewals(now, settings).catch(e => { console.warn('[billing] domain reminder failed:', e?.message || e); return 0; });
if (String(settings.billing_enabled) !== 'true') return { drafted: 0, sent: 0, reminded: 0, applied: 0, domains };
const drafted = await createDueDrafts(now, settings);
const sent = await sendDueInvoices(now, settings);
const reminded = await remindUnapproved(now, settings);
const applied = await applyDueLicenceChanges(now);
return { drafted, sent, reminded, applied };
return { drafted, sent, reminded, applied, domains };
}
// ─── Invoice actions ─────────────────────────────────────────────────────────
@@ -622,7 +650,7 @@ async function licenceChangePdfFor(id) {
module.exports = {
nextNumber, loadInvoice, invoicePdfFor, draftForCustomer, createDueDrafts, sendInvoice, sendDueInvoices, remindUnapproved,
applyDueLicenceChanges, runBilling, approveInvoice, unapproveInvoice, updateInvoice, recalculateInvoice, voidInvoice,
applyDueLicenceChanges, runBilling, remindDomainRenewals, approveInvoice, unapproveInvoice, updateInvoice, recalculateInvoice, voidInvoice,
resendInvoice, createManualInvoice, recordPayment, deletePayment, resendReceipt, receiptPdfFor, customerStatement,
statementPdfFor, emailStatement, createLicenceChange, resendLicenceChange, licenceChangePdfFor, emailApproval,
};
+11 -1
View File
@@ -41,6 +41,16 @@ const APP_TIERS = [
/** Extra website storage: R20 a month per 5 GB above the 5 GB included. */
const STORAGE = { includedBytes: 5 * 1024 ** 3, stepBytes: 5 * 1024 ** 3, monthly: 20 };
/**
* The church's address, one for all its products, charged once per invoice and never discounted.
* Other domain endings (.com, .church) are charged at cost: a custom price on the customer.
*/
const ADDRESS = {
own_domain: { label: 'Own domain', monthly: 0 },
crosscode: { label: 'Cross Code address', monthly: 10 },
included: { label: 'Domain included', monthly: 25 },
};
/** Bundles: % off the products (not the add-ons) by how many are billed. Accounts doesn't count. */
const BUNDLE_PERCENT = { 2: 15, 3: 25 };
@@ -112,6 +122,6 @@ function extraStorageBlocks(storage) {
}
module.exports = {
TIERS, WEBSITE_PLANS, APP_TIERS, PLANS, STORAGE, BUNDLE_PERCENT, YEARLY_MONTHS, FOUNDING_PERCENT,
TIERS, WEBSITE_PLANS, APP_TIERS, PLANS, STORAGE, ADDRESS, BUNDLE_PERCENT, YEARLY_MONTHS, FOUNDING_PERCENT,
tierNamed, tierForBand, appTierForBand, reviewTier, planPrice, extraStorageBlocks,
};