-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify phd courses endpoint for widget
- Loading branch information
Showing
1 changed file
with
47 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |