const config = require('../../config'); const providers = config.providers; const crypto = require('crypto'); const bcrypt = require('bcryptjs'); const moment = require('moment'); module.exports = function (sequelize, DataTypes) { const clients = sequelize.define( 'clients', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, contact_email: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); clients.associate = (db) => { db.clients.belongsToMany(db.orders, { as: 'orders', foreignKey: { name: 'clients_ordersId', }, constraints: false, through: 'clientsOrdersOrders', }); db.clients.belongsToMany(db.orders, { as: 'orders_filter', foreignKey: { name: 'clients_ordersId', }, constraints: false, through: 'clientsOrdersOrders', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.clients.hasMany(db.invoices, { as: 'invoices_client', foreignKey: { name: 'clientId', }, constraints: false, }); db.clients.hasMany(db.orders, { as: 'orders_client', foreignKey: { name: 'clientId', }, constraints: false, }); //end loop db.clients.belongsTo(db.organizations, { as: 'organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.clients.belongsTo(db.users, { as: 'createdBy', }); db.clients.belongsTo(db.users, { as: 'updatedBy', }); }; return clients; };