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 contracts = sequelize.define( 'contracts', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, start_date: { type: DataTypes.DATE, }, end_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); contracts.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.contracts.belongsTo(db.tenants, { as: 'tenant', foreignKey: { name: 'tenantId', }, constraints: false, }); db.contracts.belongsTo(db.rooms, { as: 'room', foreignKey: { name: 'roomId', }, constraints: false, }); db.contracts.belongsTo(db.tenants, { as: 'tenants', foreignKey: { name: 'tenantsId', }, constraints: false, }); db.contracts.hasMany(db.file, { as: 'signed_agreement', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.contracts.getTableName(), belongsToColumn: 'signed_agreement', }, }); db.contracts.belongsTo(db.users, { as: 'createdBy', }); db.contracts.belongsTo(db.users, { as: 'updatedBy', }); }; return contracts; };