From 9940b11cf44bf079d7113e7a54721f9aa4432269 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 12 Dec 2020 21:52:55 +0100 Subject: [PATCH 1/7] Update worker with better name management and event compare --- agenda/worker.js | 88 +++++++++++++++++++++++------------------------- bin/www | 2 +- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/agenda/worker.js b/agenda/worker.js index 5a5f934..366dc31 100644 --- a/agenda/worker.js +++ b/agenda/worker.js @@ -2,12 +2,14 @@ const https = require("https"); const config = require("worker_threads").workerData; const models = require("../models"); -const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}(LP(?:[ a-zA-Z0-9]*))\\n((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs; +const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:LP(?:[ a-zA-Z0-9\\]*?))\\n){1,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs; const reg_location = /((?:[SH0-9][0-9]{2})|(?:(?:Préfa |Amphi)[0-9]))/g; const reg_teachers = /^(?:((?:[a-zA-Z]*[ -]{0,}){0,}) ([a-zA-Z]*))$/m; const reg_date = /([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z/; -const reg_classe = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/; +const reg_group = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/; const base_url = config["edt"]; +const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase(); + function fetchEvents(days = 1, TS_Start = new Date()) { return new Promise((resolve, reject) => { @@ -32,8 +34,8 @@ function fetchEvents(days = 1, TS_Start = new Date()) { reg_event.lastIndex++; } let event = []; - let promotion = []; - let classe = []; + let semester = []; + let group = []; /* m = [ FullMatch, @@ -47,21 +49,30 @@ function fetchEvents(days = 1, TS_Start = new Date()) { */ if (m[1] !== undefined) { // LPXXXX - classe.push(m[5]); } else { m.splice(1, 6); // GXSX | SX | ASPE - let csplit = m[5].split("\\n"); - csplit.pop(); - csplit.forEach(e => { - if (e === "ASPE") promotion.push(e); - if (/^S[0-9]$/.test(e)) promotion.push(e); - if (/^G[0-9]S[0-9]$/.test(e)) classe.push(e); - }); } + let csplit = m[5].split("\\n"); + csplit.pop(); + csplit.forEach(e => { + if (e === "ASPE") { + semester.push(e); + }else if(/^S[0-9]$/.test(e)) { + semester.push(e); + }else{ + let groupSplit = reg_group.exec(e); + if (groupSplit[1] === 'LP') { + group.push([groupSplit[2], groupSplit[1]]); + } else { + group.push([groupSplit[3], groupSplit[4]]); + } + } + }); + event["title"] = m[3]; - event["promotion"] = promotion; - event["class"] = classe; + event["semesters"] = semester; + event["groups"] = group; /* Date */ @@ -83,7 +94,7 @@ function fetchEvents(days = 1, TS_Start = new Date()) { /* Teachers */ - event['teachers'] = [] + event['teachers'] = []; let fullTeachers = m[6].split('\\n'); fullTeachers.forEach(e => { if(e !== ""){ @@ -128,11 +139,11 @@ function compare(a, b) { } function compareGroups(list1, list2) { - return compare(list1, list2.map(g => [g.number, g.Semester.name, g.Semester.year])); + return compare(list1.map(g => g[0] + " " + g[1]), list2.map(g => g.number + " " + g.Semester.name)); } function compareTeachers(list1, list2) { - return compare(list1, list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase())); + return compare(list1.map(t => t[0].toUpperCase() + " " + t[1].toUpperCase()), list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase())); } function compareSemesters(list1, list2) { @@ -147,13 +158,14 @@ async function updateDatabase() { include: {model: models.Semester, required: true} }, models.User, models.Semester] })) { - if (!events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() && + let ev = events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() && e.endDate.getTime() === event.endDate.getTime() && e.locations.join(", ") === event.locations && - compareGroups(e.class, event.Groups) && compareTeachers(e.teachers, event.Users) && - compareSemesters(e.semesters, event.Semesters)))) + compareGroups(e.groups, event.Groups) && compareTeachers(e.teachers, event.Users) && + compareSemesters(e.semesters, event.Semesters))); + if (!ev) await event.destroy(); else - delete events[events.indexOf(event)]; + events = events.filter(e => e !== ev); } for (let event of events) { @@ -166,12 +178,12 @@ async function updateDatabase() { let teachers = []; for (let teacher of event.teachers) { - let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0], firstName: teacher[1]}}); + let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0].toUpperCase(), firstName: capitalizeFirstLetter(teacher[1])}}); if (!t) t = await models.User.create({ - email: teacher[1].toLowerCase().replace(" ", "-") + "." + teacher[0].toLowerCase().replace(" ", "-") + "@univ-lyon1.fr", - firstName: teacher[1], - lastName: teacher[0], + email: teacher[1].toLowerCase().replaceAll(" ", "-") + "." + teacher[0].toLowerCase().replaceAll(" ", "-") + "@univ-lyon1.fr", + firstName: capitalizeFirstLetter(teacher[1]), + lastName: teacher[0].toUpperCase(), permissions: 2, passwordHash: Math.round((Math.pow(36, 12 + 1) - Math.random() * Math.pow(36, 12))).toString(36).slice(1) }); @@ -180,7 +192,7 @@ async function updateDatabase() { await e.addUsers(teachers); let semesters = []; - for (let semester of event.promotion) { + for (let semester of event.semesters) { let s = await models.Semester.findOne({where: {name: semester, year: event.startDate.getFullYear()}}); if (!s) s = await models.Semester.create({ @@ -192,32 +204,18 @@ async function updateDatabase() { await e.addSemesters(semesters); let groups = []; - for (let group of event.class) { - let rGroup = reg_classe.exec(group); - let semesterName; - let groupName; - if (rGroup[1] !== undefined) { - // LP - semesterName = 'LP'; - groupName = rGroup[2]; - } else { - // GXSX - semesterName = rGroup[4]; - groupName = rGroup[3]; - } - - - let s = await models.Semester.findOne({where: {name: semesterName, year: event.startDate.getFullYear()}}); + for (let group of event.groups) { + let s = await models.Semester.findOne({where: {name: group[1], year: event.startDate.getFullYear()}}); if (!s) s = await models.Semester.create({ - name: semesterName, + name: group[1], year: event.startDate.getFullYear() }); - let g = await models.Group.findOne({where: {number: groupName, SemesterId: s.id}}); + let g = await models.Group.findOne({where: {number: group[0], SemesterId: s.id}}); if (!g) g = await models.Group.create({ - number: groupName, + number: group[0], SemesterId: s.id }); groups.push(g); diff --git a/bin/www b/bin/www index b75ec66..aad9016 100755 --- a/bin/www +++ b/bin/www @@ -17,7 +17,7 @@ const mailClient = new SMTPClient(process.env.NODE_ENV === "test" ? {} : require /** * Render all Sass to css */ -css = sass.renderSync({ +let css = sass.renderSync({ file: "sass/style.sass", includePaths: ["sass/"], outputStyle: "compressed" From 5b9c7b4d969f1373226647690692bd3904ddbb4d Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 12 Dec 2020 22:49:09 +0100 Subject: [PATCH 2/7] Add grade and evaluation models and getter socket --- models/evaluation.js | 27 +++++++++++++++++++++++++++ models/grade.js | 30 ++++++++++++++++++++++++++++++ models/user.js | 3 +++ sockets/get/evaluationGet.js | 22 ++++++++++++++++++++++ sockets/get/gradeGet.js | 22 ++++++++++++++++++++++ sockets/index.js | 4 +++- 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 models/evaluation.js create mode 100644 models/grade.js create mode 100644 sockets/get/evaluationGet.js create mode 100644 sockets/get/gradeGet.js diff --git a/models/evaluation.js b/models/evaluation.js new file mode 100644 index 0000000..216a075 --- /dev/null +++ b/models/evaluation.js @@ -0,0 +1,27 @@ +"use strict"; + +const { Model } = require("sequelize"); + +module.exports = (sequelize, DataTypes) => { + class Evaluation extends Model { + static associate(models) { + Evaluation.belongsToMany(models.User, {through: "EvaluationTeacher"}); + Evaluation.hasMany(models.Grade); + Evaluation.belongsTo(models.UE); + } + } + Evaluation.init({ + name: { + type: DataTypes.STRING, + allowNull: false + }, + date: { + type: DataTypes.DATE, + allowNull: false + } + }, { + sequelize, + modelName: "Evaluation", + }); + return Evaluation; +}; diff --git a/models/grade.js b/models/grade.js new file mode 100644 index 0000000..1f75ee4 --- /dev/null +++ b/models/grade.js @@ -0,0 +1,30 @@ +"use strict"; + +const { Model } = require("sequelize"); + +module.exports = (sequelize, DataTypes) => { + class Grade extends Model { + static associate(models) { + Grade.belongsTo(models.Evaluation); + Grade.belongsTo(models.User, {as: "TeacherGrade"}); + Grade.belongsTo(models.User, {as: "StudentGrade"}); + } + } + Grade.init({ + score: { + type: DataTypes.FLOAT, + allowNull: false + }, + limit: { + type: DataTypes.FLOAT, + allowNull: false + }, + comment: { + type: DataTypes.TEXT + } + }, { + sequelize, + modelName: "Grade", + }); + return Grade; +}; diff --git a/models/user.js b/models/user.js index d2ccef9..0eacb8f 100644 --- a/models/user.js +++ b/models/user.js @@ -20,7 +20,10 @@ module.exports = (sequelize, DataTypes) => { static associate(models) { User.belongsToMany(models.Group, {through: "UserGroup"}); User.belongsToMany(models.Event, {through: "EventTeacher"}); + User.belongsToMany(models.Evaluation, {through: "EvaluationTeacher"}); User.belongsToMany(models.UE, {through: "UEUser"}); + //User.hasMany(models.Grade, {as : "TeacherGrade"}); + //User.hasMany(models.Grade, {as: "StudentGrade"}); } checkPassword(password) { diff --git a/sockets/get/evaluationGet.js b/sockets/get/evaluationGet.js new file mode 100644 index 0000000..55c3beb --- /dev/null +++ b/sockets/get/evaluationGet.js @@ -0,0 +1,22 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + const options = {where: {}, + include: [models.UE, {model: models.User}, + { + model: models.Grade, + include: [{model: models.User, as: "TeacherGrade"}, {model: models.User, as: "StudentGrade"}] + }] + }; + + if (data && data.id) + options.where.id = data.id; + + if (socket.request.session.user.permissions === 2) { + options.include[1].where = {email: socket.request.session.user.email}; + options.include[1].required = true; + } + socket.emit("evaluationGet", await models.Evaluation.findAll(options)); + } +}; diff --git a/sockets/get/gradeGet.js b/sockets/get/gradeGet.js new file mode 100644 index 0000000..f0a1174 --- /dev/null +++ b/sockets/get/gradeGet.js @@ -0,0 +1,22 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + const options = {where: {}, include: [models.Evaluation, + {model: models.User, as: "TeacherGrade"}, + {model: models.User, as: "StudentGrade"}]}; + + if (data && data.id) + options.where.id = data.id; + + switch (socket.request.session.user.permissions) { + case 1: + options.where.StudentGradeEmail = socket.request.session.user.email; + break; + case 2: + options.where.TeacherGradeEmail = socket.request.session.user.email; + break; + } + socket.emit("gradeGet", await models.Grade.findAll(options)); + } +}; diff --git a/sockets/index.js b/sockets/index.js index b420000..8055765 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,6 +1,5 @@ module.exports = socket => { console.log("New connection !"); - console.log(socket.request.session.user); if (!socket.request.session.user) { socket.on("login", require("./login")(socket)); socket.on("register", require("./register")(socket)); @@ -11,6 +10,9 @@ module.exports = socket => { socket.on("profileEdit", require("./profile/edit")(socket)); socket.on("logout", require("./logout")(socket)); socket.on("agendaGet", require("./get/agendaGet")(socket)); + socket.on("gradeGet", require("./get/gradeGet")(socket)); + if (socket.request.session.user.permissions > 1) + socket.on("evaluationGet", require("./get/evaluationGet")(socket)); } socket.emit("connected"); }; From 755fce2f7f585512e97fbc6f85e06c886ec93a6b Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 21:55:55 +0100 Subject: [PATCH 3/7] Some fox on agendaGet --- sockets/get/agendaGet.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/sockets/get/agendaGet.js b/sockets/get/agendaGet.js index c27a05c..8472920 100644 --- a/sockets/get/agendaGet.js +++ b/sockets/get/agendaGet.js @@ -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)); From 4bc5d00344f0135fabc636d068193c8451ca213c Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 22:43:08 +0100 Subject: [PATCH 4/7] Fix routes --- app.js | 2 -- routes/edt.js | 3 ++- routes/home.js | 9 --------- routes/marks.js | 3 ++- routes/profil.js | 3 ++- routes/viescol.js | 3 ++- 6 files changed, 8 insertions(+), 15 deletions(-) delete mode 100644 routes/home.js diff --git a/app.js b/app.js index caba270..3b56354 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,6 @@ let config = process.env.NODE_ENV === "test" ? {} : require("./config/config.jso let indexRouter = require("./routes/index"); const loginRouter = require("./routes/login"); const edtRouter = require("./routes/edt"); -const homeRouter = require("./routes/home"); const marksRouter = require("./routes/marks"); const registerRouter = require("./routes/register"); const viescolRouter = require("./routes/viescol"); @@ -41,7 +40,6 @@ app.use("/", indexRouter); app.use("/login", loginRouter); app.use("/email", require("./routes/email")); app.use("/edt", edtRouter); -app.use("/home", homeRouter); app.use("/marks", marksRouter); app.use('/register', registerRouter); app.use('/viescol', viescolRouter); diff --git a/routes/edt.js b/routes/edt.js index 753a638..8b9291c 100644 --- a/routes/edt.js +++ b/routes/edt.js @@ -1,7 +1,8 @@ const express = require("express"); +const sessionCheck = require("./utils/sessionCheck"); const router = express.Router(); -router.get("/", (req, res) => { +router.get("/", sessionCheck(1), (req, res) => { res.render("pages/edt", { title: "L'ETU" }); }); diff --git a/routes/home.js b/routes/home.js deleted file mode 100644 index 0e85584..0000000 --- a/routes/home.js +++ /dev/null @@ -1,9 +0,0 @@ -const express = require("express"); -const router = express.Router(); - -/* GET home page. */ -router.get("/", (req, res) => { - res.render("pages/home", { title: "L'ETU" }); -}); - -module.exports = router; diff --git a/routes/marks.js b/routes/marks.js index db46ce0..8b3338a 100644 --- a/routes/marks.js +++ b/routes/marks.js @@ -1,7 +1,8 @@ const express = require("express"); +const sessionCheck = require("./utils/sessionCheck"); const router = express.Router(); -router.get("/", (req, res) => { +router.get("/", sessionCheck(1), (req, res) => { res.render("pages/marks", { title: "L'ETU" }); }); diff --git a/routes/profil.js b/routes/profil.js index 82c23b8..3b0066b 100644 --- a/routes/profil.js +++ b/routes/profil.js @@ -1,7 +1,8 @@ const express = require("express"); +const sessionCheck = require("./utils/sessionCheck"); const router = express.Router(); -router.get("/", (req, res) => { +router.get("/", sessionCheck(1), (req, res) => { res.render("pages/profil", { title: "L'ETU" }); }); diff --git a/routes/viescol.js b/routes/viescol.js index e846717..4bd1b47 100644 --- a/routes/viescol.js +++ b/routes/viescol.js @@ -1,7 +1,8 @@ const express = require("express"); +const sessionCheck = require("./utils/sessionCheck"); const router = express.Router(); -router.get("/", (req, res) => { +router.get("/", sessionCheck(1), (req, res) => { res.render("pages/viescol", { title: "L'ETU" }); }); From f08e249701f5376b39d52161febd513885b61566 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 23:00:47 +0100 Subject: [PATCH 5/7] Fix name and group display on views --- sockets/login.js | 2 +- views/template/navbar.pug | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sockets/login.js b/sockets/login.js index 6d0b89a..70b4707 100644 --- a/sockets/login.js +++ b/sockets/login.js @@ -2,7 +2,7 @@ const models = require("../models"); module.exports = socket => { return async (data) => { - let user = await models.User.findByPk(data.email); + let user = await models.User.findByPk(data.email, {include: {model: models.Group, include: models.Semester}}); if (!user) socket.emit("login", {error: {message: "not_found"}}); else if (!user.checkPassword(data.password)) diff --git a/views/template/navbar.pug b/views/template/navbar.pug index 2da3620..99b6c62 100644 --- a/views/template/navbar.pug +++ b/views/template/navbar.pug @@ -1,18 +1,21 @@ extends layout block navbar - if student === true + if session.user.permissions === 1 div(id="navprofile" class="student" onclick="profilRedirect()") - p Kezel Benoit - p G4S3 + p=session.user.firstName + " " + session.user.lastName + if session.user.Groups[session.user.Groups.length-1].number.startsWith("G") + p=session.user.Groups[session.user.Groups.length-1].number + session.user.Groups[session.user.Groups.length-1].Semester.name + else + p=session.user.Groups[session.user.Groups.length-1].Semester.name + " " + session.user.Groups[session.user.Groups.length-1].number a(id="logout") Logout - if teacher === true + if session.user.permissions === 2 div(id="navprofile" class="teacher" onclick="profilRedirect()") - p Kezel Benoit + p=session.user.firstName + " " + session.user.lastName a(id="logout") Logout - if admin === true + if session.user.permissions === 3 div(id="navprofile" class="admin" onclick="profilRedirect()") - p Kezel Benoit + p=session.user.firstName + " " + session.user.lastName a(id="logout") Logout div(id="navtop") From b930cd11eadcade3a94e8a94ed0c32e03c18c2ef Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 23:04:32 +0100 Subject: [PATCH 6/7] Add userSet socket --- sockets/index.js | 2 ++ sockets/set/userSet.js | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 sockets/set/userSet.js diff --git a/sockets/index.js b/sockets/index.js index 8055765..6a74c08 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -13,6 +13,8 @@ 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) + socket.on("userSet", require("./set/userSet")(socket)); } socket.emit("connected"); }; diff --git a/sockets/set/userSet.js b/sockets/set/userSet.js new file mode 100644 index 0000000..ee3ee59 --- /dev/null +++ b/sockets/set/userSet.js @@ -0,0 +1,43 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + if (!data || !data.user) { + socket.emit("userSet", {error: {message: "missing_arguments"}}); + return; + } + let u = await models.User.findByPk(data.user); + if (!u) { + socket.emit("userSet", {error: {message: "user_not_found"}}); + return; + } + for (let i in data) + switch (i) { + case "firstName": + u.firstName = data.firstName; + break; + case "lastName": + u.lastName = data.lastName; + break; + case "password": + u.passwordHash = data.password; + break; + case "permissions": + u.permissions = data.permissions; + break; + case "groups": + let groups = []; + for (let g of data.groups) { + let gp = await models.Group.findByPk(g); + if (!gp) { + socket.emit("userGet", {error: {message: "group_not_found"}}); + return + } + groups.push(gp); + } + u.setGroups(groups); + break; + } + await u.save(); + } +}; From 25d228a0353519eaca136d4509fc703258602b51 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 14 Dec 2020 23:13:32 +0100 Subject: [PATCH 7/7] 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"); };