fix recurrent event update

This commit is contained in:
les 2020-02-10 12:01:22 +01:00
parent e1046b49fc
commit cb02c5ff1a

View file

@ -292,19 +292,32 @@ const eventController = {
return res.sendStatus(403) return res.sendStatus(403)
} }
if (req.file) { const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null
if (event.image_path) { const eventDetails = {
const old_path = path.resolve(config.upload_path, event.image_path) title: body.title,
const old_thumb_path = path.resolve(config.upload_path, 'thumb', event.image_path) // remove html tags
await fs.unlink(old_path, e => console.error(e)) description: helpers.sanitizeHTML(body.description),
await fs.unlink(old_thumb_path, e => console.error(e)) multidate: body.multidate,
} start_datetime: body.start_datetime,
body.image_path = req.file.filename end_datetime: body.end_datetime,
recurrent
} }
body.description = helpers.sanitizeHTML(body.description) if (req.file) {
if (event.image_path && !event.recurrent) {
const old_path = path.resolve(config.upload_path, event.image_path)
const old_thumb_path = path.resolve(config.upload_path, 'thumb', event.image_path)
try {
await fs.unlinkSync(old_path)
await fs.unlinkSync(old_thumb_path)
} catch (e) {
debug(e.toString())
}
}
eventDetails.image_path = req.file.filename
}
await event.update(body) await event.update(eventDetails)
let place let place
try { try {
place = await Place.findOrCreate({ place = await Place.findOrCreate({
@ -323,6 +336,13 @@ const eventController = {
} }
const newEvent = await Event.findByPk(event.id, { include: [Tag, Place] }) const newEvent = await Event.findByPk(event.id, { include: [Tag, Place] })
res.json(newEvent) res.json(newEvent)
// create recurrent instances of event if needed
// without waiting for the task manager
if (event.recurrent) {
eventController._createRecurrent()
}
const notifier = require('../../notifier') const notifier = require('../../notifier')
notifier.notifyEvent('Update', event.id) notifier.notifyEvent('Update', event.id)
}, },
@ -331,14 +351,14 @@ const eventController = {
const event = await Event.findByPk(req.params.id) const event = await Event.findByPk(req.params.id)
// check if event is mine (or user is admin) // check if event is mine (or user is admin)
if (event && (req.user.is_admin || req.user.id === event.userId)) { if (event && (req.user.is_admin || req.user.id === event.userId)) {
if (event.image_path) { if (event.image_path && !event.recurrent) {
const old_path = path.join(config.upload_path, event.image_path) const old_path = path.join(config.upload_path, event.image_path)
const old_thumb_path = path.join(config.upload_path, 'thumb', event.image_path) const old_thumb_path = path.join(config.upload_path, 'thumb', event.image_path)
try { try {
fs.unlinkSync(old_thumb_path) fs.unlinkSync(old_thumb_path)
fs.unlinkSync(old_path) fs.unlinkSync(old_path)
} catch (e) { } catch (e) {
debug(e) debug(e.toString())
} }
} }
const notifier = require('../../notifier') const notifier = require('../../notifier')
@ -447,6 +467,9 @@ const eventController = {
cursor.date(d) cursor.date(d)
} else { } else {
cursor.day(d - 1) cursor.day(d - 1)
if (cursor.isBefore(moment())) {
cursor.day(d - 1 + 7)
}
} }
event.start_datetime = cursor.unix() event.start_datetime = cursor.unix()
event.end_datetime = event.start_datetime + duration event.end_datetime = event.start_datetime + duration