diff --git a/pages/add/WhereInput.vue b/pages/add/WhereInput.vue index 7b0e7c8f..66881481 100644 --- a/pages/add/WhereInput.vue +++ b/pages/add/WhereInput.vue @@ -75,6 +75,7 @@ export default { if (typeof p === 'object' && !p.create) { this.place.name = p.name.trim() this.place.address = p.address + this.place.id = p.id this.disableAddress = true } else { // this is a new place this.place.name = p.name || p @@ -83,9 +84,11 @@ export default { const place = this.places.find(p => p.name.toLowerCase() === tmpPlace) if (place) { this.place.name = place.name + this.place.id = place.id this.place.address = place.address this.disableAddress = true } else { + delete this.place.id this.place.address = '' this.disableAddress = false this.$refs.place.blur() diff --git a/pages/add/_edit.vue b/pages/add/_edit.vue index e3cf64d5..13728cba 100644 --- a/pages/add/_edit.vue +++ b/pages/add/_edit.vue @@ -187,12 +187,15 @@ export default { if (this.event.media.length) { formData.append('image', this.event.media[0].image) - formData.append('image_url', this.event.media[0].url) + // formData.append('image_url', this.event.media[0].url) formData.append('image_name', this.event.media[0].name) formData.append('image_focalpoint', this.event.media[0].focalpoint) } formData.append('title', this.event.title) + if (this.event.place.id) { + formData.append('place_id', this.event.place.id) + } formData.append('place_name', this.event.place.name) formData.append('place_address', this.event.place.address) formData.append('description', this.event.description) diff --git a/server/api/controller/event.js b/server/api/controller/event.js index 4e0dd2f6..84219e9d 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -290,7 +290,7 @@ const eventController = { }, async isAnonEventAllowed (_req, res, next) { - if (!res.locals.settings.allow_anon_event) { + if (!res.locals.settings.allow_anon_event && !res.locals.user) { return res.sendStatus(403) } next() @@ -307,11 +307,28 @@ const eventController = { const body = req.body const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null - const required_fields = [ 'title', 'place_name', 'start_datetime'] - const missing_field = required_fields.find(required_field => !body[required_field]) + const required_fields = [ 'title', 'start_datetime'] + let missing_field = required_fields.find(required_field => !body[required_field]) if (missing_field) { - log.warn(`${missing_field} is required`) - return res.status(400).send(`${missing_field} is required`) + log.warn(`${missing_field} required`) + return res.status(400).send(`${missing_field} required`) + } + + // find or create the place + let place + if (body.place_id) { + place = await Place.findByPk(body.place_id) + } else { + place = await Place.findOne({ where: { name: body.place_name.trim() }}) + if (!place) { + if (!body.place_address || !body.place_name) { + return res.status(400).send(`place_id or place_name and place_address required`) + } + place = await Place.create({ + name: body.place_name, + address: body.place_address + }) + } } const eventDetails = { @@ -347,13 +364,6 @@ const eventController = { let event = await Event.create(eventDetails) - const [place] = await Place.findOrCreate({ - where: { name: body.place_name }, - defaults: { - address: body.place_address - } - }) - await event.setPlace(place) // create/assign tags @@ -427,8 +437,9 @@ const eventController = { } } - if (req.file || body.image_url) { - if (body.image_url && /^https?:\/\//.test(body.image_url)) { + // modify associated media only if a new file is uploaded or remote image_url is used + if (req.file || (body.image_url && /^https?:\/\//.test(body.image_url))) { + if (body.image_url) { req.file = await helpers.getImageFromURL(body.image_url) } @@ -441,10 +452,9 @@ const eventController = { name: body.image_name || body.title || '', focalpoint: [parseFloat(focalpoint[0].slice(0, 6)), parseFloat(focalpoint[1].slice(0, 6))] }] - } else { + } else if (!body.image) { eventDetails.media = [] } - await event.update(eventDetails) const [place] = await Place.findOrCreate({ where: { name: body.place_name },