Skip to content

Commit

Permalink
public seminars api
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Oct 22, 2023
1 parent 1b0eea2 commit 1751027
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 65 deletions.
8 changes: 7 additions & 1 deletion server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
47 changes: 47 additions & 0 deletions server/controllers/public/seminars.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down Expand Up @@ -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
62 changes: 62 additions & 0 deletions server/controllers/public/visits.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1751027

Please sign in to comment.