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"); };