mail notification
This commit is contained in:
23
app/cron.js
23
app/cron.js
@@ -1,20 +1,23 @@
|
||||
const mail = require('./mail')
|
||||
const { Event, Reminder, EventReminder, User, Place, Tag } = require('./model')
|
||||
const { Event, Notification, EventNotification, User, Place, Tag } = require('./model')
|
||||
|
||||
async function loop () {
|
||||
console.log('nel loop')
|
||||
// get all event reminder in queue
|
||||
const eventReminders = await EventReminder.findAll()
|
||||
const promises = eventReminders.map(async e => {
|
||||
// get all event notification in queue
|
||||
const eventNotifications = await EventNotification.findAll()
|
||||
const promises = eventNotifications.map(async e => {
|
||||
const event = await Event.findByPk(e.eventId, { include: [User, Place, Tag] })
|
||||
console.log('EVENT ')
|
||||
console.log(event)
|
||||
if (!event.place) return
|
||||
const reminder = await Reminder.findByPk(e.reminderId)
|
||||
const notification = await Notification.findByPk(e.notificationId)
|
||||
try {
|
||||
await mail.send(reminder.email, 'event', { event })
|
||||
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 })))
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('DENTRO CATCH!', e)
|
||||
console.log('CATCH!', e)
|
||||
return false
|
||||
}
|
||||
return e.destroy()
|
||||
|
||||
Reference in New Issue
Block a user