Files
gancio/app/cron.js

31 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-03-10 01:01:23 +01:00
const mail = require('./mail')
2019-03-11 00:20:37 +01:00
const { Event, Notification, EventNotification, User, Place, Tag } = require('./model')
2019-03-10 01:01:23 +01:00
async function loop () {
2019-03-11 00:20:37 +01:00
// get all event notification in queue
const eventNotifications = await EventNotification.findAll()
const promises = eventNotifications.map(async e => {
2019-03-10 01:01:23 +01:00
const event = await Event.findByPk(e.eventId, { include: [User, Place, Tag] })
if (!event.place) return
2019-03-11 00:20:37 +01:00
const notification = await Notification.findByPk(e.notificationId)
2019-03-10 01:01:23 +01:00
try {
2019-03-11 00:20:37 +01:00
if (notification.type === 'mail') {
await mail.send(notification.email, 'event', { event })
} else if (notification.type === 'mail_admin') {
const admins = await User.findAll({ where: { is_admin: true } })
await Promise.all(admins.map(admin =>
mail.send(admin.email, 'event', { event, to_confirm: true, notification })))
}
2019-03-10 01:01:23 +01:00
} catch (e) {
2019-03-11 00:20:37 +01:00
console.log('CATCH!', e)
2019-03-10 01:01:23 +01:00
return false
}
return e.destroy()
})
return Promise.all(promises)
}
setInterval(loop, 20000)
loop()