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 parcels = sequelize.define( 'parcels', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, parcel_number: { type: DataTypes.TEXT, }, piece_index: { type: DataTypes.INTEGER, }, weight_kg: { type: DataTypes.DECIMAL, }, length_cm: { type: DataTypes.DECIMAL, }, width_cm: { type: DataTypes.DECIMAL, }, height_cm: { type: DataTypes.DECIMAL, }, volumetric_weight_kg: { type: DataTypes.DECIMAL, }, tracking_number: { type: DataTypes.TEXT, }, parcel_status: { type: DataTypes.ENUM, values: [ "created", "label_created", "picked_up", "in_transit", "delivered", "returned", "lost", "damaged" ], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); parcels.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.parcels.hasMany(db.labels, { as: 'labels_parcel', foreignKey: { name: 'parcelId', }, constraints: false, }); db.parcels.hasMany(db.tracking_events, { as: 'tracking_events_parcel', foreignKey: { name: 'parcelId', }, constraints: false, }); //end loop db.parcels.belongsTo(db.shipments, { as: 'shipment', foreignKey: { name: 'shipmentId', }, constraints: false, }); db.parcels.belongsTo(db.users, { as: 'createdBy', }); db.parcels.belongsTo(db.users, { as: 'updatedBy', }); }; return parcels; };