From 25d228a0353519eaca136d4509fc703258602b51 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 23:13:32 +0100 Subject: [PATCH] Update groupGet and semesterGet --- sockets/get/groupGet.js | 15 ++++++++++----- sockets/get/semesterGet.js | 14 +++++++++----- sockets/index.js | 5 ++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/sockets/get/groupGet.js b/sockets/get/groupGet.js index 12f16ba..48ae8ed 100644 --- a/sockets/get/groupGet.js +++ b/sockets/get/groupGet.js @@ -2,19 +2,24 @@ const models = require("../../models"); module.exports = socket => { return async (data) => { - let options = {where: {}, include: [{model: models.Semester, require: true}]}; - if (data.number) + let options = {include: [{model: models.Semester}]}; + if (data && (data.id || data.number || data.semester)) + options.where = {}; + if (data && data.id) + options.where.id = data.id; + if (data && data.number) options.where.number = data.number; - if (data.semester) { + if (data && data.semester) { let s = await models.Semester.findByPk(data.semester); if (!s) { socket.emit("groupGet", {error: {message: "semester_not_found"}}); return } options.include[0].where = {id: s.id}; + options.include[0].require = true; } - if (data.users) - options.include.push({model: models.User, require: true}); + if (data && data.users) + options.include.push({model: models.User, where: {email: {[models.Sequelize.Op.in]: data.users}}, require: true}); socket.emit("groupGet", await models.Group.findAll(options)); } diff --git a/sockets/get/semesterGet.js b/sockets/get/semesterGet.js index a7ef457..e7b0bee 100644 --- a/sockets/get/semesterGet.js +++ b/sockets/get/semesterGet.js @@ -2,13 +2,17 @@ const models = require("../../models"); module.exports = socket => { return async (data) => { - let options = {where: {}, include: []}; - if (data.name) + let options = {include: [{model: models.Group}]}; + if (data && (data.id || data.name || data.year)) + options.where = {}; + if (data && data.id) + options.where.id = data.id; + if (data && data.name) options.where.name = data.name; - if (data.year) + if (data && data.year) options.where.year = data.year; - if (data.groups) - options.include.push({model: models.Group, require: true}); + if (data && data.groups) + options.include[0].where = {id: {[models.Sequelize.Op.or]: data.groups}}; socket.emit("semesterGet", await models.Semester.findAll(options)); } diff --git a/sockets/index.js b/sockets/index.js index 6a74c08..fb53df2 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -13,8 +13,11 @@ module.exports = socket => { socket.on("gradeGet", require("./get/gradeGet")(socket)); if (socket.request.session.user.permissions > 1) socket.on("evaluationGet", require("./get/evaluationGet")(socket)); - if (socket.request.session.user.permissions > 2) + if (socket.request.session.user.permissions > 2) { + socket.on("groupGet", require("./get/groupGet")(socket)); + socket.on("semesterGet", require("./get/semesterGet")(socket)); socket.on("userSet", require("./set/userSet")(socket)); + } } socket.emit("connected"); };