Skip to content

Commit

Permalink
modify phd courses endpoint for widget
Browse files Browse the repository at this point in the history
  • Loading branch information
c2d13p committed Jul 11, 2024
1 parent 3d9e84a commit 5900ada
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions server/controllers/public/courses.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
const EventPhdCourse = require('../../models/EventPhdCourse')

async function coursesQuery(req) {
var from = undefined;
switch (req.query.from) {
case 'now':
from = new Date()
break;
case undefined:
from = undefined
break;
default:
from = new Date(req.query.from)
}

var to = undefined;
switch (req.query.to) {
case 'now':
to = new Date()
break;
case undefined:
to = undefined
break;
default:
to = new Date(req.query.to)
}

var match = {}

if (from !== undefined) {
match["endDate"] = { "$gte": from }
}
if (to !== undefined) {
match["startDate"] = { "$lte": to }
}

const pipeline = [
{ $unwind: '$lessons'},
{ $lookup: {
from: 'conferencerooms',
localField: 'lessons.conferenceRoom',
as: 'lessons.conferenceRoom',
foreignField: '_id'
}},
{ $match: match },
{ $lookup: {
from: 'people',
localField: 'lecturers',
foreignField: '_id',
as: 'lecturers',
}},
{ $unwind: {
path: '$person',
preserveNullAndEmptyArrays: true
}},
{ $project: {
_id: 1,
startDate: 1,
endDate: 1,
title: 1,
description: 1,
lecturers: {
_id: 1,
firstName: 1,
lastName: 1,
},
title: 1,
lessons: {
date: 1,
duration: 1,
conferenceRoom: {
name: 1
}
lastName: 1
}
}}
];

const courses = await EventPhdCourse.aggregate(pipeline)
const courses = await EventPhdCourse.aggregate(pipeline);

return {
data: courses
}
};
}

module.exports = coursesQuery
module.exports = coursesQuery;

0 comments on commit 5900ada

Please sign in to comment.