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 loans = sequelize.define( 'loans', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, amount: { type: DataTypes.DECIMAL, }, purpose: { type: DataTypes.TEXT, }, application_date: { type: DataTypes.DATE, }, approval_date: { type: DataTypes.DATE, }, status: { type: DataTypes.ENUM, values: ['Pending', 'Approved', 'Rejected'], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); loans.associate = (db) => { db.loans.belongsToMany(db.loan_repayments, { as: 'loan_repayments', foreignKey: { name: 'loans_loan_repaymentsId', }, constraints: false, through: 'loansLoan_repaymentsLoan_repayments', }); db.loans.belongsToMany(db.loan_repayments, { as: 'loan_repayments_filter', foreignKey: { name: 'loans_loan_repaymentsId', }, constraints: false, through: 'loansLoan_repaymentsLoan_repayments', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.loans.hasMany(db.loan_repayments, { as: 'loan_repayments_loan', foreignKey: { name: 'loanId', }, constraints: false, }); //end loop db.loans.belongsTo(db.chama_members, { as: 'chama_member', foreignKey: { name: 'chama_memberId', }, constraints: false, }); db.loans.belongsTo(db.chamas, { as: 'chamas', foreignKey: { name: 'chamasId', }, constraints: false, }); db.loans.belongsTo(db.users, { as: 'createdBy', }); db.loans.belongsTo(db.users, { as: 'updatedBy', }); }; return loans; };