Your IP : 216.73.216.220


Current Path : /home/deltalab/PMS/partner-manager-backend/models/mongoose/
Upload File :
Current File : //home/deltalab/PMS/partner-manager-backend/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');
const { WarehouseSchema } = require('./warehouse');

/**
 * 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,
  },
);

/**
 * Price representation
 */
const OrderActivitySchema = new mongoose.Schema(
  {
    done: Boolean,
    date: Date,
  },
  {
    _id: false,
  },
);

const OrderActivitiesSchema = new mongoose.Schema(
  {
    register: OrderActivitySchema,
    supplierMail: OrderActivitySchema,
    erpInsert: OrderActivitySchema,
    warehouse: OrderActivitySchema,
    booking: OrderActivitySchema,
    transfer: OrderActivitySchema,
    pickingList: OrderActivitySchema,
    shipmentShippyPro: OrderActivitySchema,
    shipmentSurf: OrderActivitySchema,
    mail: OrderActivitySchema,
    picking: OrderActivitySchema,
    pickup: OrderActivitySchema,
    close: OrderActivitySchema,
    cancel: OrderActivitySchema
  },
  {
    _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
    storeId: String, // store id from the order
    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,
    cashOnDelivery: Boolean,
    fullyBooked: Boolean, // quantity booked before shipping
    fullyPicked: Boolean, // quantity updated after shipping
    fulfilled: Boolean, //shipped
    instanceId: String,
    storeName: String,
    items: [ // order saved items
      {
        type: OrderItemSchema,
        required: true,
      },
    ],
    shipmentWarehouse: {
      type: WarehouseSchema,
      required: false,
    },
    activities: {
      type: OrderActivitiesSchema,
      required: false,
    },
  },
  {
    _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);

const orderActivityModel = mongoose.model('OrderActivity', OrderActivitySchema);
const orderActivitiesModel = mongoose.model('OrderActivities', OrderActivitiesSchema);

// EXPORTS ===========================================
module.exports = {
  OrderAddressSchema,
  OrderBaseSchema,
  OrderCustomerSchema,
  OrderMoneySchema,
  OrderItemSchema,
  OrderSchema,
  orderAddressModel,
  orderBaseModel,
  orderCustomerModel,
  orderItemModel,
  orderModel,
  orderMoneyModel,
  orderActivityModel,
  orderActivitiesModel,
};