Hub 2.12.0: one product uninstalled at a time, stale installs removed, customers archived
- Uninstalls name the product (agent 1.5.0); a whole-church uninstall marks every product of the church on that server removed. Older agents are refused while the church has others. - A second product's uninstall joins one already planned (one export and one uninstall) or gets its own time; the last product removes the whole church. Accounts goes last. - Remove an install from the Hub after 24 hours without a heartbeat (POST .../forget). - Archive and restore customers (migration 20261123090000_customer_archive). - Several addresses for a customer's email and billing email, with a + button. - Statements show R0 invoices' amounts. Vendors contracts 1.16.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,7 @@ const { parseRepoUrl, syncReleases, releaseSources, cleanSources } = require('..
|
||||
const ai = require('../utils/ai');
|
||||
const { AI_MODELS } = require('../utils/aiModels');
|
||||
const { requireTotp } = require('../utils/jobs');
|
||||
const { emailList } = require('../utils/emails');
|
||||
|
||||
const router = express.Router();
|
||||
router.use(protect);
|
||||
@@ -73,7 +74,7 @@ router.get('/dashboard', wrap(async (req, res) => {
|
||||
const churches = churchesOf(installs);
|
||||
const rows = installs.map(i => summary(i, settings, latest, new Date(), churches.get(i.customerId)));
|
||||
const [customers, licences, openAlerts, billed, sentInvoices] = await Promise.all([
|
||||
prisma.customer.count(),
|
||||
prisma.customer.count({ where: { archivedAt: null } }),
|
||||
prisma.licence.groupBy({ by: ['product', 'status'], _count: { _all: true } }),
|
||||
prisma.installEvent.count({ where: { resolvedAt: null, level: { not: 'info' } } }),
|
||||
prisma.customer.findMany({ where: { billingCycle: { not: 'none' } }, include: { licences: { include: { install: true } } } }),
|
||||
@@ -113,8 +114,11 @@ router.get('/dashboard', wrap(async (req, res) => {
|
||||
|
||||
// ─── Customers ────────────────────────────────────────────────────────────────
|
||||
|
||||
// Hub 2.12.0: archived customers only with ?archived=1 (and only them).
|
||||
router.get('/customers', wrap(async (req, res) => {
|
||||
const archived = req.query.archived === '1' || req.query.archived === 'true';
|
||||
const customers = await prisma.customer.findMany({
|
||||
where: { archivedAt: archived ? { not: null } : null },
|
||||
orderBy: { name: 'asc' },
|
||||
include: { licences: { select: { id: true, product: true, status: true, expiresAt: true } }, installs: { where: { releasedAt: null }, select: { id: true, product: true, url: true, lastHeartbeatAt: true } } },
|
||||
});
|
||||
@@ -123,7 +127,9 @@ router.get('/customers', wrap(async (req, res) => {
|
||||
|
||||
function customerData(body, { partial = false } = {}) {
|
||||
const data = {};
|
||||
for (const f of ['name', 'contactName', 'email', 'phone', 'notes']) if (body?.[f] !== undefined) data[f] = text(body[f], f === 'notes' ? 5000 : 200);
|
||||
for (const f of ['name', 'contactName', 'phone', 'notes']) if (body?.[f] !== undefined) data[f] = text(body[f], f === 'notes' ? 5000 : 200);
|
||||
// Hub 2.12.0: one or more addresses (a list, or one value with commas).
|
||||
if (body?.email !== undefined) data.email = emailList(body.email);
|
||||
// AI (Hub 2.0: the church's, for all its installs): the monthly cap and an optional markup.
|
||||
if (body?.aiMonthlyCapCents !== undefined) {
|
||||
const n = parseInt(body.aiMonthlyCapCents, 10);
|
||||
@@ -163,6 +169,39 @@ router.put('/customers/:id', wrap(async (req, res) => {
|
||||
res.json(customer);
|
||||
}));
|
||||
|
||||
/**
|
||||
* Hub 2.12.0: archive a customer (left out of the list and pickers) or restore it. Not while it
|
||||
* still has something running or is billed: those would disappear from view.
|
||||
*/
|
||||
async function archiveBlockers(customerId) {
|
||||
const [onServers, sites, customer] = await Promise.all([
|
||||
prisma.deployment.count({ where: { customerId, status: { notIn: ['removed', 'failed'] } } }),
|
||||
prisma.install.count({ where: { customerId, releasedAt: null } }),
|
||||
prisma.customer.findUnique({ where: { id: customerId } }),
|
||||
]);
|
||||
const out = [];
|
||||
if (onServers) out.push(`${onServers} install${onServers === 1 ? ' is' : 's are'} still on a server (uninstall ${onServers === 1 ? 'it' : 'them'}, or remove ${onServers === 1 ? 'it' : 'them'} from the Hub once stale)`);
|
||||
else if (sites) out.push(`${sites} site${sites === 1 ? '' : 's'} still send${sites === 1 ? 's' : ''} heartbeats (release the licence first)`);
|
||||
if (customer && customer.billingCycle !== 'none') out.push('it\'s still billed (set billing to "not invoiced" first)');
|
||||
return out;
|
||||
}
|
||||
|
||||
router.post('/customers/:id/archive', wrap(async (req, res) => {
|
||||
const customer = await prisma.customer.findUnique({ where: { id: req.params.id } });
|
||||
if (!customer) throw bad('Customer not found', 404);
|
||||
const blockers = await archiveBlockers(customer.id);
|
||||
if (blockers.length) throw bad(`${customer.name} can't be archived yet: ${blockers.join('; ')}.`, 409);
|
||||
const updated = await prisma.customer.update({ where: { id: customer.id }, data: { archivedAt: customer.archivedAt || new Date() } });
|
||||
await audit(req, 'customer_archived', customer.id, { name: customer.name });
|
||||
res.json(updated);
|
||||
}));
|
||||
|
||||
router.post('/customers/:id/restore', wrap(async (req, res) => {
|
||||
const customer = await prisma.customer.update({ where: { id: req.params.id }, data: { archivedAt: null } }).catch(() => { throw bad('Customer not found', 404); });
|
||||
await audit(req, 'customer_restored', customer.id, { name: customer.name });
|
||||
res.json(customer);
|
||||
}));
|
||||
|
||||
// ─── Licences ─────────────────────────────────────────────────────────────────
|
||||
|
||||
function licenceData(body, { partial = false } = {}) {
|
||||
|
||||
@@ -15,6 +15,7 @@ const { ymd, day, today } = require('../utils/billing/dates');
|
||||
const { TIERS, PLANS, BUNDLE_PERCENT, STORAGE, ADDRESS } = require('../utils/billing/tiers');
|
||||
const { addMonths } = require('../utils/billing/dates');
|
||||
const { BILLABLE, PRODUCT_NAMES } = require('../utils/products');
|
||||
const { emailList } = require('../utils/emails');
|
||||
|
||||
const router = express.Router();
|
||||
router.use(protect);
|
||||
@@ -298,9 +299,8 @@ router.put('/customers/:id', wrap(async (req, res) => {
|
||||
if (b.foundingNote !== undefined) data.foundingNote = text(b.foundingNote, 500);
|
||||
if (b.billingAddress !== undefined) data.billingAddress = text(b.billingAddress, 500);
|
||||
if (b.billingEmail !== undefined) {
|
||||
const e = text(b.billingEmail, 200);
|
||||
if (e && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)) throw bad('That email address doesn\'t look right.');
|
||||
data.billingEmail = e;
|
||||
// Hub 2.12.0: invoices, statements and receipts go to every address in the list.
|
||||
data.billingEmail = emailList(b.billingEmail);
|
||||
}
|
||||
// The schedule (Hub 2.0: per customer, for all its products).
|
||||
if (b.billingCycle !== undefined) {
|
||||
@@ -359,7 +359,9 @@ router.get('/customers/:id/statement', wrap(async (req, res) => {
|
||||
router.get('/customers/:id/statement.pdf', wrap(async (req, res) => sendPdf(res, await billing.statementPdfFor(req.params.id, req.query.from, req.query.to))));
|
||||
|
||||
router.post('/customers/:id/statement/email', emailLimiter, wrap(async (req, res) => {
|
||||
const out = await billing.emailStatement(req.params.id, req.body || {});
|
||||
const body = req.body || {};
|
||||
// Hub 2.12.0: an override can name several addresses too.
|
||||
const out = await billing.emailStatement(req.params.id, { ...body, email: body.email ? emailList(body.email) : undefined });
|
||||
await audit(req, 'statement_sent', req.params.id, { to: out.to, from: out.statement.from, until: out.statement.to });
|
||||
res.json({ message: `Statement sent to ${out.to}.` });
|
||||
}));
|
||||
|
||||
@@ -198,7 +198,15 @@ router.post('/deployments/link', wrap(async (req, res) => {
|
||||
router.get('/deployments/:id', wrap(async (req, res) => {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: req.params.id }, include: { server: true, customer: { select: { id: true, name: true } }, install: true, jobs: { orderBy: { createdAt: 'desc' }, take: 50 }, exports: { select: { id: true, bytes: true, expiresAt: true, downloadedAt: true, deletedAt: true, createdAt: true } } } });
|
||||
if (!d) throw bad('Not found.', 404);
|
||||
res.json({ deployment: { ...d, server: serverSummary(d.server) }, fastPanel: await jobs.fastPanelText(d), canRemoveRecord: await jobs.neverInstalled(d) });
|
||||
res.json({ deployment: { ...d, server: serverSummary(d.server) }, fastPanel: await jobs.fastPanelText(d), canRemoveRecord: await jobs.neverInstalled(d), stale: jobs.staleness(d), uninstall: await jobs.uninstallPlan(d) });
|
||||
}));
|
||||
|
||||
// Hub 2.12.0: an install that's no longer on its server (no heartbeat for 24 hours), removed from
|
||||
// the Hub only: nothing runs on the server.
|
||||
router.post('/deployments/:id/forget', wrap(async (req, res) => {
|
||||
const d = await jobs.forgetStale(req.params.id, { confirm: req.body?.confirm });
|
||||
await audit(req, 'install_removed_from_hub', d.id, { shortName: d.shortName, product: d.product, server: d.serverId, lastHeardAt: jobs.staleness(d).since });
|
||||
res.json({ ok: true });
|
||||
}));
|
||||
|
||||
// A failed install's record (nothing of it is on the server, so there's nothing to uninstall).
|
||||
@@ -252,7 +260,7 @@ router.post('/deployments/:id/seed-demo', wrap(async (req, res) => {
|
||||
}));
|
||||
|
||||
router.post('/deployments/:id/uninstall', wrap(async (req, res) => {
|
||||
const out = await jobs.scheduleUninstall({ deploymentId: req.params.id, at: req.body?.at, totp: req.body?.totp, confirm: req.body?.confirm }, req.admin);
|
||||
const out = await jobs.scheduleUninstall({ deploymentId: req.params.id, at: req.body?.at, totp: req.body?.totp, confirm: req.body?.confirm, join: typeof req.body?.join === 'boolean' ? req.body.join : undefined, joinJobId: req.body?.joinJobId || undefined }, req.admin);
|
||||
await audit(req, 'uninstall_scheduled', req.params.id, { at: out.uninstallJob.notBefore });
|
||||
res.status(201).json(out);
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user