Your IP : 216.73.216.43


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

const mongoose = require('mongoose');
const extendSchema = require('mongoose-extend-schema');
const { ProductSchema }     = require('./product');

// SCHEMA ============================================
/**
 * Listing entry
 */
 const ListingEntrySchema = new mongoose.Schema(
  {
    productId         : {
                          type: mongoose.Schema.Types.ObjectId,
                          ref: 'Product',
                          unique: true,
                          required: true,
                        },
    price              : Number
  },
  {
    _id: false
  }
)

/**
 * Listing entry
 */
 const ListingEntryProductSchema = new mongoose.Schema(
  {
    product            : ProductSchema,
    price              : Number
  },
  {
    _id: false
  }
)

const ListingBaseSchema = new mongoose.Schema(
  {
    name: String,
    description: String,
    handle: String, // this is provided by the backoffice to match their collection
  },
  {
    _id: false
  }
);

const ListingSchema = extendSchema(ListingBaseSchema,
  {
    imsgid: String, // the global idenfier provided by IMS upon creation
    partnerId: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: 'Partner',
    },
    entries: [ListingEntrySchema]
  },
  {
    timestamps: true,
    versionKey: false
  }
);

// MODELS ============================================
const listingModel = mongoose.model('Listing', ListingSchema);
const listingBaseModel = mongoose.model('ListingBase', ListingBaseSchema);
const listingEntryModel = mongoose.model('ListingEntry', ListingEntrySchema);
const listingEntryProductModel = mongoose.model('ListingEntryProduct', ListingEntryProductSchema);

// EXPORTS ===========================================
module.exports = {
  listingModel,
  listingBaseModel,
  listingEntryModel,
  listingEntryProductModel,
  ListingSchema,
  ListingBaseSchema,
  ListingEntrySchema,
  ListingEntryProductSchema
};