Some fox on agendaGet
This commit is contained in:
parent
5b9c7b4d96
commit
755fce2f7f
1 changed files with 30 additions and 8 deletions
|
@ -1,37 +1,59 @@
|
|||
const models = require("../../models");
|
||||
|
||||
|
||||
function addWhere(options, index, attr, value) {
|
||||
if (!options.include[index].where)
|
||||
options.include[index].where = {};
|
||||
if (!options.include[index].where[attr])
|
||||
options.include[index].where[attr] = {};
|
||||
if (!options.include[index].where[attr][models.Sequelize.Op.or])
|
||||
options.include[index].where[attr][models.Sequelize.Op.or] = [];
|
||||
options.include[index].where[attr][models.Sequelize.Op.or].push(value);
|
||||
options.include[index].require = true;
|
||||
}
|
||||
|
||||
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) {
|
||||
let options = {
|
||||
where: {},
|
||||
include: [
|
||||
{model: models.User, attributes: ["email", "firstName", "lastName"]},
|
||||
{model: models.Group, include: models.Semester},
|
||||
{model: models.Semester, include: models.Group}
|
||||
]
|
||||
};
|
||||
if (data && data.startDate)
|
||||
options.where.startDate = data.startDate;
|
||||
if (data && data.endDate)
|
||||
options.where.endDate = data.endDate;
|
||||
if (data && 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});
|
||||
addWhere(options, 2, "id", s.id);
|
||||
}
|
||||
}
|
||||
if (data.groups) {
|
||||
if (data && 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});
|
||||
addWhere(options, 1, "id", g.id);
|
||||
}
|
||||
}
|
||||
if (data.teachers) {
|
||||
if (data && 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});
|
||||
addWhere(options, 0, "email", t.email);
|
||||
}
|
||||
}
|
||||
socket.emit("agendaGet", await models.Event.findAll(options));
|
||||
|
|
Reference in a new issue