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 shipments = sequelize.define( 'shipments', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, customer: { type: DataTypes.TEXT, }, phoneNumber: { type: DataTypes.TEXT, }, address: { type: DataTypes.TEXT, }, address2: { type: DataTypes.TEXT, }, zipCode: { type: DataTypes.TEXT, }, city: { type: DataTypes.TEXT, }, state: { type: DataTypes.TEXT, }, customerCharge: { type: DataTypes.DECIMAL, }, actualCharge: { type: DataTypes.DECIMAL, }, businessname: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); shipments.associate = (db) => { db.shipments.belongsToMany(db.products, { as: 'products', foreignKey: { name: 'shipments_productsId', }, constraints: false, through: 'shipmentsProductsProducts', }); db.shipments.belongsToMany(db.products, { as: 'products_filter', foreignKey: { name: 'shipments_productsId', }, constraints: false, through: 'shipmentsProductsProducts', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.shipments.hasMany(db.quotes, { as: 'quotes_shipment', foreignKey: { name: 'shipmentId', }, constraints: false, }); //end loop db.shipments.belongsTo(db.quotes, { as: 'quote', foreignKey: { name: 'quoteId', }, constraints: false, }); db.shipments.belongsTo(db.users, { as: 'createdBy', }); db.shipments.belongsTo(db.users, { as: 'updatedBy', }); }; return shipments; };