pm2 / cron worker to send reminder
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
const { User, Event, Comment, Tag, Place, MailReminder } = require('../model')
|
||||
const { User, Event, Comment, Tag, Place, Reminder } = require('../model')
|
||||
const moment = require('moment')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
const { Op } = require('sequelize')
|
||||
const lodash = require('lodash')
|
||||
const eventController = {
|
||||
|
||||
async addComment (req, res) {
|
||||
// comment could be added to an event or to another comment
|
||||
let event = await Event.findOne({ where: { activitypub_id: req.body.id } })
|
||||
let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
|
||||
if (!event) {
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: req.body.id }, include: Event })
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
|
||||
event = comment.event
|
||||
}
|
||||
const comment = new Comment(req.body)
|
||||
@@ -23,6 +23,26 @@ const eventController = {
|
||||
res.json({ tags, places })
|
||||
},
|
||||
|
||||
async getReminders (event) {
|
||||
function match (event, filters) {
|
||||
// matches if no filter specified
|
||||
if (!filters.tags.length && !filters.places.length) return true
|
||||
if (filters.tags.length) {
|
||||
const m = lodash.intersection(event.tags.map(t => t.tag), filters.tags)
|
||||
if (m.length > 0) return true
|
||||
}
|
||||
if (filters.places.length) {
|
||||
if (filters.places.find(p => p === event.place.name)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
const reminders = await Reminder.findAll()
|
||||
|
||||
// get reminder that matches with selected event
|
||||
return reminders.filter(reminder => match(event, reminder.filters))
|
||||
},
|
||||
|
||||
async updateTag (req, res) {
|
||||
const tag = await Tag.findByPk(req.body.tag)
|
||||
console.log(tag)
|
||||
@@ -68,8 +88,12 @@ const eventController = {
|
||||
},
|
||||
|
||||
async addReminder (req, res) {
|
||||
await MailReminder.create(req.body.reminder)
|
||||
res.json(200)
|
||||
try {
|
||||
await Reminder.create(req.body)
|
||||
res.sendStatus(200)
|
||||
} catch (e) {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
},
|
||||
async getAll (req, res) {
|
||||
const start = moment().year(req.params.year).month(req.params.month).startOf('month').subtract(1, 'week')
|
||||
@@ -77,9 +101,9 @@ const eventController = {
|
||||
const events = await Event.findAll({
|
||||
where: {
|
||||
is_visible: true,
|
||||
[Sequelize.Op.and]: [
|
||||
{ start_datetime: { [Sequelize.Op.gte]: start } },
|
||||
{ start_datetime: { [Sequelize.Op.lte]: end } }
|
||||
[Op.and]: [
|
||||
{ start_datetime: { [Op.gte]: start } },
|
||||
{ start_datetime: { [Op.lte]: end } }
|
||||
]
|
||||
},
|
||||
order: [['start_datetime', 'ASC']],
|
||||
|
||||
Reference in New Issue
Block a user