s/cohort/collection

This commit is contained in:
lesion
2022-06-18 01:10:27 +02:00
parent c660bd8eb4
commit da0216a606
8 changed files with 157 additions and 133 deletions

View File

@@ -1,38 +1,42 @@
const Cohort = require('../models/cohort')
const Collection = require('../models/collection')
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')
const { col: Col } = require('../../helpers')
const { Op, Sequelize } = require('sequelize')
const cohortController = {
const collectionController = {
async getAll (req, res) {
const withFilters = req.query.withFilters
let cohorts
let collections
if (withFilters) {
cohorts = await Cohort.findAll({ include: [Filter] })
collections = await Collection.findAll({ include: [Filter] })
} else {
cohorts = await Cohort.findAll()
collections = await Collection.findAll()
}
return res.json(cohorts)
return res.json(collections)
},
// return events from cohort
// return events from collection
async getEvents (req, res) {
const format = req.params.format || 'json'
const name = req.params.name
const cohort = await Cohort.findOne({ where: { name } })
if (!cohort) {
const collection = await Collection.findOne({ where: { name } })
if (!collection) {
return res.sendStatus(404)
}
const filters = await Filter.findAll({ where: { cohortId: cohort.id } })
const filters = await Filter.findAll({ where: { collectionId: collection.id } })
if (!filters.length) {
return res.json([])
}
const start = dayjs().unix()
const where = {
// do not include parent recurrent event
@@ -42,24 +46,16 @@ const cohortController = {
is_visible: true,
// [Op.or]: {
start_datetime: { [Op.gte]: start },
start_datetime: { [Op.gte]: start },
// end_datetime: { [Op.gte]: start }
// }
}
// if (!show_recurrent) {
// where.parentId = null
// }
// if (end) {
// where.start_datetime = { [Op.lte]: end }
// }
const replacements = []
const ors = []
filters.forEach(f => {
if (f.tags && f.tags.length) {
const tags = Sequelize.fn('EXISTS', Sequelize.literal('SELECT 1 FROM event_tags WHERE "event_tags"."eventId"="event".id AND "tagTag" in (?)'))
const tags = Sequelize.fn('EXISTS', Sequelize.literal(`SELECT 1 FROM event_tags WHERE ${Col('event_tags.eventId')}=event.id AND ${Col('tagTag')} in (?)`))
replacements.push(f.tags)
if (f.places && f.places.length) {
ors.push({ [Op.and]: [ { placeId: f.places.map(p => p.id) },tags] })
@@ -71,21 +67,7 @@ const cohortController = {
}
})
// if (tags && places) {
// where[Op.or] = {
// placeId: places ? places.split(',') : [],
// // '$tags.tag$': Sequelize.literal(`EXISTS (SELECT 1 FROM event_tags WHERE tagTag in ( ${Sequelize.QueryInterface.escape(tags)} ) )`)
// }
// } else if (tags) {
// where[Op.and] = Sequelize.literal(`EXISTS (SELECT 1 FROM event_tags WHERE event_tags.eventId=event.id AND tagTag in (?))`)
// replacements.push(tags)
// } else if (places) {
// where.placeId = places.split(',')
// }
if (ors.length) {
where[Op.or] = ors
}
where[Op.and] = { [Op.or]: ors }
const events = await Event.findAll({
where,
@@ -96,7 +78,7 @@ const cohortController = {
include: [
{
model: Tag,
order: [Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE tagTag = tag) DESC')],
// order: [Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE tagTag = tag) DESC')],
attributes: ['tag'],
through: { attributes: [] }
},
@@ -120,43 +102,43 @@ const cohortController = {
},
async add (req, res) {
const cohortDetail = {
const collectionDetail = {
name: req.body.name,
isActor: true,
isTop: true
}
// TODO: validation
log.info('Create cohort: ' + req.body.name)
const cohort = await Cohort.create(cohortDetail)
res.json(cohort)
log.info('Create collection: ' + req.body.name)
const collection = await Collection.create(collectionDetail)
res.json(collection)
},
async remove (req, res) {
const cohort_id = req.params.id
log.info('Remove cohort', cohort_id)
const collection_id = req.params.id
log.info('Remove collection', collection_id)
try {
const cohort = await Cohort.findByPk(cohort_id)
await cohort.destroy()
const collection = await Collection.findByPk(collection_id)
await collection.destroy()
res.sendStatus(200)
} catch (e) {
log.error('Remove cohort failed:', e)
log.error('Remove collection failed:', e)
res.sendStatus(404)
}
},
async getFilters (req, res) {
const cohortId = req.params.cohort_id
const filters = await Filter.findAll({ where: { cohortId } })
const collectionId = req.params.collection_id
const filters = await Filter.findAll({ where: { collectionId } })
return res.json(filters)
},
async addFilter (req, res) {
const cohortId = req.body.cohortId
const collectionId = req.body.collectionId
const tags = req.body.tags
const places = req.body.places
try {
const filter = await Filter.create({ cohortId, tags, places })
const filter = await Filter.create({ collectionId, tags, places })
return res.json(filter)
} catch (e) {
log.error(String(e))
@@ -183,4 +165,4 @@ const cohortController = {
module.exports = cohortController
module.exports = collectionController