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 tenants = sequelize.define( 'tenants', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, first_name: { type: DataTypes.TEXT, }, last_name: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); tenants.associate = (db) => { db.tenants.belongsToMany(db.agreements, { as: 'agreements', foreignKey: { name: 'tenants_agreementsId', }, constraints: false, through: 'tenantsAgreementsAgreements', }); db.tenants.belongsToMany(db.agreements, { as: 'agreements_filter', foreignKey: { name: 'tenants_agreementsId', }, constraints: false, through: 'tenantsAgreementsAgreements', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.tenants.hasMany(db.agreements, { as: 'agreements_tenant', foreignKey: { name: 'tenantId', }, constraints: false, }); db.tenants.hasMany(db.payments, { as: 'payments_tenant', foreignKey: { name: 'tenantId', }, constraints: false, }); db.tenants.hasMany(db.repair_requests, { as: 'repair_requests_tenant', foreignKey: { name: 'tenantId', }, constraints: false, }); //end loop db.tenants.belongsTo(db.users, { as: 'user', foreignKey: { name: 'userId', }, constraints: false, }); db.tenants.belongsTo(db.rooms, { as: 'room', foreignKey: { name: 'roomId', }, constraints: false, }); db.tenants.belongsTo(db.companies, { as: 'companies', foreignKey: { name: 'companiesId', }, constraints: false, }); db.tenants.belongsTo(db.users, { as: 'createdBy', }); db.tenants.belongsTo(db.users, { as: 'updatedBy', }); }; return tenants; };