From 9f28a8f30e84fa97991596ff890c49b5f42a9d22 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Fri, 24 Feb 2023 16:24:25 +0100 Subject: [PATCH] Send status e-mails --- index.js | 90 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index f2ce9b7..475cdc0 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ const icons = { const servicesNotifiedAboutOutage = new Set(); let renderedDashboard = null; -async function processOutage({ outage, server, settings, onDateUpdated }) { +async function processOutage({ outage, server, onDateUpdated }) { if (typeof onDateUpdated !== 'function') { onDateUpdated = () => null; } @@ -34,12 +34,6 @@ async function processOutage({ outage, server, settings, onDateUpdated }) { { $set: { lastChecked: new Date() } } ).then(() => onDateUpdated(id)); - // Get service entry - const service = await server - .storage - .store('smartyellow/webservice') - .findOne({ id }); - // Get last heartbeat const heartbeat = await server .storage @@ -187,18 +181,18 @@ module.exports = { description: 'Tags that can be assigned to outage messages to categorise them.', default: {}, }, - // emailSender: { - // type: 'string', - // label: 'notification sender', - // description: 'Sender of notifications about service statuses. Format: Name ', - // default: '', - // }, - // emailRecipient: { - // type: 'array', - // label: 'notification recipients', - // description: 'Recipients of notifications about service statuses. Format: Name ', - // default: [], - // }, + emailSender: { + type: 'string', + label: 'notification sender', + description: 'Sender of notifications about service statuses. Format: Name ', + default: '', + }, + emailRecipients: { + type: 'array', + label: 'e-mail recipients', + description: 'Recipients of e-mail notifications about service statuses. Format: Name ', + default: [], + }, smsRecipients: { type: 'array', label: 'SMS recipients', @@ -412,6 +406,62 @@ module.exports = { }, }, + { id: 'sendEmailOnOutage', + order: 10, + event: 'populateDashboardTiles', + purpose: 'Sends an e-mail when a tile with priority of 1 or higher is rendered', + handler: async ({ tiles }) => { + if ((typeof server.sendEmail !== 'function') || (typeof settings.emailSender !== 'string') || !settings.emailRecipients?.length) { + // Bad configuration or no email extension. + return; + } + + const tilesToNotifyAbout = new Set(); + + for (const tile of tiles) { + if (tile.prio < 1) { + // Tile is not of priority 1 or higher. Remove its ID from servicesNotifiedAboutOutage. + servicesNotifiedAboutOutage.delete(tile.serviceId); + } + else { + // Tile is of sufficient priority. + if (servicesNotifiedAboutOutage.has(tile.serviceId)) { + // Already notified about: do nothing. + } + else { + // Not yet notified about: send email. + servicesNotifiedAboutOutage.add(tile.serviceId); + tilesToNotifyAbout.add(tile); + } + } + } + + if (tilesToNotifyAbout.size > 0) { + server.debug('Sending status e-mails for the following services: ', [ ...tilesToNotifyAbout ].map(t => t.serviceId).join(', ')); + const message = '

The following tiles are updated to have priority 1 or higher:

    ' + + [ ...tilesToNotifyAbout ].map(tile => { + let text = `
  • ${tile.service.name?.en || tile.serviceId}: ${tile.statusText}`; + if (tile.badges?.length > 0) { + text += ' (' + tile.badges.map(String).join(', ') + ')'; + } + text += '.

  • '; + return text; + }).join('') + + '
'; + + settings.emailRecipients.forEach(address => server.sendEmail({ + subject: `[alert] ${tilesToNotifyAbout.size} new tile${tilesToNotifyAbout.size === 1 ? '' : 's'}!`, + sender: settings.emailSender, + to: address, + body: message, + }).catch(err => { + server.error('status: failed to send e-mail'); + server.error(err); + })); + } + }, + }, + { id: 'sendSmsOnOutage', order: 10, event: 'populateDashboardTiles', @@ -450,7 +500,7 @@ module.exports = { to: phoneNumber, msg: 'The following service/s is/are experiencing outage: ' + string, }).catch(err => { - server.error('Failed to send status SMS'); + server.error('status: failed to send SMS'); server.error(err); })); }