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
+159
View File
@@ -956,3 +956,162 @@ describe("frp's config from the Hub (2.11.0)", () => {
expect(missing.map(p => p.type)).toEqual(['frp_config_missing']);
});
});
describe('uninstalling one product of a church, and stale installs (2.12.0)', () => {
const at = new Date(NOW.getTime() + 5 * H);
const pollAs = (server, agentVersion, when = at) => jobs.poll(server, { agentApiVersion: 1, agentVersion, mode: 'church', runningJobId: null }, when);
async function church({ agentVersion = '1.5.0' } = {}) {
const { server } = await joinServer();
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
await db.server.update({ where: { id: server.id }, data: { agentVersion } });
const evInst = await db.install.create({ data: { id: 'inst-ev', customerId: 'cust-1', product: 'events', name: 'Hope', url: 'https://events.hope.co.za', secret: 'x', lastHeartbeatAt: NOW } });
const accInst = await db.install.create({ data: { id: 'inst-acc', customerId: 'cust-1', product: 'accounts', name: 'Hope', url: 'https://accounts.hope.co.za', secret: 'x', lastHeartbeatAt: NOW } });
const ev = await db.deployment.create({ data: { id: 'dep-ev', serverId: server.id, customerId: 'cust-1', installId: evInst.id, product: 'events', shortName: 'hope', domain: 'events.hope.co.za', webPort: 20000, apiPort: 20001, status: 'live', liveAt: NOW, createdAt: NOW } });
const acc = await db.deployment.create({ data: { id: 'dep-acc', serverId: server.id, customerId: 'cust-1', installId: accInst.id, product: 'accounts', shortName: 'hope', domain: 'accounts.hope.co.za', webPort: 20002, apiPort: 20003, status: 'live', liveAt: NOW, createdAt: NOW } });
for (const port of [20000, 20001, 20002, 20003]) await db.port.create({ data: { port, serverId: server.id, role: port % 2 ? 'api' : 'web' } });
return { server, ev, acc };
}
async function runBatch(server, agentVersion) {
const exp = (await pollAs(server, agentVersion)).jobs[0];
await jobs.receiveReport(server, signedReport(exp, 'succeeded'), at);
const un = (await pollAs(server, agentVersion)).jobs[0];
await jobs.receiveReport(server, signedReport(un, 'succeeded'), at);
return { exp, un };
}
test('agent 1.5.0: only Events goes, Accounts stays live; then Accounts on its own', async () => {
const { server, ev, acc } = await church();
await jobs.scheduleUninstall({ deploymentId: ev.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW);
const { exp, un } = await runBatch(server, '1.5.0');
expect(exp.args.products).toEqual(['events']);
expect(un.args).toEqual({ church: 'hope', products: ['events'] });
expect(await db.deployment.findUnique({ where: { id: ev.id } })).toMatchObject({ status: 'removed' });
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'live' });
expect((await db.job.findFirst({ where: { type: 'purge', deploymentId: ev.id } })).args).toEqual({ church: 'hope', products: ['events'] });
await jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW);
const second = await runBatch(server, '1.5.0');
expect(second.un.args).toEqual({ church: 'hope', products: ['accounts'] });
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'removed' });
});
test('Accounts goes last: refused while Events stays, and not planned before Events\' uninstall', async () => {
const { ev, acc } = await church();
await expect(jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW)).rejects.toMatchObject({ code: 'ACCOUNTS_IN_USE' });
await jobs.scheduleUninstall({ deploymentId: ev.id, totp: totpNow(), confirm: 'hope', at: new Date(at.getTime() + H) }, admin, NOW);
await expect(jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at, join: false }, admin, NOW)).rejects.toMatchObject({ code: 'ACCOUNTS_FIRST' });
await jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at: new Date(at.getTime() + 2 * H), join: false }, admin, NOW);
});
test('an older agent would remove the whole church: refused while it has other products', async () => {
const { ev } = await church({ agentVersion: '1.4.0' });
await expect(jobs.scheduleUninstall({ deploymentId: ev.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW)).rejects.toMatchObject({ code: 'AGENT_TOO_OLD' });
const plan = await jobs.uninstallPlan(await db.deployment.findUnique({ where: { id: ev.id }, include: { server: true } }));
expect(plan).toMatchObject({ onlyThisProduct: false, others: [{ product: 'accounts' }] });
});
test('a whole-church uninstall (no products) removes every product of it here, and cancels what was planned for them', async () => {
const { server, ev, acc } = await church({ agentVersion: '1.4.0' });
// As from Hub 2.11.0: an uninstall without products, and Accounts' own batch planned after it.
const un = await jobs.makeJob(db, { serverId: server.id, type: 'uninstall', deploymentId: ev.id, args: { church: 'hope' }, notBefore: at });
const later = await jobs.makeJob(db, { serverId: server.id, type: 'export', deploymentId: acc.id, status: 'scheduled', args: { church: 'hope', exportId: 'exp_abcdefgh' }, notBefore: new Date(at.getTime() + H) });
const job = (await pollAs(server, '1.4.0')).jobs[0];
expect(job.id).toBe(un.id);
await jobs.receiveReport(server, signedReport(job, 'succeeded'), at);
expect(await db.deployment.findUnique({ where: { id: ev.id } })).toMatchObject({ status: 'removed' });
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'removed' });
expect((await db.install.findUnique({ where: { id: 'inst-acc' } })).releasedAt).toEqual(at);
expect(await db.job.findUnique({ where: { id: later.id } })).toMatchObject({ status: 'cancelled' });
expect((await db.job.findFirst({ where: { type: 'purge', deploymentId: ev.id } })).args).toEqual({ church: 'hope' });
});
test('an install without a heartbeat for 24 hours can be removed from the Hub; a fresh one can\'t', async () => {
const { server, acc } = await church();
const later = new Date(NOW.getTime() + 23 * H);
await expect(jobs.forgetStale(acc.id, { confirm: 'hope' }, later)).rejects.toMatchObject({ code: 'NOT_STALE' });
const dayLater = new Date(NOW.getTime() + 25 * H);
await expect(jobs.forgetStale(acc.id, { confirm: 'nope' }, dayLater)).rejects.toMatchObject({ code: 'CONFIRM' });
const planned = await jobs.makeJob(db, { serverId: server.id, type: 'env-read', deploymentId: acc.id, args: { church: 'hope', product: 'accounts' }, status: 'scheduled', notBefore: dayLater });
await jobs.forgetStale(acc.id, { confirm: 'hope' }, dayLater);
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'removed' });
expect((await db.install.findUnique({ where: { id: 'inst-acc' } })).releasedAt).toEqual(dayLater);
expect(await db.job.findUnique({ where: { id: planned.id } })).toMatchObject({ status: 'cancelled' });
expect((await db.port.findMany({ where: { port: { in: [20002, 20003] } } })).every(p => p.releasedAt)).toBe(true);
});
});
describe('a second product while another is being uninstalled (2.12.0)', () => {
const at = new Date(NOW.getTime() + 5 * H);
const poll = (server, when = at) => jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.5.0', mode: 'church', runningJobId: null }, when);
async function church() {
const { server } = await joinServer();
await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' });
const ev = await db.deployment.create({ data: { id: 'dep-ev', serverId: server.id, customerId: 'cust-1', product: 'events', shortName: 'hope', domain: 'events.hope.co.za', webPort: 20000, apiPort: 20001, status: 'live', liveAt: NOW, createdAt: NOW } });
const acc = await db.deployment.create({ data: { id: 'dep-acc', serverId: server.id, customerId: 'cust-1', product: 'accounts', shortName: 'hope', domain: 'accounts.hope.co.za', webPort: 20002, apiPort: 20003, status: 'live', liveAt: NOW, createdAt: NOW } });
await db.server.update({ where: { id: server.id }, data: { agentVersion: '1.5.0' } });
await jobs.scheduleUninstall({ deploymentId: ev.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW);
return { server, ev, acc };
}
test('the Hub asks: join that run, or a time of its own', async () => {
const { acc } = await church();
await expect(jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at }, admin, NOW)).rejects.toMatchObject({ code: 'CHOOSE' });
const plan = await jobs.uninstallPlan(await db.deployment.findUnique({ where: { id: acc.id }, include: { server: true } }));
expect(plan).toMatchObject({ lastProduct: true, pending: [{ products: ['events'], joinable: true }] });
});
test('joined: one export and one uninstall for both, which removes the whole church', async () => {
const { server, ev, acc } = await church();
const out = await jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', join: true }, admin, NOW);
expect(out.joined).toBe(true);
expect(out.uninstallJob.notBefore).toEqual(at);
expect(await db.job.count({ where: { type: 'uninstall' } })).toBe(1);
const exp = (await poll(server)).jobs[0];
expect(exp.args.products).toEqual(['accounts', 'events']);
await jobs.receiveReport(server, signedReport(exp, 'succeeded'), at);
const un = (await poll(server)).jobs[0];
expect(un.args).toEqual({ church: 'hope', products: ['accounts', 'events'] });
await jobs.receiveReport(server, signedReport(un, 'succeeded', { result: { products: ['accounts', 'events'], remaining: [] } }), at);
expect(await db.deployment.findUnique({ where: { id: ev.id } })).toMatchObject({ status: 'removed' });
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'removed' });
});
test('joined, and the export fails: both are live again', async () => {
const { server, ev, acc } = await church();
await jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', join: true }, admin, NOW);
const exp = (await poll(server)).jobs[0];
await jobs.receiveReport(server, signedReport(exp, 'failed'), at);
expect(await db.deployment.findUnique({ where: { id: ev.id } })).toMatchObject({ status: 'live' });
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'live' });
});
test('a run less than 4 hours away can\'t be joined (the church needs notice)', async () => {
const { acc } = await church();
await expect(jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', join: true }, admin, new Date(at.getTime() - 2 * H))).rejects.toMatchObject({ code: 'TOO_SOON' });
});
test('its own, later time: the last product left takes the whole church with it', async () => {
const { server, ev, acc } = await church();
const later = new Date(at.getTime() + H);
await jobs.scheduleUninstall({ deploymentId: acc.id, totp: totpNow(), confirm: 'hope', at: later, join: false }, admin, NOW);
expect(await db.job.count({ where: { type: 'uninstall' } })).toBe(2);
// 10:00: Events only; the church keeps Accounts.
const exp1 = (await poll(server)).jobs[0];
await jobs.receiveReport(server, signedReport(exp1, 'succeeded'), at);
const un1 = (await poll(server)).jobs[0];
expect(un1.args.products).toEqual(['events']);
await jobs.receiveReport(server, signedReport(un1, 'succeeded', { result: { products: ['events'], remaining: ['accounts'] } }), at);
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'uninstalling' });
// 11:00: Accounts, the last one: the script removes the church (remaining is empty).
const exp2 = (await poll(server, later)).jobs[0];
await jobs.receiveReport(server, signedReport(exp2, 'succeeded'), later);
const un2 = (await poll(server, later)).jobs[0];
expect(un2.args.products).toEqual(['accounts']);
await jobs.receiveReport(server, signedReport(un2, 'succeeded', { result: { products: ['accounts'], remaining: [] } }), later);
expect(await db.deployment.findUnique({ where: { id: acc.id } })).toMatchObject({ status: 'removed' });
expect(mockMails.some(m => m.subject.includes('Uninstalled: hope') && m.text.includes('folder and app user'))).toBe(true);
});
});