1
0
Fork 0

Add grade and evaluation models and getter socket

This commit is contained in:
Ethanell 2020-12-12 22:49:09 +01:00
parent 9940b11cf4
commit 5b9c7b4d96
6 changed files with 107 additions and 1 deletions

27
models/evaluation.js Normal file
View file

@ -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;
};

30
models/grade.js Normal file
View file

@ -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;
};

View file

@ -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) {

View file

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

22
sockets/get/gradeGet.js Normal file
View file

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

View file

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