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:
2026-11-23 09:17:35 +02:00
co-authored by Claude Opus 5.5
parent f3bee827e5
commit 3a00271a02
25 changed files with 683 additions and 61 deletions
+41 -2
View File
@@ -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 } = {}) {