Releases with what's new, notices to churches, withdrawn update requests
- Releases page: each Cross Events version with notes and an urgent flag; the newest is the latest version (moved off Settings; the old setting becomes the first release). Installs get the notes for every newer version. - Notices page: a banner for site admins and supervisors (with a time window), an email to the customer and admin contact, or both; for every install or chosen ones. - POST /api/installs/update-request/cancel resolves the request and emails Joshua. - Protocol doc: releases and notices in the heartbeat/register replies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -89,7 +89,7 @@ async function runAlerts(now = new Date()) {
|
||||
for (const install of installs) {
|
||||
const current = problems(install, settings, now);
|
||||
// Update requests are opened by the install and resolved by its heartbeat, not by these checks.
|
||||
const openByType = new Map(install.events.filter(e => !['registered', 'version', 'update_requested'].includes(e.type)).map(e => [e.type, e]));
|
||||
const openByType = new Map(install.events.filter(e => !['registered', 'version', 'update_requested', 'update_request_withdrawn'].includes(e.type)).map(e => [e.type, e]));
|
||||
for (const p of current) {
|
||||
if (openByType.has(p.type)) continue;
|
||||
await prisma.installEvent.create({ data: { installId: install.id, type: p.type, level: p.level, message: p.message, createdAt: now } });
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Notices from Joshua to churches (Notices page): a banner on the site for site admins and
|
||||
* supervisors, sent with every heartbeat until it ends, and/or one email to the customer and the
|
||||
* install's admin contact.
|
||||
*/
|
||||
|
||||
const prisma = require('../config/db');
|
||||
const { sendMail } = require('./mail');
|
||||
|
||||
function forInstall(notice, installId) {
|
||||
return notice.allInstalls || (notice.installIds || []).includes(installId);
|
||||
}
|
||||
|
||||
/**
|
||||
* The banners an install is sent (§3): for it, banner on, and not ended. Ones that start later are
|
||||
* included so the site shows them on time without waiting for a heartbeat; the site checks the dates.
|
||||
*/
|
||||
function noticesFor(notices, installId, now = new Date()) {
|
||||
return notices
|
||||
.filter(n => n.showBanner && forInstall(n, installId) && (!n.endsAt || new Date(n.endsAt) > now))
|
||||
.map(n => ({
|
||||
id: n.id,
|
||||
level: n.level,
|
||||
title: n.title,
|
||||
message: n.message,
|
||||
startsAt: n.startsAt ? new Date(n.startsAt).toISOString() : null,
|
||||
endsAt: n.endsAt ? new Date(n.endsAt).toISOString() : null,
|
||||
updatedAt: new Date(n.updatedAt).toISOString(),
|
||||
}));
|
||||
}
|
||||
|
||||
async function noticesForInstall(installId, now = new Date()) {
|
||||
const notices = await prisma.notice.findMany({
|
||||
where: { showBanner: true, OR: [{ endsAt: null }, { endsAt: { gt: now } }] },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return noticesFor(notices, installId, now);
|
||||
}
|
||||
|
||||
/** Who a notice is emailed to: each install's customer email and admin contact, once each. */
|
||||
function recipients(installs) {
|
||||
const seen = new Map();
|
||||
for (const install of installs) {
|
||||
for (const email of [install.customer?.email, install.adminContact?.email]) {
|
||||
const e = String(email || '').trim();
|
||||
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e) && !seen.has(e.toLowerCase())) seen.set(e.toLowerCase(), e);
|
||||
}
|
||||
}
|
||||
return [...seen.values()];
|
||||
}
|
||||
|
||||
const when = d => new Date(d).toLocaleString('en-ZA', { timeZone: 'Africa/Johannesburg', day: 'numeric', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit' });
|
||||
|
||||
function emailText(notice) {
|
||||
const lines = [notice.message.trim()];
|
||||
if (notice.startsAt || notice.endsAt) {
|
||||
lines.push('', [notice.startsAt && `From: ${when(notice.startsAt)}`, notice.endsAt && `Until: ${when(notice.endsAt)}`].filter(Boolean).join('\n'));
|
||||
}
|
||||
lines.push('', '— Cross Events');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Email a notice to the installs it's for (one email per address, so churches don't see each
|
||||
* other). Returns how many were sent.
|
||||
*/
|
||||
async function emailNotice(notice) {
|
||||
const installs = await prisma.install.findMany({
|
||||
where: { releasedAt: null, ...(notice.allInstalls ? {} : { id: { in: notice.installIds } }) },
|
||||
include: { customer: { select: { email: true } } },
|
||||
});
|
||||
let sent = 0;
|
||||
for (const to of recipients(installs)) {
|
||||
try {
|
||||
await sendMail({ to, subject: `Cross Events: ${notice.title}`, text: emailText(notice) });
|
||||
sent++;
|
||||
} catch (e) {
|
||||
console.warn(`[notices] email to ${to} failed:`, e?.message);
|
||||
}
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
module.exports = { noticesFor, noticesForInstall, recipients, emailText, emailNotice };
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Cross Events releases and their notes (Releases page). The highest version is the latest one,
|
||||
* kept in the latest_version setting so alerts and the install API read it as before.
|
||||
*/
|
||||
|
||||
const prisma = require('../config/db');
|
||||
const { compareVersions } = require('./alerts');
|
||||
const { setSettings } = require('./settings');
|
||||
|
||||
const VERSION_RE = /^\d+\.\d+\.\d+$/;
|
||||
const MAX_SENT = 20;
|
||||
|
||||
/** Newest first. */
|
||||
function sortReleases(releases) {
|
||||
return [...releases].sort((a, b) => compareVersions(b.version, a.version));
|
||||
}
|
||||
|
||||
/**
|
||||
* What an install on `version` is told (§3): every release newer than it, newest first — at most
|
||||
* 20, so a long-neglected install still gets a reply of sensible size.
|
||||
*/
|
||||
function releasesFor(releases, version) {
|
||||
return sortReleases(releases)
|
||||
.filter(r => !version || compareVersions(r.version, version) > 0)
|
||||
.slice(0, MAX_SENT)
|
||||
.map(r => ({ version: r.version, releasedAt: new Date(r.releasedAt).toISOString(), urgent: !!r.urgent, notes: r.notes || '' }));
|
||||
}
|
||||
|
||||
/** Point latest_version at the highest release (blank when there are none). */
|
||||
async function syncLatestVersion() {
|
||||
const releases = await prisma.release.findMany({ select: { version: true } });
|
||||
const latest = sortReleases(releases)[0]?.version || '';
|
||||
await setSettings({ latest_version: latest });
|
||||
return latest;
|
||||
}
|
||||
|
||||
async function releasesForInstall(version) {
|
||||
return releasesFor(await prisma.release.findMany(), version);
|
||||
}
|
||||
|
||||
module.exports = { VERSION_RE, sortReleases, releasesFor, releasesForInstall, syncLatestVersion };
|
||||
Reference in New Issue
Block a user