29891/backend/src/db/models/centers.js
2025-03-14 20:12:01 +00:00

88 lines
1.8 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 centers = sequelize.define(
'centers',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
location: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
centers.associate = (db) => {
db.centers.belongsToMany(db.professionals, {
as: 'professionals',
foreignKey: {
name: 'centers_professionalsId',
},
constraints: false,
through: 'centersProfessionalsProfessionals',
});
db.centers.belongsToMany(db.professionals, {
as: 'professionals_filter',
foreignKey: {
name: 'centers_professionalsId',
},
constraints: false,
through: 'centersProfessionalsProfessionals',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.centers.hasMany(db.shifts, {
as: 'shifts_center',
foreignKey: {
name: 'centerId',
},
constraints: false,
});
//end loop
db.centers.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.centers.belongsTo(db.users, {
as: 'createdBy',
});
db.centers.belongsTo(db.users, {
as: 'updatedBy',
});
};
return centers;
};