29710/backend/src/db/models/counselors.js
2025-03-08 03:55:10 +00:00

110 lines
2.4 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 counselors = sequelize.define(
'counselors',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
first_name: {
type: DataTypes.TEXT,
},
last_name: {
type: DataTypes.TEXT,
},
email: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
counselors.associate = (db) => {
db.counselors.belongsToMany(db.students, {
as: 'students',
foreignKey: {
name: 'counselors_studentsId',
},
constraints: false,
through: 'counselorsStudentsStudents',
});
db.counselors.belongsToMany(db.students, {
as: 'students_filter',
foreignKey: {
name: 'counselors_studentsId',
},
constraints: false,
through: 'counselorsStudentsStudents',
});
db.counselors.belongsToMany(db.appointments, {
as: 'appointments',
foreignKey: {
name: 'counselors_appointmentsId',
},
constraints: false,
through: 'counselorsAppointmentsAppointments',
});
db.counselors.belongsToMany(db.appointments, {
as: 'appointments_filter',
foreignKey: {
name: 'counselors_appointmentsId',
},
constraints: false,
through: 'counselorsAppointmentsAppointments',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.counselors.hasMany(db.appointments, {
as: 'appointments_counselor',
foreignKey: {
name: 'counselorId',
},
constraints: false,
});
db.counselors.hasMany(db.students, {
as: 'students_counselor',
foreignKey: {
name: 'counselorId',
},
constraints: false,
});
//end loop
db.counselors.belongsTo(db.users, {
as: 'createdBy',
});
db.counselors.belongsTo(db.users, {
as: 'updatedBy',
});
};
return counselors;
};