gancio-upstream/server/api/controller/collection.js

175 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-06-18 01:10:27 +02:00
const Collection = require('../models/collection')
2022-05-20 13:04:07 +02:00
const Filter = require('../models/filter')
const Event = require('../models/event')
const Tag = require('../models/tag')
const Place = require('../models/place')
const log = require('../../log')
const dayjs = require('dayjs')
2022-06-18 01:10:27 +02:00
const { col: Col } = require('../../helpers')
2022-05-25 10:55:39 +02:00
const { Op, Sequelize } = require('sequelize')
2022-05-20 13:04:07 +02:00
2022-06-18 01:10:27 +02:00
const collectionController = {
2022-05-20 13:04:07 +02:00
async getAll (req, res) {
2022-05-25 10:55:39 +02:00
const withFilters = req.query.withFilters
2022-06-18 01:10:27 +02:00
let collections
2022-05-25 10:55:39 +02:00
if (withFilters) {
collections = await Collection.findAll({ include: [ Filter ] })
2022-05-25 10:55:39 +02:00
} else {
2022-06-18 01:10:27 +02:00
collections = await Collection.findAll()
2022-05-25 10:55:39 +02:00
}
2022-06-18 01:10:27 +02:00
return res.json(collections)
2022-05-20 13:04:07 +02:00
},
2022-06-18 01:10:27 +02:00
// return events from collection
2022-05-20 13:04:07 +02:00
async getEvents (req, res) {
2022-06-18 01:10:27 +02:00
const format = req.params.format || 'json'
2022-05-20 13:04:07 +02:00
const name = req.params.name
2022-06-18 01:10:27 +02:00
const collection = await Collection.findOne({ where: { name } })
if (!collection) {
2022-05-25 10:55:39 +02:00
return res.sendStatus(404)
}
2022-06-18 01:10:27 +02:00
const filters = await Filter.findAll({ where: { collectionId: collection.id } })
2022-05-20 13:04:07 +02:00
2022-06-18 01:10:27 +02:00
if (!filters.length) {
return res.json([])
}
2022-05-20 13:04:07 +02:00
const start = dayjs().unix()
const where = {
// do not include parent recurrent event
recurrent: null,
// confirmed event only
is_visible: true,
// [Op.or]: {
2022-06-18 01:10:27 +02:00
start_datetime: { [Op.gte]: start },
2022-05-20 13:04:07 +02:00
// end_datetime: { [Op.gte]: start }
// }
}
const replacements = []
const ors = []
filters.forEach(f => {
if (f.tags && f.tags.length) {
2022-06-18 01:10:27 +02:00
const tags = Sequelize.fn('EXISTS', Sequelize.literal(`SELECT 1 FROM event_tags WHERE ${Col('event_tags.eventId')}=event.id AND ${Col('tagTag')} in (?)`))
2022-05-20 13:04:07 +02:00
replacements.push(f.tags)
if (f.places && f.places.length) {
ors.push({ [Op.and]: [ { placeId: f.places.map(p => p.id) },tags] })
} else {
ors.push(tags)
}
} else if (f.places && f.places.length) {
ors.push({ placeId: f.places.map(p => p.id) })
}
})
2022-06-18 01:10:27 +02:00
where[Op.and] = { [Op.or]: ors }
2022-05-20 13:04:07 +02:00
const events = await Event.findAll({
where,
attributes: {
exclude: ['likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources']
},
order: ['start_datetime'],
include: [
{
model: Tag,
2022-06-18 01:10:27 +02:00
// order: [Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE tagTag = tag) DESC')],
2022-05-20 13:04:07 +02:00
attributes: ['tag'],
through: { attributes: [] }
},
{ model: Place, required: true, attributes: ['id', 'name', 'address'] }
],
// limit: max,
replacements
}).catch(e => {
log.error('[EVENT]', e)
return []
})
const ret = events.map(e => {
e = e.get()
e.tags = e.tags ? e.tags.map(t => t && t.tag) : []
return e
})
return res.json(ret)
},
async add (req, res) {
2022-06-18 01:10:27 +02:00
const collectionDetail = {
2022-05-20 13:04:07 +02:00
name: req.body.name,
isActor: true,
isTop: true
}
// TODO: validation
log.info(`Create collection: ${req.body.name}`)
try {
const collection = await Collection.create(collectionDetail)
res.json(collection)
} catch (e) {
log.error(`Create collection failed ${e}`)
res.sendStatus(400)
}
2022-05-20 13:04:07 +02:00
},
async remove (req, res) {
2022-06-18 01:10:27 +02:00
const collection_id = req.params.id
log.info('Remove collection', collection_id)
2022-05-20 13:04:07 +02:00
try {
2022-06-18 01:10:27 +02:00
const collection = await Collection.findByPk(collection_id)
await collection.destroy()
2022-05-20 13:04:07 +02:00
res.sendStatus(200)
} catch (e) {
log.error('Remove collection failed:' + String(e))
2022-05-20 13:04:07 +02:00
res.sendStatus(404)
}
},
async getFilters (req, res) {
2022-06-18 01:10:27 +02:00
const collectionId = req.params.collection_id
const filters = await Filter.findAll({ where: { collectionId } })
2022-05-20 13:04:07 +02:00
return res.json(filters)
},
async addFilter (req, res) {
2022-06-18 01:10:27 +02:00
const collectionId = req.body.collectionId
2022-05-20 13:04:07 +02:00
const tags = req.body.tags
const places = req.body.places
try {
2022-06-18 01:10:27 +02:00
const filter = await Filter.create({ collectionId, tags, places })
2022-05-20 13:04:07 +02:00
return res.json(filter)
} catch (e) {
log.error(String(e))
return res.status(500)
}
},
async removeFilter (req, res) {
const filter_id = req.params.id
log.info(`Remove filter ${filter_id}`)
2022-05-20 13:04:07 +02:00
try {
const filter = await Filter.findByPk(filter_id)
if (!filter) {
return res.sendStatus(404)
}
2022-05-20 13:04:07 +02:00
await filter.destroy()
res.sendStatus(200)
} catch (e) {
log.error('Remove filter failed:', e)
res.sendStatus(404)
}
},
}
2022-06-18 01:10:27 +02:00
module.exports = collectionController