fix: maintenance mode not enqeuing notifications

This commit is contained in:
Ben Allfree 2024-01-24 22:16:13 +00:00
parent 197b2f5370
commit bccd1a0d31
2 changed files with 37 additions and 30 deletions

View File

@ -4,42 +4,49 @@ onModelAfterUpdate((e) => {
const newModel = /** @type {models.Record} */ (e.model)
const oldModel = newModel.originalCopy()
const { mkLog, enqueueNotification } = /** @type {Lib} */ (
const { mkLog, enqueueNotification, audit } = /** @type {Lib} */ (
require(`${__hooks}/lib.js`)
)
const log = mkLog(`maintenance-mode`)
// Bail out if we aren't in maintenance mode
const isMaintenance = newModel.get('maintenance')
// log({ isMaintenance })
if (!isMaintenance) return
try {
// Bail out if we aren't in maintenance mode
const isMaintenance = newModel.get('maintenance')
// log({ isMaintenance })
if (!isMaintenance) return
// Bail out if the maintenance mode flag has not changed
if (isMaintenance === oldModel.get(`maintenance`)) return
// Bail out if the maintenance mode flag has not changed
if (isMaintenance === oldModel.get(`maintenance`)) return
log(`switched`)
const uid = newModel.get(`uid`)
const user = dao.findRecordById('users', uid)
log(`switched`)
const uid = newModel.get(`uid`)
const user = dao.findRecordById('users', uid)
// Bail out if the user has notifications disabled globally or for the instance
const shouldNotify =
user.getBool(`notifyMaintenanceMode`) &&
newModel.getBool(`notifyMaintenanceMode`)
log({ shouldNotify })
if (!shouldNotify) return
// Bail out if the user has notifications disabled globally or for the instance
const shouldNotify =
user.getBool(`notifyMaintenanceMode`) &&
newModel.getBool(`notifyMaintenanceMode`)
log({ shouldNotify })
if (!shouldNotify) return
// Send the email warning about maintenance mode
const instanceId = newModel.getId()
const subdomain = newModel.getString(`subdomain`)
const address = user.getString(`email`)
log({ instanceId, subdomain, address })
enqueueNotification(`email`, `maintenance_mode`, uid, {
log,
dao,
message_template_vars: {
subdomain,
instanceId,
},
})
// Send the email warning about maintenance mode
const instanceId = newModel.getId()
const subdomain = newModel.getString(`subdomain`)
const address = user.getString(`email`)
log({ instanceId, subdomain, address })
enqueueNotification(`email`, `maintenance-mode`, uid, {
log,
dao,
message_template_vars: {
subdomain,
instanceId,
},
})
} catch (e) {
audit('ERROR', `Failed to enqueue template with ${e}`, {
log,
dao,
})
}
}, 'instances')

View File

@ -24,7 +24,7 @@ interface Lib {
enqueueNotification: (
channel: 'email' | 'lemonbot',
template:
| 'maintenance_mode'
| 'maintenance-mode'
| 'lemon_order_email'
| 'lemon_order_discord'
| 'welcome',