| Current Path : /home/deltalab/PMS/sms-connector/models/mongoose/ |
| Current File : //home/deltalab/PMS/sms-connector/models/mongoose/parcel.js |
const mongoose = require('mongoose');
const { OrderItemSchema } = require('./order');
// SCHEMA ============================================
const BoxSchema = mongoose.Schema(
{
name : String,
length : Number,
width : Number,
height : Number,
maxWeight : Number,
},
{
_id: false
}
);
/**
* This is what the shipment managament requires,
* just size and weight of the parcel to ship
*/
const ParcelBaseSchema = mongoose.Schema(
{
height : Number,
length : Number,
width : Number,
weight : Number,
},
{
_id: false
}
);
/**
* This is an enriched version of the parcel
* adding used box details and items
*/
const ParcelSchema = mongoose.Schema(
{
box : BoxSchema,
items : [OrderItemSchema],
},
{
_id: false
}
).add(ParcelBaseSchema);
// assign a function to the "methods" object of our animalSchema
ParcelSchema.methods.setBox = function(box) {
this.box = box;
this.height = box.height;
this.length = box.length;
this.width = box.width;
};
ParcelSchema.methods.addItem = function(item) {
this.items.push(item);
// item weight sanitization
const itemWeight = item.weight>0 ? item.weight : 0;
// ensure parcel weight is initialized properly
this.weight = this.weight ? this.weight + itemWeight : itemWeight;
};
ParcelSchema.methods.setItems = function(items) {
for (const item of items) {
this.addItem(item);
}
}
// MODELS ============================================
const parcelBaseModel = mongoose.model('ParcelBase', ParcelBaseSchema);
const parcelModel = mongoose.model('Parcel', ParcelSchema);
const boxModel = mongoose.model('Box', BoxSchema);
module.exports = {
BoxSchema,
ParcelBaseSchema,
ParcelSchema,
boxModel,
parcelBaseModel,
parcelModel,
}