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,7 +1,9 @@
const { User, Event, Comment, Tag, Place, Reminder } = require('../model')
const { User, Event, Comment, Tag, Place, Notification } = require('../model')
const moment = require('moment')
const { Op } = require('sequelize')
const lodash = require('lodash')
const crypto = require('crypto')
const eventController = {
async addComment (req, res) {
@@ -23,7 +25,7 @@ const eventController = {
res.json({ tags, places })
},
async getReminders (event) {
async getNotifications (event) {
function match (event, filters) {
// matches if no filter specified
if (!filters.tags.length && !filters.places.length) return true
@@ -37,10 +39,10 @@ const eventController = {
}
}
}
const reminders = await Reminder.findAll()
const notifications = await Notification.findAll()
// get reminder that matches with selected event
return reminders.filter(reminder => match(event, reminder.filters))
// get notification that matches with selected event
return notifications.filter(notification => match(event, notification.filters))
},
async updateTag (req, res) {
@@ -68,6 +70,11 @@ const eventController = {
async confirm (req, res) {
const id = req.params.event_id
const event = await Event.findByPk(id)
// insert notification
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
try {
await event.update({ is_visible: true })
res.send(200)
@@ -87,14 +94,28 @@ const eventController = {
res.json(events)
},
async addReminder (req, res) {
async addNotification (req, res) {
try {
await Reminder.create(req.body)
const notification = req.body
notification.remove_code = crypto.randomBytes(16).toString('hex')
await Notification.create(req.body)
res.sendStatus(200)
} catch (e) {
res.sendStatus(404)
}
},
async delNotification (req, res) {
const remove_code = req.params.code
try {
const notification = await Notification.findOne({ where: { remove_code: { [Op.eq]: remove_code } } })
await notification.destroy()
} catch (e) {
return res.send('Error')
}
res.send('Ok, notification removed')
},
async getAll (req, res) {
const start = moment().year(req.params.year).month(req.params.month).startOf('month').subtract(1, 'week')
const end = moment().year(req.params.year).month(req.params.month).endOf('month').add(1, 'week')

View File

@@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken')
const Mastodon = require('mastodon-api')
const User = require('../models/user')
const { Event, Tag, Place } = require('../models/event')
const { Event, Tag, Place, Notification } = require('../models/event')
const eventController = require('./event')
const config = require('../config')
const mail = require('../mail')
@@ -105,9 +105,14 @@ const userController = {
event.save()
}
// insert reminder
const reminders = await eventController.getReminders(event)
await event.setReminders(reminders)
if (req.user) {
// insert notifications
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
} else {
const notification = await Notification.create({ type: 'admin_email' })
await event.setNotification(notification)
}
return res.json(event)
},