mail notification

This commit is contained in:
lesion
2019-03-11 00:20:37 +01:00
parent 6ed639d94b
commit 9702f93cf9
17 changed files with 95 additions and 59 deletions

View File

@@ -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()