40 lines
1.6 KiB
JavaScript
40 lines
1.6 KiB
JavaScript
|
const models = require("../../models");
|
||
|
|
||
|
module.exports = socket => {
|
||
|
return async (data) => {
|
||
|
let startDate = data.startDate ? data.startDate : new Date(), endDate = data.endDate ? data.endDate : new Date();
|
||
|
let options = {where: {startDate: startDate, endDate: endDate}, include: []};
|
||
|
if (data.semesters) {
|
||
|
for (let semester of data.semesters) {
|
||
|
let s = await models.Semester.findByPk(semester);
|
||
|
if (!s) {
|
||
|
socket.emit("agendaGet", {error: {message: "semester_not_found"}});
|
||
|
return
|
||
|
} else
|
||
|
options.include.push({model: models.Semester, where: {id: s.id}, require: true});
|
||
|
}
|
||
|
}
|
||
|
if (data.groups) {
|
||
|
for (let group of data.groups) {
|
||
|
let g = await models.Group.findByPk(group);
|
||
|
if (!g) {
|
||
|
socket.emit("agendaGet", {error: {message: "group_not_found"}});
|
||
|
return
|
||
|
} else
|
||
|
options.include.push({model: models.Group, where: {id: g.id}, require: true});
|
||
|
}
|
||
|
}
|
||
|
if (data.teachers) {
|
||
|
for (let teacher of data.teachers) {
|
||
|
let t = await models.User.findByPk(teacher);
|
||
|
if (!t) {
|
||
|
socket.emit("agendaGet", {error: {message: "teacher_not_found"}});
|
||
|
return
|
||
|
} else
|
||
|
options.include.push({model: models.User, where: {id: t.email}, require: true});
|
||
|
}
|
||
|
}
|
||
|
socket.emit("agendaGet", await models.Event.findAll(options));
|
||
|
}
|
||
|
};
|