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 foundations = sequelize.define( 'foundations', { 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, }, ); foundations.associate = (db) => { db.foundations.belongsToMany(db.appointments, { as: 'appointments', foreignKey: { name: 'foundations_appointmentsId', }, constraints: false, through: 'foundationsAppointmentsAppointments', }); db.foundations.belongsToMany(db.appointments, { as: 'appointments_filter', foreignKey: { name: 'foundations_appointmentsId', }, constraints: false, through: 'foundationsAppointmentsAppointments', }); db.foundations.belongsToMany(db.leads, { as: 'leads', foreignKey: { name: 'foundations_leadsId', }, constraints: false, through: 'foundationsLeadsLeads', }); db.foundations.belongsToMany(db.leads, { as: 'leads_filter', foreignKey: { name: 'foundations_leadsId', }, constraints: false, through: 'foundationsLeadsLeads', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.foundations.hasMany(db.appointments, { as: 'appointments_foundation', foreignKey: { name: 'foundationId', }, constraints: false, }); db.foundations.hasMany(db.job_posts, { as: 'job_posts_foundation', foreignKey: { name: 'foundationId', }, constraints: false, }); db.foundations.hasMany(db.leads, { as: 'leads_foundation', foreignKey: { name: 'foundationId', }, constraints: false, }); db.foundations.hasMany(db.orders, { as: 'orders_foundation', foreignKey: { name: 'foundationId', }, constraints: false, }); //end loop db.foundations.belongsTo(db.organizations, { as: 'organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.foundations.belongsTo(db.users, { as: 'createdBy', }); db.foundations.belongsTo(db.users, { as: 'updatedBy', }); }; return foundations; };