| Current Path : /home/deltalab/PMS/buy-button-generator/models/mongoose/ |
| Current File : //home/deltalab/PMS/buy-button-generator/models/mongoose/order.js |
/**
* Mongoose Order Model
*
* Exports a instantiable class ready for mongoose persistance
*/
const mongoose = require('mongoose');
const extendSchema = require('mongoose-extend-schema');
/**
* Information about the order customer
*/
const OrderCustomerSchema = new mongoose.Schema(
{
displayName: String,
email: String,
phone: String
},
{
_id : false
}
);
/**
* Order address details
*/
const OrderAddressSchema = new mongoose.Schema(
{
name: String,
latitude: Number,
longitude: Number,
company: String,
address1: String,
address2: String,
phone: String,
city: String,
province: String,
zip: String,
country: String,
},
{
_id : false
}
);
/**
* Price representation
*/
const OrderMoneySchema = new mongoose.Schema(
{
amount: Number,
currencyCode: String,
},
{
_id: false
}
);
/**
* An item entry in the order
*/
const OrderItemSchema = new mongoose.Schema(
{
name: String,
sku: String,
imsgid: String, // a reference to the IMS product
quantity: Number, // how many of these items in the order
weight: Number, // how heavy a single item is
size: Number, // how big a single item is
totalPriceSet:{ // total cost of all the items
type: OrderMoneySchema,
required: true
},
unitPriceSet: { // cost of a single item
type: OrderMoneySchema,
required: true
}
},
{
_id: false
}
);
/**
* Basic order data
*/
const OrderBaseSchema = new mongoose.Schema(
{
omsgid: String, // this is the order id given from the OMS
name: String, // human readable order identifier
createdAt: Date, // when the order was created
customer: {
type: OrderCustomerSchema,
required: false,
},
shippingAddress: {
type: OrderAddressSchema,
required: true,
},
totalPriceSet: {
type: OrderMoneySchema,
required: true,
},
status: String, // Payment, Fullfillment, etc
fullyPaid : Boolean,
fullyBooked : Boolean, //quantity booked before shipping
fullyPicked : Boolean, //quantity updated after shipping
items: [ // order saved items
{
type: OrderItemSchema,
required: true
}
],
},
{
_id : false
}
)
/**
* The persistent order representation
* Extends the basic order with references
*/
const OrderSchema = extendSchema(OrderBaseSchema,
{
warehouseId:{ // define what warehouse is being used to fulfill the order
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Warehouse'
},
shipmentId: { // tell where the order started from
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Shipment'
},
partnerId: { // shortcut to partner
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Partner'
},
},
{
timestamps: true,
versionKey: false
}
);
// MODELS ============================================
const orderModel = mongoose.model('Order', OrderSchema);
const orderBaseModel = mongoose.model('OrderBase', OrderBaseSchema);
const orderAddressModel = mongoose.model('OrderAddress', OrderAddressSchema);
const orderMoneyModel = mongoose.model('OrderMoney', OrderMoneySchema);
const orderItemModel = mongoose.model('OrderItem', OrderItemSchema);
const orderCustomerModel = mongoose.model('Ordercustomer', OrderCustomerSchema);
// EXPORTS ===========================================
module.exports = {
OrderAddressSchema,
OrderBaseSchema,
OrderCustomerSchema,
OrderMoneySchema,
OrderItemSchema,
OrderSchema,
orderAddressModel,
orderBaseModel,
orderCustomerModel,
orderItemModel,
orderModel,
orderMoneyModel,
};