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 orders = sequelize.define( 'orders', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, status: { type: DataTypes.ENUM, values: ['Pending', 'Preparing', 'OutforDelivery', 'Delivered'], }, order_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); orders.associate = (db) => { db.orders.belongsToMany(db.menu_items, { as: 'menu_items', foreignKey: { name: 'orders_menu_itemsId', }, constraints: false, through: 'ordersMenu_itemsMenu_items', }); db.orders.belongsToMany(db.menu_items, { as: 'menu_items_filter', foreignKey: { name: 'orders_menu_itemsId', }, constraints: false, through: 'ordersMenu_itemsMenu_items', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.orders.hasMany(db.payments, { as: 'payments_order', foreignKey: { name: 'orderId', }, constraints: false, }); //end loop db.orders.belongsTo(db.users, { as: 'customer', foreignKey: { name: 'customerId', }, constraints: false, }); db.orders.belongsTo(db.restaurants, { as: 'restaurant', foreignKey: { name: 'restaurantId', }, constraints: false, }); db.orders.belongsTo(db.users, { as: 'delivery_personnel', foreignKey: { name: 'delivery_personnelId', }, constraints: false, }); db.orders.belongsTo(db.users, { as: 'createdBy', }); db.orders.belongsTo(db.users, { as: 'updatedBy', }); }; return orders; };