Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya062003 committed Apr 9, 2024
2 parents d0e70ee + 52cafdd commit 18b4eb0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
71 changes: 71 additions & 0 deletions controllers/vendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,81 @@ const updateAvailablity = asyncHandler(async (req, res) => {
res.status(200).json(updatedItem);
});

const createDiscount = asyncHandler(async (req, res) => {
const {
item_id,
offer_price
} = req.body

console.log(item_id)

const {vendor_id} = req;

const item = await MenuItem.findOne({ item_id: item_id });


if(!item){
res.status(404).send("Item not found");
}

const menu = await Menu.findOne({vendor_id: vendor_id});

if(!menu){
res.status(404).send("menu not found");
}

const menuIndex = menu.items.findIndex(item => item.item_id === item_id);

if(menuIndex == -1){
res.status(404).send("Item not found in the menu");
}

menu.items[menuIndex].on_offer = true;
menu.items[menuIndex].offer_price = offer_price;
await menu.save();

const updatedItem = await MenuItem.findOneAndUpdate(
{ item_id: item_id },
{ on_offer: true, offer_price: offer_price },
)
res.status(200).send(updatedItem)
})

const deleteDiscount = asyncHandler(async (req, res) => {
const { item_id } = req.body

const {vendor_id} = req

const item = await MenuItem.findOne({ item_id: item_id });
if(!item){
res.status(404).send("Item not found");
}

const menu = await Menu.findOne({vendor_id: vendor_id});

if(!menu){
res.status(404).send("menu not found");
}

const menuIndex = menu.items.findIndex(item => item.item_id === item_id);
menu.items[menuIndex].on_offer = false;
menu.items[menuIndex].offer_price = 0;
await menu.save();

const updatedItem = await MenuItem.findOneAndUpdate(
{ item_id: item_id },
{ on_offer: false, offer_price: 0 },
)

res.status(200).send(updatedItem)
})

module.exports = {
addItem,
updateItem,
deleteItem,
updateAvailablity,
getAllItems,
createDiscount,
deleteDiscount
};
7 changes: 7 additions & 0 deletions routes/vendorRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const {
deleteItem,
updateAvailablity,
getAllItems,
createDiscount,
deleteDiscount,
} = require("../controllers/vendorController");
const extractTokenMiddleware = require("../middleware/authMiddleware");

Expand All @@ -21,4 +23,9 @@ router.delete("/menuitems",extractTokenMiddleware, deleteItem);

router.put("/updatequantity",extractTokenMiddleware, updateAvailablity);

router.post("/discount", extractTokenMiddleware, createDiscount);

router.delete("/discount", extractTokenMiddleware, deleteDiscount);


module.exports = router;

0 comments on commit 18b4eb0

Please sign in to comment.