-
Notifications
You must be signed in to change notification settings - Fork 10
/
notification-sender.js
53 lines (51 loc) · 1.84 KB
/
notification-sender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import emailClient from '@aragonone/court-backend-shared/helpers/email-client'
import { UserNotification } from '@aragonone/court-backend-server/build/models/objection'
import * as notificationScanners from '../models/notification-scanners'
import { accountData } from '../../../../emails/helpers'
/**
* This worker loops over all unprocessed notification DB entries
* and sends an associated email
*/
export default async function (ctx) {
const notifications = await UserNotification.findUnsent()
for (const notification of notifications) {
await trySendNotification(ctx, notification)
}
}
export async function trySendNotification(ctx, notification) {
const { logger, metrics } = ctx
if (notification.sentAt != null) return
notification = await notification.$fetchGraph('[user.email, type]')
const { user, type: { model } } = notification
const scanner = notificationScanners[model]
if (!scanner) {
logger.error(`Notification scanner ${model} not found.`)
return
}
if (!await scanner.shouldNotifyUser(user)) {
logger.warn(`Deleting stale notification type ${model} for user ${user.address}`)
await notification.$query().del()
return
}
let TemplateModel = notification.details.emailTemplateModel ?? {}
TemplateModel = {
...TemplateModel,
...accountData(user.address),
date: notification.createdAtDateString
}
const message = {
To: user.email.email,
TemplateAlias: scanner.emailTemplateAlias,
TemplateModel,
}
try {
await emailClient.sendEmailWithTemplate(message)
await notification.$query().update({sentAt: new Date()})
logger.success(`Notification type ${model} sent for user ${user.address}`)
metrics.notificationSent(model)
}
catch (error) {
metrics.workerError()
logger.error(`Could not send notification type ${model} for user ${user.address}`, error)
}
}