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:
@@ -12,7 +12,7 @@ const { audit } = require('../utils/audit');
|
||||
const billing = require('../utils/billing');
|
||||
const compute = require('../utils/billing/compute');
|
||||
const { ymd, day, today } = require('../utils/billing/dates');
|
||||
const { TIERS, PLANS, BUNDLE_PERCENT, STORAGE } = require('../utils/billing/tiers');
|
||||
const { TIERS, PLANS, BUNDLE_PERCENT, STORAGE, ADDRESS } = require('../utils/billing/tiers');
|
||||
const { addMonths } = require('../utils/billing/dates');
|
||||
const { BILLABLE, PRODUCT_NAMES } = require('../utils/products');
|
||||
|
||||
@@ -194,8 +194,14 @@ const billingLicence = l => ({
|
||||
url: l.install?.url || null, ...reported(l),
|
||||
});
|
||||
|
||||
/** A customer's schedule for the billing screens. */
|
||||
const schedule = c => ({ billingCycle: c.billingCycle, billingStart: ymd(c.billingStart), nextInvoiceDate: ymd(c.nextInvoiceDate), reviewDate: ymd(c.reviewDate) });
|
||||
/** A customer's schedule and address for the billing screens. */
|
||||
const schedule = c => ({
|
||||
billingCycle: c.billingCycle, billingStart: ymd(c.billingStart), nextInvoiceDate: ymd(c.nextInvoiceDate), reviewDate: ymd(c.reviewDate),
|
||||
addressType: c.addressType, domain: c.domain, addressCustomCents: c.addressCustomCents, addressMonthlyCents: compute.addressMonthlyCents(c), domainRenewsOn: ymd(c.domainRenewsOn),
|
||||
});
|
||||
|
||||
// A domain name: letters, digits and hyphens in dot-separated labels, at least one dot.
|
||||
const DOMAIN_RE = /^(?=.{3,253}$)(?!-)[a-z0-9-]{1,63}(?<!-)(\.(?!-)[a-z0-9-]{1,63}(?<!-))+$/;
|
||||
|
||||
/** Every customer's billing at a glance: founding, schedule, licences' plans, balance. */
|
||||
router.get('/customers', wrap(async (req, res) => {
|
||||
@@ -246,9 +252,21 @@ router.get('/customers/:id', wrap(async (req, res) => {
|
||||
plans: PLANS,
|
||||
bundlePercent: BUNDLE_PERCENT,
|
||||
storage: STORAGE,
|
||||
address: ADDRESS,
|
||||
});
|
||||
}));
|
||||
|
||||
// Domain included: Joshua renewed it, so the renewal date moves on a year.
|
||||
router.post('/customers/:id/domain-renewed', wrap(async (req, res) => {
|
||||
const c = await prisma.customer.findUnique({ where: { id: req.params.id } });
|
||||
if (!c) throw bad('Customer not found', 404);
|
||||
if (c.addressType !== 'included' || !c.domainRenewsOn) throw bad('This church has no domain of ours with a renewal date.');
|
||||
const next = addMonths(c.domainRenewsOn, 12);
|
||||
await prisma.customer.update({ where: { id: c.id }, data: { domainRenewsOn: next } });
|
||||
await audit(req, 'domain_renewed', c.id, { domain: c.domain, until: ymd(next) });
|
||||
res.json({ domainRenewsOn: ymd(next), message: `Renewed: next due ${ymd(next)}.` });
|
||||
}));
|
||||
|
||||
// The next invoice as it would be drafted now (nothing is saved): its lines, discounts and total.
|
||||
router.get('/customers/:id/preview', wrap(async (req, res) => {
|
||||
const { getSettings } = require('../utils/settings');
|
||||
@@ -283,9 +301,32 @@ router.put('/customers/:id', wrap(async (req, res) => {
|
||||
data.billingCycle = b.billingCycle;
|
||||
}
|
||||
for (const f of ['billingStart', 'nextInvoiceDate', 'reviewDate']) if (b[f] !== undefined) data[f] = dayOrNull(b[f]);
|
||||
// The address (Hub 2.0): one per church, for all its products.
|
||||
if (b.addressType !== undefined) {
|
||||
if (!Object.keys(ADDRESS).includes(b.addressType)) throw bad('Choose own domain, Cross Code address or domain included.');
|
||||
data.addressType = b.addressType;
|
||||
}
|
||||
if (b.domain !== undefined) {
|
||||
const d = text(b.domain, 253)?.toLowerCase().replace(/^https?:\/\//, '').replace(/\/.*$/, '').replace(/^www\./, '') || null;
|
||||
if (d && !DOMAIN_RE.test(d)) throw bad('The domain looks like hopefamily.co.za.');
|
||||
data.domain = d;
|
||||
}
|
||||
if (b.addressCustomCents !== undefined) {
|
||||
if (b.addressCustomCents === null || b.addressCustomCents === '') data.addressCustomCents = null;
|
||||
else {
|
||||
const n = Math.round(Number(b.addressCustomCents));
|
||||
if (!Number.isInteger(n) || n < 0 || n > 1e7) throw bad('Enter the address price a month.');
|
||||
data.addressCustomCents = n;
|
||||
}
|
||||
}
|
||||
if (b.domainRenewsOn !== undefined) data.domainRenewsOn = dayOrNull(b.domainRenewsOn);
|
||||
const existing = await prisma.customer.findUnique({ where: { id: req.params.id }, include: { licences: true } });
|
||||
if (!existing) throw bad('Customer not found', 404);
|
||||
const merged = { ...existing, ...data };
|
||||
if (merged.addressType === 'crosscode' && merged.domain && !merged.domain.endsWith('.crosscode.co.za')) throw bad('A Cross Code address ends in .crosscode.co.za, e.g. hopefamily.crosscode.co.za.');
|
||||
if (merged.addressType === 'included' && !merged.domainRenewsOn && data.domainRenewsOn === undefined && b.addressType === 'included') {
|
||||
throw bad('Give the date the domain is next due for renewal, so you\'re reminded in time.');
|
||||
}
|
||||
if (merged.billingCycle !== 'none') {
|
||||
if (!merged.billingStart) throw bad('Set the date billing starts.');
|
||||
if (!merged.nextInvoiceDate) data.nextInvoiceDate = merged.billingStart;
|
||||
|
||||
Reference in New Issue
Block a user