2020-11-24 08:47:13 +01:00
|
|
|
const models = require("../../models");
|
|
|
|
|
2020-12-14 21:55:55 +01:00
|
|
|
|
|
|
|
function addWhere(options, index, attr, value) {
|
|
|
|
if (!options.include[index].where)
|
|
|
|
options.include[index].where = {};
|
|
|
|
if (!options.include[index].where[attr])
|
2021-01-20 22:04:59 +01:00
|
|
|
options.include[index].where[attr] = value;
|
|
|
|
else {
|
|
|
|
if (options.include[index].where[attr] !== [models.Sequelize.Op.in])
|
|
|
|
options.include[index].where[attr] = {[models.Sequelize.Op.in]: [options.include[index].where[attr]]};
|
|
|
|
options.include[index].where[attr][models.Sequelize.Op.in].push(value);
|
|
|
|
}
|
2020-12-14 21:55:55 +01:00
|
|
|
options.include[index].require = true;
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:47:13 +01:00
|
|
|
module.exports = socket => {
|
|
|
|
return async (data) => {
|
2020-12-14 21:55:55 +01:00
|
|
|
let options = {
|
|
|
|
where: {},
|
|
|
|
include: [
|
|
|
|
{model: models.User, attributes: ["email", "firstName", "lastName"]},
|
|
|
|
{model: models.Group, include: models.Semester},
|
2021-01-20 22:26:13 +01:00
|
|
|
{model: models.Semester, include: {model: models.Group, include: models.Semester}}
|
2020-12-14 21:55:55 +01:00
|
|
|
]
|
|
|
|
};
|
2021-01-20 22:04:59 +01:00
|
|
|
|
|
|
|
if (data && data.startDate && data.endDate)
|
|
|
|
options.where = {startDate: {[models.Sequelize.Op.between]: [data.startDate, data.endDate]}, endDate: {[models.Sequelize.Op.between]: [data.startDate, data.endDate]}};
|
|
|
|
|
2020-12-14 21:55:55 +01:00
|
|
|
if (data && data.semesters) {
|
2020-11-24 08:47:13 +01:00
|
|
|
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
|
2020-12-14 21:55:55 +01:00
|
|
|
addWhere(options, 2, "id", s.id);
|
2020-11-24 08:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 21:55:55 +01:00
|
|
|
if (data && data.groups) {
|
2020-11-24 08:47:13 +01:00
|
|
|
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
|
2020-12-14 21:55:55 +01:00
|
|
|
addWhere(options, 1, "id", g.id);
|
2020-11-24 08:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 22:26:13 +01:00
|
|
|
if (socket.request.session.user.permissions === 2)
|
|
|
|
if (data && !data.teachers)
|
|
|
|
data.teachers = [socket.request.session.user.email];
|
2020-12-14 21:55:55 +01:00
|
|
|
if (data && data.teachers) {
|
2020-11-24 08:47:13 +01:00
|
|
|
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
|
2020-12-14 21:55:55 +01:00
|
|
|
addWhere(options, 0, "email", t.email);
|
2020-11-24 08:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.emit("agendaGet", await models.Event.findAll(options));
|
|
|
|
}
|
|
|
|
};
|