2019-06-06 23:54:32 +02:00
|
|
|
const { event: Event, place: Place } = require('../models')
|
2019-04-03 00:25:12 +02:00
|
|
|
const { Op } = require('sequelize')
|
|
|
|
const moment = require('moment')
|
|
|
|
const ics = require('ics')
|
|
|
|
|
|
|
|
const exportController = {
|
|
|
|
|
2019-06-07 17:02:33 +02:00
|
|
|
async export(req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const type = req.params.type
|
|
|
|
const tags = req.query.tags
|
|
|
|
const places = req.query.places
|
|
|
|
const whereTag = {}
|
|
|
|
const wherePlace = {}
|
|
|
|
const yesterday = moment().subtract('1', 'day')
|
|
|
|
if (tags) {
|
|
|
|
whereTag.tag = tags.split(',')
|
|
|
|
}
|
|
|
|
if (places) {
|
2019-05-30 12:12:51 +02:00
|
|
|
wherePlace.id = places.split(',')
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
const events = await Event.findAll({
|
2019-05-30 12:12:51 +02:00
|
|
|
order: ['start_datetime'],
|
2019-06-07 17:02:33 +02:00
|
|
|
where: {
|
|
|
|
is_visible: true,
|
2019-05-30 12:12:51 +02:00
|
|
|
start_datetime: { [Op.gte]: yesterday },
|
|
|
|
placeId: places.split(',')
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
exclude: ['createdAt', 'updatedAt']
|
|
|
|
},
|
2019-06-07 17:02:33 +02:00
|
|
|
include: [{ model: Place, attributes: ['name', 'id', 'address', 'weigth'] }]
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
|
|
|
switch (type) {
|
|
|
|
case 'feed':
|
|
|
|
return exportController.feed(res, events.slice(0, 20))
|
|
|
|
case 'ics':
|
|
|
|
return exportController.ics(res, events)
|
2019-04-29 00:27:29 +02:00
|
|
|
case 'json':
|
|
|
|
return res.json(events)
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-06-07 17:02:33 +02:00
|
|
|
feed(res, events) {
|
2019-04-03 00:25:12 +02:00
|
|
|
res.type('application/rss+xml; charset=UTF-8')
|
2019-06-10 00:40:37 +02:00
|
|
|
res.render('feed/rss.pug', { events, config: process.env, moment })
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-06-07 17:02:33 +02:00
|
|
|
ics(res, events) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const eventsMap = events.map(e => {
|
|
|
|
const tmpStart = moment(e.start_datetime)
|
|
|
|
const tmpEnd = moment(e.end_datetime)
|
|
|
|
const start = [tmpStart.year(), tmpStart.month() + 1, tmpStart.date(), tmpStart.hour(), tmpStart.minute()]
|
|
|
|
const end = [tmpEnd.year(), tmpEnd.month() + 1, tmpEnd.date(), tmpEnd.hour(), tmpEnd.minute()]
|
|
|
|
return {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
title: e.title,
|
|
|
|
description: e.description,
|
|
|
|
location: e.place.name + ' ' + e.place.address
|
|
|
|
}
|
|
|
|
})
|
|
|
|
res.type('text/calendar; charset=UTF-8')
|
|
|
|
const { error, value } = ics.createEvents(eventsMap)
|
|
|
|
res.send(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exportController
|