diff --git a/backend/src/routes/installApi.js b/backend/src/routes/installApi.js index 599fa99..68f09f3 100644 --- a/backend/src/routes/installApi.js +++ b/backend/src/routes/installApi.js @@ -152,6 +152,9 @@ router.post('/installs/register', registerLimiter, async (req, res) => { ? await prisma.install.update({ where: { id: licence.install.id }, data }) : await prisma.install.create({ data: { ...data, product: licence.product, customerId: licence.customerId, licenceId: licence.id } }); await prisma.installEvent.create({ data: { installId: install.id, type: 'registered', level: 'info', message: `${licence.install ? 'Re-registered' : 'Registered'} from ${url} (version ${install.version || '?'})`, resolvedAt: new Date() } }); + // A site the Hub installed (Servers → New install) is linked to its registration, so its + // update and closing banners reach it (found in the VM test: they never did). + await prisma.deployment.updateMany({ where: { licenceId: licence.id, installId: null }, data: { installId: install.id } }).catch(() => {}); res.status(201).json({ installId: install.id, diff --git a/backend/src/utils/jobs.js b/backend/src/utils/jobs.js index 1484176..ae0a0b8 100644 --- a/backend/src/utils/jobs.js +++ b/backend/src/utils/jobs.js @@ -324,8 +324,17 @@ async function fastPanelText(d) { // ─── Notices (banners) ──────────────────────────────────────────────────────── async function addNotice(deployment, { title, message, endsAt, level = 'maintenance' }, now) { - if (!deployment.installId) return null; - const n = await prisma.notice.create({ data: { title, message, level, allInstalls: false, installIds: [deployment.installId], showBanner: true, startsAt: now, endsAt } }); + let installId = deployment.installId; + // Registered before registration made the link (Hub 2.1.0 builds before the VM test). + if (!installId && deployment.licenceId) { + const install = await prisma.install.findFirst({ where: { licenceId: deployment.licenceId } }); + if (install) { + installId = install.id; + await prisma.deployment.update({ where: { id: deployment.id }, data: { installId } }).catch(() => {}); + } + } + if (!installId) return null; + const n = await prisma.notice.create({ data: { title, message, level, allInstalls: false, installIds: [installId], showBanner: true, startsAt: now, endsAt } }); return n.id; } diff --git a/backend/tests/servers.test.js b/backend/tests/servers.test.js index 46b3175..7b5fc36 100644 --- a/backend/tests/servers.test.js +++ b/backend/tests/servers.test.js @@ -294,3 +294,21 @@ describe('settings for servers (found in the VM test)', () => { expect(await servers.portRange()).toEqual({ from: 21000, to: 21100 }); }); }); + +describe('banners reach a site the Hub installed (found in the VM test)', () => { + test('an update to a deployment registered through its licence gets a banner', async () => { + const { server } = await joinServer(); + await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' }); + await db.release.create({ data: { product: 'events', version: '2.4.0', notes: 'New', releasedAt: NOW } }); + // A Hub install: the deployment knows its licence, not yet its install. + await db.licence.create({ data: { id: 'lic-9', customerId: 'cust-1', product: 'events', key: 'CEH-TEST' } }); + await db.install.create({ data: { id: 'inst-9', customerId: 'cust-1', licenceId: 'lic-9', product: 'events', name: 'Hope', url: 'https://events.hope.test', secret: 'x', version: '2.3.0' } }); + const d = await db.deployment.create({ data: { id: 'dep-9', serverId: server.id, customerId: 'cust-1', licenceId: 'lic-9', product: 'events', shortName: 'hope', domain: 'events.hope.test', webPort: 20000, apiPort: 20001, status: 'live', version: '2.3.0' } }); + const r = await jobs.scheduleUpdate({ deploymentIds: [d.id], version: '2.4.0', at: new Date(NOW.getTime() + 5 * H) }, admin, NOW); + const job = await db.job.findUnique({ where: { id: r.jobs[0].id } }); + expect(job.noticeId).toBeTruthy(); + const notice = await db.notice.findUnique({ where: { id: job.noticeId } }); + expect(notice).toMatchObject({ showBanner: true, installIds: ['inst-9'] }); + expect((await db.deployment.findUnique({ where: { id: d.id } })).installId).toBe('inst-9'); + }); +});