Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wandan23 committed Mar 28, 2021
2 parents 9a47654 + 2dc275a commit 8fe58a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
18 changes: 17 additions & 1 deletion Backend/controllers/package-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,21 @@ const addPackage= async(req, res, next) => {
});
}


const removePackage = async(req, res, next) => {
const {serialNumber} = req.body;
await Package.deleteOne({serialNumber}, function(error) {
if(error){
sendStatus(404);
}
})

res.json({
message: 'Package removed successfully!',
});
}


exports.scanPackage = scanPackage;
exports.addPackage = addPackage;
exports.addPackage = addPackage;
exports.removePackage= removePackage;
50 changes: 28 additions & 22 deletions Backend/controllers/user-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ const mongoose = require('mongoose');
const User = require('../database/User');
const Order= require('../database/Order');

// const userDetails = (req, res, next) => {
// const id = req.query['id'];

// const identifiedUser = DUMMY_USERS.find(u => u.id === id);
// if(!identifiedUser){
// const error = Error('User not found.');
// error.code = 404;
// throw error;
// }
// res.json(identifiedUser);
// }
const placeOrder= async(req, res, next) =>{
const {reusablePackageFlag, restaurantPhoneNo, restaurantCourier, orderItems, customer, walletUsed, total} = req.body;
let order={customerName:customer.name, customerAddress: customer.address, customerPhoneNo: customer.phoneNo, reusablePackageFlag, foodItems: orderItems}
Expand Down Expand Up @@ -73,16 +62,32 @@ const getOrders= async(req, res, next) =>{
const removeOrder= async(req, res, next) =>{
const{orderID, phoneNo}= req.body;

let identifiedUser= await User.updateOne({phoneNo}, {$pull: {orders: {orderID}}});
// let orderArray=[]
// for (index = 0; index < identifiedUser.orders.length; index++) {
// if(indentifiedUser.orders[index].orderID!==orderID){
// orderArray.push(identifiedUser.orders[index]);
// }
// }
// console.log(orderArray);
// identifiedUser.orders= orderArray;
// identifiedUser.save();
const identifiedUser = await User.findOne({phoneNo});
if(!identifiedUser)
{
return res.sendCode(404);
}
identifiedUser.orders = identifiedUser.orders.filter(p => {
return p.orderID !== orderID;
});
await identifiedUser.save();

res.json({
message: 'Order removed Successfully!',
user: identifiedUser
});
}

const addWalletMoney= async(req, res, next) =>{
const {phoneNo, amount}= req.body;

let identifiedUser= await User.findOne({phoneNo, userType:"Customer"}).exec().catch((error) => {
return next(error);
});
let money= identifiedUser.wallet;
money+= parseFloat(amount);
identifiedUser.wallet= money;
identifiedUser.save();
res.json({
message: 'Order removed Successfully!',
user: identifiedUser
Expand All @@ -91,4 +96,5 @@ const removeOrder= async(req, res, next) =>{

exports.placeOrder = placeOrder;
exports.getOrders= getOrders;
exports.removeOrder= removeOrder;
exports.removeOrder= removeOrder;
exports.addWalletMoney= addWalletMoney;
1 change: 1 addition & 0 deletions Backend/routes/package-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const packageRouter = express.Router();

packageRouter.post('/scanPackage', packageController.scanPackage);
packageRouter.post('/addPackage', packageController.addPackage);
packageRouter.post('/removePackage', packageController.removePackage);

module.exports = packageRouter;
1 change: 1 addition & 0 deletions Backend/routes/user-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const userRouter = express.Router();
userRouter.post('/placeOrder', userController.placeOrder);
userRouter.get('/restaurant', userController.getOrders);
userRouter.post('/removeOrder', userController.removeOrder);
userRouter.post('/addWalletMoney', userController.addWalletMoney);

module.exports = userRouter;

0 comments on commit 8fe58a9

Please sign in to comment.