32351/backend/src/db/models/clients.js
2025-06-19 17:10:07 +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 clients = sequelize.define(
'clients',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
email: {
type: DataTypes.TEXT,
},
phone: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
clients.associate = (db) => {
db.clients.belongsToMany(db.messages, {
as: 'messages',
foreignKey: {
name: 'clients_messagesId',
},
constraints: false,
through: 'clientsMessagesMessages',
});
db.clients.belongsToMany(db.messages, {
as: 'messages_filter',
foreignKey: {
name: 'clients_messagesId',
},
constraints: false,
through: 'clientsMessagesMessages',
});
db.clients.belongsToMany(db.consultations, {
as: 'consultations',
foreignKey: {
name: 'clients_consultationsId',
},
constraints: false,
through: 'clientsConsultationsConsultations',
});
db.clients.belongsToMany(db.consultations, {
as: 'consultations_filter',
foreignKey: {
name: 'clients_consultationsId',
},
constraints: false,
through: 'clientsConsultationsConsultations',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.clients.hasMany(db.consultations, {
as: 'consultations_client',
foreignKey: {
name: 'clientId',
},
constraints: false,
});
db.clients.hasMany(db.messages, {
as: 'messages_client',
foreignKey: {
name: 'clientId',
},
constraints: false,
});
//end loop
db.clients.belongsTo(db.users, {
as: 'createdBy',
});
db.clients.belongsTo(db.users, {
as: 'updatedBy',
});
};
return clients;
};