| Current Path : /home/deltalab/PMS/partner-manager-backend/models/mongoose/ |
| Current File : //home/deltalab/PMS/partner-manager-backend/models/mongoose/shipment.js |
/**
* Shipment mongoose type models
*/
const mongoose = require('mongoose');
const extendSchema = require('mongoose-extend-schema');
const { AddressSchema } = require('./address');
const { ParcelBaseSchema } = require('./parcel');
const { CarrierBaseSchema } = require('./carrier');
/**
* The manifest is created alongside
* and normalized in the shipment object
*/
const ShipmentManifestSchema = new mongoose.Schema(
{
error: String,
manifestNumber: String,
manifestUrl: String,
},
{
_id: false,
},
);
const ShipmentRateSchema = new mongoose.Schema(
{
rateId: String,
rate: Number,
deliveryDays: String,
currency: String,
carrier: CarrierBaseSchema,
},
{
_id: false,
},
);
const ShipmentValueInfoSchema = new mongoose.Schema(
{
insurance: Number,
insuranceCurrency: String,
cashOnDelivery: Number,
cashOnDeliveryCurrency: String,
contentDescription: String,
totalValue: Number,
totalValueCurrency: String,
shippingService: String,
},
{
_id: false,
},
);
/**
* Basic shipment details used as input to compute rates.
* Use the extended class to request a shipment
*/
const ShipmentBaseInfoSchema = new mongoose.Schema(
{
from: AddressSchema,
to: AddressSchema,
parcels: [ParcelBaseSchema],
shipmentValueInfo: ShipmentValueInfoSchema,
},
{
_id: false,
},
);
/**
* Complete shipment information to be used to create
* an actual shipment.
* The orderId referes to the OMS order global id.
*/
const ShipmentInfoSchema = extendSchema(ShipmentBaseInfoSchema,
{
orderId: String,
carrierId: Number,
rateId: String,
incoterm: String, // enum {"DAP","DDP","EXW"}
paymentMethod: String, // "PayPal"
note: String,
},
{
_id: false,
});
/**
* The local shipment representation
*/
const ShipmentSchema = new mongoose.Schema(
{
smsid: String, // this is the shipment id provided by the SMS
createdAt: Date, // when this shipment was created
status: String, // SMS provided status code
labelUrl: [String], // where to get the shipment label
manifestNumber: String, // the generated manifest identifer
manifestUrl: String, // where to get the shipment manifest
manifest: { // Tha associated manifest when available
type: ShipmentManifestSchema,
required: false,
},
trackingCarrier: String,
trackingNumber: String,
pickupId: String,
carrierId: { // the carrier used for the shipment
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Carrier',
},
orderId: { // Reference to the order
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Order',
},
warehouseId: { // where the shipment will be picked up from
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Warehouse',
},
partnerId: { // reference to the partner
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Partner',
},
},
{
timestamps: true,
versionKey: false,
},
);
// MODELS ============================================
const shipmentBaseInfoModel = mongoose.model('ShipmentBaseInfo', ShipmentBaseInfoSchema);
const shipmentInfoModel = mongoose.model('ShipmentInfo', ShipmentInfoSchema);
const shipmentManifestModel = mongoose.model('ShipmentManifest', ShipmentManifestSchema);
const shipmentModel = mongoose.model('Shipment', ShipmentSchema);
const shipmentRateModel = mongoose.model('ShipmentRate', ShipmentRateSchema);
const shipmentValueInfoModel = mongoose.model('ShipmentValueInfo', ShipmentValueInfoSchema);
// FUNCTIONS =========================================
/**
* Creates a ShipmentValueInfo object
* with the given standard values
* @param {Number} totalValue
* @param {String} currency
* @param {String} contentDescription
* @return a properly populated shipmentValueInfoModel
*/
function createShipmentValueInfo(
totalValue,
currency = 'EUR',
cashOnDelivery = false,
contentDescription = 'Not specified',
) {
const shipmentValueInfo = new shipmentValueInfoModel();
shipmentValueInfo.insurance = 0;
shipmentValueInfo.insuranceCurrency = currency;
shipmentValueInfo.cashOnDelivery = cashOnDelivery ? totalValue : 0;
shipmentValueInfo.cashOnDeliveryCurrency = currency;
shipmentValueInfo.contentDescription = contentDescription;
shipmentValueInfo.totalValue = totalValue;
shipmentValueInfo.totalValueCurrency = currency;
shipmentValueInfo.shippingService = 'Standard';
return shipmentValueInfo;
}
/**
* Creates a Shipment Info object
* using on the given data
* @param {*} orderId
* @param {*} carrierId
* @param {*} rateId
* @param {*} shipmentBaseInfo
* @return a properly populated shipmentInfoModel
*/
function createShipmentInfo(
orderId,
carrierId,
rateId,
shipmentBaseInfo,
) {
// Populating the shipment info with the extended parameters
const shipmentInfo = new shipmentInfoModel();
// copy base information over the shipment info
shipmentInfo.from = shipmentBaseInfo.from;
shipmentInfo.to = shipmentBaseInfo.to;
shipmentInfo.company = shipmentBaseInfo.company;
shipmentInfo.parcels = shipmentBaseInfo.parcels;
shipmentInfo.shipmentValueInfo = shipmentBaseInfo.shipmentValueInfo;
shipmentInfo.orderId = orderId;
shipmentInfo.carrierId = carrierId;
shipmentInfo.rateId = rateId;
shipmentInfo.email = shipmentBaseInfo.email;
shipmentInfo.phone = shipmentBaseInfo.phone;
shipmentInfo.incoterm = 'DAP'; // ["DAP","DDP","EXW"]' Delivered Duty Paid
shipmentInfo.paymentMethod = 'Paypal';
return shipmentInfo;
}
// EXPORTS ===========================================
module.exports = {
// Functions -------------------
createShipmentInfo,
createShipmentValueInfo,
// Schemas ---------------------
ShipmentBaseInfoSchema,
ShipmentInfoSchema,
ShipmentManifestSchema,
ShipmentRateSchema,
ShipmentSchema,
ShipmentValueInfoSchema,
// Models ----------------------
shipmentBaseInfoModel,
shipmentInfoModel,
shipmentManifestModel,
shipmentModel,
shipmentRateModel,
shipmentValueInfoModel,
};