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 service_providers = sequelize.define( 'service_providers', { 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, }, ); service_providers.associate = (db) => { db.service_providers.belongsToMany(db.appointments, { as: 'appointments', foreignKey: { name: 'service_providers_appointmentsId', }, constraints: false, through: 'service_providersAppointmentsAppointments', }); db.service_providers.belongsToMany(db.appointments, { as: 'appointments_filter', foreignKey: { name: 'service_providers_appointmentsId', }, constraints: false, through: 'service_providersAppointmentsAppointments', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.service_providers.hasMany(db.appointments, { as: 'appointments_service_provider', foreignKey: { name: 'service_providerId', }, constraints: false, }); //end loop db.service_providers.belongsTo(db.organizations, { as: 'organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.service_providers.belongsTo(db.users, { as: 'createdBy', }); db.service_providers.belongsTo(db.users, { as: 'updatedBy', }); }; return service_providers; };