Change an open update request to a newer version (1.5.0)
When a site asks again after a newer version came out, the open
update_requested alert moves on to the latest version ("changed from
...") and the alert address is emailed once; the response says
changed: true with previousVersion (protocol §3a). Needs Cross Events
1.47.4 for the button.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ const licenceToken = require('../utils/licenceToken');
|
||||
const { checkSignature } = require('../utils/signing');
|
||||
const { getSettings } = require('../utils/settings');
|
||||
const { compareVersions } = require('../utils/alerts');
|
||||
const { requestMessage, requestedVersionOf, isOutdated } = require('../utils/updateRequests');
|
||||
const { sendMail } = require('../utils/mail');
|
||||
const { releasesForInstall } = require('../utils/releases');
|
||||
const { noticesForInstall } = require('../utils/notices');
|
||||
@@ -176,19 +177,40 @@ router.post('/installs/update-request', updateRequestLimiter, async (req, res) =
|
||||
return fail(res, 409, 'UP_TO_DATE', 'This site already runs the latest version.');
|
||||
}
|
||||
|
||||
const open = await prisma.installEvent.findFirst({ where: { installId: install.id, type: 'update_requested', resolvedAt: null }, orderBy: { createdAt: 'desc' } });
|
||||
if (open) return res.json({ requestedAt: open.createdAt.toISOString(), version: latest, alreadyRequested: true });
|
||||
const customerName = async () => (await prisma.customer.findUnique({ where: { id: install.customerId }, select: { name: true } }))?.name || install.name;
|
||||
const link = `${(process.env.FRONTEND_URL || '').replace(/\/$/, '')}/installs/${install.id}`;
|
||||
|
||||
const event = await prisma.installEvent.create({ data: { installId: install.id, type: 'update_requested', level: 'warning', message: `Update requested: runs ${current}, latest is ${latest}` } });
|
||||
const open = await prisma.installEvent.findFirst({ where: { installId: install.id, type: 'update_requested', resolvedAt: null }, orderBy: { createdAt: 'desc' } });
|
||||
if (open) {
|
||||
// Asked for a version that has since been overtaken: the request moves on to the latest one
|
||||
// (Joshua is told once). Otherwise nothing changes and nobody is emailed again.
|
||||
if (!isOutdated(open.message, latest)) {
|
||||
return res.json({ requestedAt: open.createdAt.toISOString(), version: latest, alreadyRequested: true, changed: false });
|
||||
}
|
||||
const previous = requestedVersionOf(open.message);
|
||||
const now = new Date();
|
||||
await prisma.installEvent.update({ where: { id: open.id }, data: { message: requestMessage(current, latest, previous) } });
|
||||
if (settings.alert_email) {
|
||||
const name = await customerName();
|
||||
await sendMail({
|
||||
to: settings.alert_email,
|
||||
subject: `Cross Events Hub: update request changed by ${name}`,
|
||||
text: `${name} (${install.url}) now asks to be updated from ${current} to ${latest}${previous ? ` instead of ${previous}` : ''}.\n\n${link}`,
|
||||
}).catch(e => console.warn('[update-request] email failed:', e?.message));
|
||||
}
|
||||
return res.json({ requestedAt: now.toISOString(), version: latest, alreadyRequested: true, changed: true, previousVersion: previous });
|
||||
}
|
||||
|
||||
const event = await prisma.installEvent.create({ data: { installId: install.id, type: 'update_requested', level: 'warning', message: requestMessage(current, latest) } });
|
||||
if (settings.alert_email) {
|
||||
const customer = await prisma.customer.findUnique({ where: { id: install.customerId }, select: { name: true } });
|
||||
const name = await customerName();
|
||||
await sendMail({
|
||||
to: settings.alert_email,
|
||||
subject: `Cross Events Hub: update requested by ${customer?.name || install.name}`,
|
||||
text: `${customer?.name || install.name} (${install.url}) would like to be updated from ${current} to ${latest}.\n\n${(process.env.FRONTEND_URL || '').replace(/\/$/, '')}/installs/${install.id}`,
|
||||
subject: `Cross Events Hub: update requested by ${name}`,
|
||||
text: `${name} (${install.url}) would like to be updated from ${current} to ${latest}.\n\n${link}`,
|
||||
}).catch(e => console.warn('[update-request] email failed:', e?.message));
|
||||
}
|
||||
res.status(201).json({ requestedAt: event.createdAt.toISOString(), version: latest, alreadyRequested: false });
|
||||
res.status(201).json({ requestedAt: event.createdAt.toISOString(), version: latest, alreadyRequested: false, changed: false });
|
||||
} catch (e) {
|
||||
console.error('[update-request]', e);
|
||||
fail(res, 500, 'SERVER_ERROR', 'The Hub could not record the update request. Try again later.');
|
||||
|
||||
Reference in New Issue
Block a user