pm2 / cron worker to send reminder

This commit is contained in:
lesion
2019-03-10 01:01:23 +01:00
parent e41de7208c
commit 6ed639d94b
39 changed files with 264 additions and 105 deletions

View File

@@ -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']],

View File

@@ -1,5 +1,5 @@
const { Event, Comment, Tag, Place } = require('../model')
const Sequelize = require('sequelize')
const { Op } = require('sequelize')
const config = require('../config')
const moment = require('moment')
const ics = require('ics')
@@ -21,7 +21,7 @@ const exportController = {
}
const events = await Event.findAll({
order: [['start_datetime', 'ASC']],
where: { start_datetime: { [Sequelize.Op.gte]: yesterday } },
where: { start_datetime: { [Op.gte]: yesterday } },
include: [Comment, {
model: Tag,
where: whereTag

View File

@@ -3,14 +3,16 @@ const Mastodon = require('mastodon-api')
const User = require('../models/user')
const { Event, Tag, Place } = require('../models/event')
const eventController = require('./event')
const config = require('../config')
const mail = require('../mail')
const bot = require('./bot')
const { Op } = require('sequelize')
const userController = {
async login (req, res) {
// find the user
const user = await User.findOne({ where: { email: req.body.email } })
const user = await User.findOne({ where: { email: { [Op.eq]: req.body.email } } })
if (!user) {
res.status(404).json({ success: false, message: 'AUTH_FAIL' })
} else if (user) {
@@ -82,6 +84,7 @@ const userController = {
} catch (e) {
console.log(e)
}
let event = await Event.create(eventDetails)
await event.setPlace(place)
@@ -89,7 +92,7 @@ const userController = {
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: body.tags } })
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
await event.addTags(tags)
}
if (req.user) await req.user.addEvent(event)
@@ -102,7 +105,9 @@ const userController = {
event.save()
}
mail.send(config.admin, 'event', { event })
// insert reminder
const reminders = await eventController.getReminders(event)
await event.setReminders(reminders)
return res.json(event)
},
@@ -121,7 +126,7 @@ const userController = {
await event.update(body)
let place
try {
place = await Place.findOrCreate({ where: { name: body.place_name },
place = await Place.findOrCreate({ where: { name: { [Op.eq]: body.place_name } },
defaults: { address: body.place_address } })
.spread((place, created) => place)
} catch (e) {
@@ -132,7 +137,7 @@ const userController = {
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: body.tags } })
const tags = await Tag.findAll({ where: { tag: { [Op.eq]: body.tags } } })
await event.addTags(tags)
}
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })