31391/backend/src/db/models/subjects.js
2025-05-10 11:11:57 +00:00

110 lines
2.3 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function (sequelize, DataTypes) {
const subjects = sequelize.define(
'subjects',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
subjects.associate = (db) => {
db.subjects.belongsToMany(db.users, {
as: 'teachers',
foreignKey: {
name: 'subjects_teachersId',
},
constraints: false,
through: 'subjectsTeachersUsers',
});
db.subjects.belongsToMany(db.users, {
as: 'teachers_filter',
foreignKey: {
name: 'subjects_teachersId',
},
constraints: false,
through: 'subjectsTeachersUsers',
});
db.subjects.belongsToMany(db.users, {
as: 'students',
foreignKey: {
name: 'subjects_studentsId',
},
constraints: false,
through: 'subjectsStudentsUsers',
});
db.subjects.belongsToMany(db.users, {
as: 'students_filter',
foreignKey: {
name: 'subjects_studentsId',
},
constraints: false,
through: 'subjectsStudentsUsers',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.subjects.hasMany(db.exams, {
as: 'exams_subject',
foreignKey: {
name: 'subjectId',
},
constraints: false,
});
db.subjects.hasMany(db.grades, {
as: 'grades_subject',
foreignKey: {
name: 'subjectId',
},
constraints: false,
});
//end loop
db.subjects.belongsTo(db.schools, {
as: 'schools',
foreignKey: {
name: 'schoolsId',
},
constraints: false,
});
db.subjects.belongsTo(db.users, {
as: 'createdBy',
});
db.subjects.belongsTo(db.users, {
as: 'updatedBy',
});
};
return subjects;
};