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 accounts = sequelize.define( 'accounts', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, account_name: { type: DataTypes.TEXT, }, account_type: { type: DataTypes.ENUM, values: [ "restaurant", "caterer", "hotel", "corporate", "other" ], }, account_number: { type: DataTypes.TEXT, }, tax_exempt_number: { type: DataTypes.TEXT, }, is_tax_exempt: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, credit_status: { type: DataTypes.ENUM, values: [ "good", "hold", "delinquent" ], }, credit_limit: { type: DataTypes.DECIMAL, }, notes: { type: DataTypes.TEXT, }, is_active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); accounts.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.accounts.hasMany(db.locations, { as: 'locations_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.contacts, { as: 'contacts_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.account_price_lists, { as: 'account_price_lists_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.carts, { as: 'carts_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.orders, { as: 'orders_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.quotes, { as: 'quotes_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.sample_requests, { as: 'sample_requests_account', foreignKey: { name: 'accountId', }, constraints: false, }); db.accounts.hasMany(db.saved_lists, { as: 'saved_lists_account', foreignKey: { name: 'accountId', }, constraints: false, }); //end loop db.accounts.belongsTo(db.price_lists, { as: 'default_price_list', foreignKey: { name: 'default_price_listId', }, constraints: false, }); db.accounts.belongsTo(db.users, { as: 'assigned_sales_rep', foreignKey: { name: 'assigned_sales_repId', }, constraints: false, }); db.accounts.belongsTo(db.users, { as: 'createdBy', }); db.accounts.belongsTo(db.users, { as: 'updatedBy', }); }; return accounts; };