Your IP : 216.73.216.220


Current Path : /home/deltalab/PMS/buy-button-generator/models/mongoose/
Upload File :
Current File : //home/deltalab/PMS/buy-button-generator/models/mongoose/product.js

const mongoose = require('mongoose');
const extendSchema = require('mongoose-extend-schema');

// SCHEMA ============================================
const ProductMediaSchema = new mongoose.Schema(
  {
    name  : String,
    url   : String,
    type  : String,
    data  : String, // Base64 media content
    imsgid: String, // The ims id associated to this media (if any)
  },
  {
    _id: false
  }
)

/**
 * The amount of a given product in a warehouse
 */
 const ProductInventorySchema = new mongoose.Schema(
  {
    warehouseId         : {
                          type: mongoose.Schema.Types.ObjectId,
                          ref: 'Warehouse',
                          unique: true,
                        },
    amount              : Number
  },
  {
    _id: false
  }
)

const SizeSchema = new mongoose.Schema(
  {
    height : Number,
    length : Number,
    width  : Number,
  },
  {
    _id: false
  }
);

const ProductBaseSchema = new mongoose.Schema(
  {
    title             : { type: String, required: true },
    description       : String,
    customDescription : String,
    price             : Number,
    originalPrice     : Number,
    msrp              : Number,
    imsgid            : String, // the global idenfier provided by IMS upon creation
    sku               : { type: String, required: true },
    barcode           : String, //EAN, ISBN, other
    requiresShipping  : Boolean,  // if this product has to be shipped
    inventoryLevels   : [ProductInventorySchema],
    bookedQuantity    : Number,
    trackInventory    : Boolean,
    showMsrp          : Boolean,
    weight            : Number,
    size              : SizeSchema,
    media             : [ProductMediaSchema],
    categoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Category'
    },
  },
  {
    _id: false,
  }
)

// Product quantity is based on inventory levels
ProductBaseSchema.virtual('quantity')
  .set(function() {
    //throw new Error(`cannot set product quantity directly: use inventoryLevels`);
    console.log(`cannot set product quantity directly: use inventoryLevels`);
  })
  .get(function() {
    // sanity check
    if (!this.inventoryLevels || 0==this.inventoryLevels.length) return 0;
    // reduce inventory amount to a single quantity
    return this.inventoryLevels.reduce((quantity, inventory) => quantity + inventory.amount, 0);
  });

const ProductSchema = new mongoose.Schema(
  {
    partnerId         : {
                        type: mongoose.Schema.Types.ObjectId,
                        required: true,
                        ref: 'Partner'
                      },
  },
  {
    timestamps: true,
    versionKey: false
  }
).add(ProductBaseSchema); // Using add() will copy over the product base methods too!

// MODELS ============================================
const productMediaModel     = mongoose.model('Media', ProductMediaSchema);
const productInventoryModel = mongoose.model('ProductInventory', ProductInventorySchema);
const productBaseModel      = mongoose.model('ProductBase', ProductBaseSchema);
const productModel          = mongoose.model('Product', ProductSchema);

// EXPORTS ===========================================
module.exports = {
  productBaseModel,
  productInventoryModel,
  productMediaModel,
  productModel,
  ProductBaseSchema,
  ProductInventorySchema,
  ProductMediaSchema,
  ProductSchema,
  SizeSchema
};