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 tabs = sequelize.define( 'tabs', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, code: { type: DataTypes.TEXT, }, opened_at: { type: DataTypes.DATE, }, closed_at: { type: DataTypes.DATE, }, status: { type: DataTypes.ENUM, values: [ "open", "pending", "closed" ], }, total: { type: DataTypes.DECIMAL, }, notes: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); tabs.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.tabs.hasMany(db.tab_items, { as: 'tab_items_tab', foreignKey: { name: 'tabId', }, constraints: false, }); db.tabs.hasMany(db.payments, { as: 'payments_tab', foreignKey: { name: 'tabId', }, constraints: false, }); //end loop db.tabs.belongsTo(db.customers, { as: 'customer', foreignKey: { name: 'customerId', }, constraints: false, }); db.tabs.belongsTo(db.users, { as: 'opened_by', foreignKey: { name: 'opened_byId', }, constraints: false, }); db.tabs.belongsTo(db.users, { as: 'createdBy', }); db.tabs.belongsTo(db.users, { as: 'updatedBy', }); }; return tabs; };