diff --git a/server/api.js b/server/api.js index deaef1d2..955ed4b4 100644 --- a/server/api.js +++ b/server/api.js @@ -2,7 +2,9 @@ const express = require('express') const config = require('./config') const profile = require('./controllers/profile') -const { staffQuery, visitsQuery } = require('./controllers/public') +const staffQuery = require('./controllers/public/staff') +const visitsQuery = require('./controllers/public/visits') +const seminarsQuery = require('./controllers/public/seminars') const router = express.Router() @@ -66,5 +68,9 @@ router.get('/public/visits', async (req, res) => { res.send(await visitsQuery(req)) }) +router.get('/public/seminars', async (req, res) => { + res.send(await seminarsQuery(req)) +}) + module.exports = router module.exports.ModelSchemas = ModelSchemas \ No newline at end of file diff --git a/server/controllers/public/seminars.js b/server/controllers/public/seminars.js new file mode 100644 index 00000000..524b4ac6 --- /dev/null +++ b/server/controllers/public/seminars.js @@ -0,0 +1,47 @@ +const EventSeminar = require('../../models/EventSeminar') + +async function seminarsQuery(req) { + const pipeline = [ + { $match: { + startDatetime: {$gte: new Date()} + }}, + { $lookup: { + from: 'people', + localField: 'speaker', + foreignField: '_id', + as: 'speaker', + }}, + { $unwind: { + path: '$speaker', + preserveNullAndEmptyArrays: true + }}, + {$lookup: { + from: 'institutions', + localField: 'affiliations', + foreignField: '_id', + as: 'affiliations' + }}, + { $project: { + _id: 1, + title: 1, + startDatetime: 1, + conferenceRoom: 1, + category: 1, + duration: 1, + speaker: { + _id: 1, + firstName: 1, + lastName: 1, + affiliations: 1, + }, + }} + ] + + // console.log(JSON.stringify({pipeline})) + + const seminars = await EventSeminar.aggregate(pipeline) + + return seminars +} + +module.exports = seminarsQuery diff --git a/server/controllers/public.js b/server/controllers/public/staff.js similarity index 60% rename from server/controllers/public.js rename to server/controllers/public/staff.js index 25592618..15742ab9 100644 --- a/server/controllers/public.js +++ b/server/controllers/public/staff.js @@ -1,6 +1,4 @@ -const Person = require('../models/Person') -const Visit = require('../models/Visit') -const { personRoomAssignmentPipeline } = require('../models/RoomAssignment') +const Person = require('../../models/Person') async function staffQuery(req) { const matches = [] @@ -98,64 +96,4 @@ async function staffQuery(req) { return staffs[0] } -module.exports.staffQuery = staffQuery - -async function visitsQuery(req) { - const pipeline = [ - { $match: { - startDate: {$lte: new Date()}, - endDate: {$gte: new Date()} - }}, - { $lookup: { - from: 'people', - localField: 'person', - foreignField: '_id', - as: 'person', - }}, - { $unwind: { - path: '$person', - preserveNullAndEmptyArrays: true - }}, - ...personRoomAssignmentPipeline(), - {$lookup: { - from: 'institutions', - localField: 'affiliations', - foreignField: '_id', - as: 'affiliations', - pipeline: [ - {$project: { - _id: 0, - name: 1, - code: 1, - city: 1, - country: 1, - }} - ] - }}, - { $project: { - _id: 0, - startDate: 1, - endDate: 1, - affiliations: 1, - person: { - firstName: 1, - lastName: 1, - }, - roomAssignment: { - room: { - building: 1, - floor: 1, - number: 1, - } - }, - }}, - ] - - // console.log(JSON.stringify({pipeline})) - - const visits = await Visit.aggregate(pipeline) - - return visits -} - -module.exports.visitsQuery = visitsQuery +module.exports = staffQuery diff --git a/server/controllers/public/visits.js b/server/controllers/public/visits.js new file mode 100644 index 00000000..05d4a5e5 --- /dev/null +++ b/server/controllers/public/visits.js @@ -0,0 +1,62 @@ +const Visit = require('../../models/Visit') +const { personRoomAssignmentPipeline } = require('../../models/RoomAssignment') + +async function visitsQuery(req) { + const pipeline = [ + { $match: { + startDate: {$lte: new Date()}, + endDate: {$gte: new Date()} + }}, + { $lookup: { + from: 'people', + localField: 'person', + foreignField: '_id', + as: 'person', + }}, + { $unwind: { + path: '$person', + preserveNullAndEmptyArrays: true + }}, + ...personRoomAssignmentPipeline(), + {$lookup: { + from: 'institutions', + localField: 'affiliations', + foreignField: '_id', + as: 'affiliations', + pipeline: [ + {$project: { + _id: 0, + name: 1, + code: 1, + city: 1, + country: 1, + }} + ] + }}, + { $project: { + _id: 0, + startDate: 1, + endDate: 1, + affiliations: 1, + person: { + firstName: 1, + lastName: 1, + }, + roomAssignment: { + room: { + building: 1, + floor: 1, + number: 1, + } + }, + }}, + ] + + // console.log(JSON.stringify({pipeline})) + + const visits = await Visit.aggregate(pipeline) + + return visits +} + +module.exports = visitsQuery