diff --git a/utils/models/student.js b/utils/models/student.js index 44984bb..b164e3b 100644 --- a/utils/models/student.js +++ b/utils/models/student.js @@ -1,8 +1,54 @@ const { Sequelize, Op, Model, DataTypes } = require("sequelize"); +const { demap, map } = require("../api"); const sequelize = require("../sequelize"); +const ssps = require("../ssps-server"); +const Group = require("./group"); -const Student = sequelize.define( - "student", +class Student extends Model { + /** @type {string} */ + id; + /** @type {string} */ + name; + /** @type {string} */ + classId; + + async setClassID(classId) { + if(this.classId === classId) return; + this.classId = classId; + await this.save({ fields: "classId"}); + const [groups, mainGroup] = await Promise.all([ + this.getGroups(), + Group.findOne({ + where: { + abbrev: "celá", + classId + } + }) + ]); + await Promise.all([ + this.removeGroups(groups), + this.addGroup(mainGroup) + ]); + } + + /** + * Updates discord class + */ + async syncClass() { + const person = await this.getPerson(); + if(!person || person.discord) return false; + const user = await global.client.users.fetch(person.discord); + const member = global.client.servers.resolve(ssps.server).member(user); + if(!member) return false; + await Promise.all([ + member.roles.add(ssps.reverseRoles[map[classId]]), + member.roles.remove(Object.keys(ssps.roles).filter(t => t !== map[classId])) + ]); + return true; + } +} + +Student.init( { id: { type: DataTypes.STRING(45), @@ -14,6 +60,8 @@ const Student = sequelize.define( allowNull: false } }, { + sequelize, + modelName: "student", indexes: [{ fields: ["personId"], type: "UNIQUE"