add auditing to welcome message

This commit is contained in:
Ben Allfree
2024-01-10 16:38:28 +00:00
parent 41a0749fcd
commit 2bf2ba65d5
2 changed files with 18 additions and 11 deletions

View File

@@ -4,22 +4,28 @@ onModelBeforeUpdate((e) => {
const newModel = /** @type {models.Record} */ (e.model)
const oldModel = newModel.originalCopy()
const { mkLog, enqueueNotification } = /** @type {Lib} */ (
const { audit, mkLog, enqueueNotification } = /** @type {Lib} */ (
require(`${__hooks}/lib.js`)
)
const log = mkLog(`user-welcome-msg`)
try {
log({ newModel, oldModel })
// Bail out if we aren't verified
const isVerified = newModel.getBool('verified')
if (!isVerified) return
// Bail out if we aren't verified
const isVerified = newModel.getBool('verified')
if (!isVerified) return
// Bail out if the verified mode flag has not changed
if (isVerified === oldModel.getBool(`verified`)) return
// Bail out if the verified mode flag has not changed
if (isVerified === oldModel.getBool(`verified`)) return
log(`user just became verified`)
const uid = newModel.getId()
log(`user just became verified`)
const uid = newModel.getId()
enqueueNotification(`email`, `welcome`, uid)
newModel.set(`welcome`, new DateTime())
enqueueNotification(`email`, `welcome`, uid)
newModel.set(`welcome`, new DateTime())
} catch (e) {
audit(`ERROR`, `${e}`)
throw e
}
}, 'users')

View File

@@ -3,6 +3,7 @@ type Logger = (...args: any[]) => void
type StringKvLookup = { [_: string]: string }
type AuditEvents =
| 'ERROR'
| 'NOTIFICATION_ERR'
| 'LS'
| 'LS_ERR'
@@ -17,7 +18,7 @@ interface Lib {
mkLog: (namespace: string) => Logger
processNotification: (
notificationRec: models.Record,
context: { log: Logger; test: boolean },
context: { log: Logger; test?: boolean },
) => void
enqueueNotification: (
channel: 'email' | 'lemonbot',