promotion Modele
const mongoose = require("mongoose");
const PromotionSchema = mongoose.Schema({
name: { type: String, required: true },
item1: {
type: mongoose.Schema.Types.ObjectId,
ref: "itemPromotion",
required: true,
},
item2: {
type: mongoose.Schema.Types.ObjectId,
ref: "itemPromotion",
required: false,
},
item3: {
type: mongoose.Schema.Types.ObjectId,
ref: "itemPromotion",
required: false,
},
startDate: { type: Date, required: false },
endOfDate: { type: Date, required: false },
status: { type: Boolean, required: false },
totalPrice: { type: Number, required: false },
discount: { type: Number, required: false },
});
module.exports = new mongoose.model("Promotion", PromotionSchema);
promotion Items its item with price and quantity and discount pourcent in promotion
const mongoose = require("mongoose");
const ItemPromotionSchema = mongoose.Schema({
item: { type: mongoose.Schema.Types.ObjectId, ref: "Item", required: true },
price: { type: Number, required: true },
quantity: { type: Number, required: true },
discount: { type: Number, required: true },
});
module.exports = new mongoose.model("itemPromotion", ItemPromotionSchema);
items its the orginal item
const mongosse = require("mongoose");
const ItemSchema = mongosse.Schema({
code: {
type: String,
required: true,
},
label: {
type: String,
required: true,
},
itemCategory: {
type: mongosse.Schema.Types.ObjectId,
ref: "ItemCategory",
required: false,
},
// category: {
// type: CategorySchema,
// required: false,
// },
itemFamilly: {
type: mongosse.Schema.Types.ObjectId,
ref: "ItemFamilly",
required: false,
},
description: {
type: String,
required: false,
},
picture: {
type: String,
required: false,
},
Price: [{
type: mongosse.Schema.Types.ObjectId,
ref: "Price",
required: false,
}, ],
tempPrice: { type: Number, required: false },
active: {
type: Boolean,
required: true,
},
});
module.exports = new mongosse.model(“Item”, ItemSchema);
and I have also the methode get (route)
where I need to get my data
promotion with itemsPromotions with label of item
router.get("/config", async(req, res) => {
try {
const PromotionInDataBase = await Promotion.find().populate("item1").populate("item1.item");
res.json(PromotionInDataBase);
} catch (error) {
res.json({ message: error });
}
});