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 bookings = sequelize.define( 'bookings', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, service_type: { type: DataTypes.ENUM, values: ['Flight', 'Hotel', 'Package', 'Tour'], }, booking_date: { type: DataTypes.DATE, }, total_amount: { type: DataTypes.DECIMAL, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); bookings.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.bookings.hasMany(db.payments, { as: 'payments_booking', foreignKey: { name: 'bookingId', }, constraints: false, }); db.bookings.hasMany(db.vouchers, { as: 'vouchers_booking', foreignKey: { name: 'bookingId', }, constraints: false, }); db.bookings.hasMany(db.redemption, { as: 'redemption_booking', foreignKey: { name: 'bookingId', }, constraints: false, }); //end loop db.bookings.belongsTo(db.customers, { as: 'customer', foreignKey: { name: 'customerId', }, constraints: false, }); db.bookings.belongsTo(db.agents, { as: 'agent', foreignKey: { name: 'agentId', }, constraints: false, }); db.bookings.belongsTo(db.users, { as: 'createdBy', }); db.bookings.belongsTo(db.users, { as: 'updatedBy', }); }; return bookings; };