From 37b320d53ebde63bb05c736400272628898492d0 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sun, 24 Oct 2021 15:22:33 +0200 Subject: [PATCH] =?UTF-8?q?Lep=C5=A1=C3=AD=20model=20studenta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/models/student.js | 52 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) 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"