Skip to content

Commit

Permalink
add warehouse and promo api, error handlers, add wallet money option
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil-ahlawat committed Mar 29, 2021
1 parent 46d171e commit 57b7fa7
Show file tree
Hide file tree
Showing 21 changed files with 594 additions and 251 deletions.
11 changes: 6 additions & 5 deletions Backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ const connectDB = require('./database/connection');
const loginRouter = require('./routes/login-routes');
const userRouter = require('./routes/user-routes');
const packageRouter = require('./routes/package-routes');
const orderRouter = require('./routes/order-routes');
// const restaurantRouter = require('./routes/restaurant-routes');
// const deliveryRouter = require('./routes/delivery-routes');

const app = express();

Expand All @@ -33,7 +30,12 @@ const DUMMY_USERS = [
password: 'treats',
userType: 'Customer',
address: "Dwarka, New Delhi",
wallet: 500.10
wallet: 500.10,
promos:[{
title: "Special Reward",
description: "Get extra 10%* off on your next order!",
promoCode: "REUSE1"
},]
},
{
name: 'Swapnil Ahlawat',
Expand All @@ -53,7 +55,6 @@ app.use(express.urlencoded({extended:false}));
app.use('/login', loginRouter);
app.use('/user', userRouter);
app.use('/package', packageRouter);
app.use('/order', orderRouter);

app.use((error, req, res, next) => {
if(res.headerSent){
Expand Down
17 changes: 0 additions & 17 deletions Backend/controllers/order-controllers.js

This file was deleted.

65 changes: 42 additions & 23 deletions Backend/controllers/package-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,80 @@ const scanPackage= async(req, res, next) => {
return next(error);
});
if(!identifiedPackage){
res.sendStatus(404);
return res.status(404).send({
message: "Package Not Found."
});
}

let userPhoneNo= identifiedPackage.userPhoneNo;
identifiedPackage.userPhoneNo= phoneNo;
identifiedPackage.packageTag= packageTag;
if(packageTag==="Warehouse"){
identifiedPackage.count+=1;
identifiedPackage.userPhoneNo=null;
}
identifiedPackage.save();
console.log(userPhoneNo);
res.json({
message: 'Tag changed sucessfully!',
userPhoneNo: userPhoneNo
});

}

const addPackage= async(req, res, next) => {
const {lotNumber, restaurantPhoneNo, numPackages} = req.body;
const {lotNumber, numPackages} = req.body;

let identifiedRestaurant = await User.findOne({phoneNo: restaurantPhoneNo, userType: "Restaurant"}).exec().catch((error) => {
return next(error);
});

if(!identifiedRestaurant)
{
return res.sendStatus(404);
}
for(i=0; i<parseInt(numPackages); i++)
{
let newPackage = new Package({
serialNumber: lotNumber.concat(("00" + i).slice (-3)),
userPhoneNo: restaurantPhoneNo,
packageTag: "Restaurant"
userPhoneNo: null,
packageTag: "Warehouse"
});
await newPackage.save();
}

res.json({
message: 'Added the packages successfully!',
restaurant: identifiedRestaurant.name
});
}

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

let identifiedRestaurant = await User.findOne({phoneNo: phoneNo, userType: "Restaurant"}).exec().catch((error) => {
return next(error);
});

const removePackage = async(req, res, next) => {
const {serialNumber} = req.body;
await Package.deleteOne({serialNumber}, function(error) {
if(error){
res.sendStatus(404);
}
})
if(!identifiedRestaurant){
return res.status(404).send({
message: "Restaurant not found!"
});
}
let packages = await Package.find({packageTag:"Warehouse"}).exec().catch((error) => {
return next(error);
});

if(packages.length<numPackages){
return res.status(404).send({
message: "Not enough packages!"
});
}

for(i=0; i<parseInt(numPackages); i++)
{
packages[i].packageTag= "Restaurant";
packages[i].userPhoneNo= phoneNo;
await packages[i].save();
}

res.json({
message: 'Package removed successfully!',
message: 'Packages sent successfully!',
restaurantName: identifiedRestaurant.name
});
}


exports.scanPackage = scanPackage;
exports.addPackage = addPackage;
exports.removePackage= removePackage;
exports.sendPackage= sendPackage;
39 changes: 35 additions & 4 deletions Backend/controllers/user-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const removeOrder= async(req, res, next) =>{
const identifiedUser = await User.findOne({phoneNo});
if(!identifiedUser)
{
return res.sendCode(404);
return res.status(404).send({
message: "User Not Found."
});
}
identifiedUser.orders = identifiedUser.orders.filter(p => {
return p.orderID !== orderID;
Expand All @@ -93,19 +95,48 @@ const addWalletMoney= async(req, res, next) =>{
return next(error);
});
if(!identifiedUser){
res.sendStatus(404);
return res.status(404).send({
message: "User Not Found."
});
}
let money= identifiedUser.wallet;
money+= parseFloat(amount);
identifiedUser.wallet= money;
identifiedUser.save();
res.json({
message: 'Order removed Successfully!',
message: 'Money Added Successfully!',
user: identifiedUser
});
}

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

let identifiedUser= await User.findOne({phoneNo, userType:"Customer"}).exec().catch((error) => {
return next(error);
});
if(!identifiedUser){
return res.status(404).send({
message: "User Not Found."
})
}
let discountAmount= Math.floor((Math.random() * 10) + 10);
let code= Math.floor((Math.random() * 10000));
let promo= {
title: "Special Reward",
description: "Get extra "+discountAmount+"%* off on your next order!",
promoCode: "REUSE"+code
}
identifiedUser.promos.push(promo);
identifiedUser.save();
res.json({
message: 'Promo Rewarded Successfully!',
user: identifiedUser
});
}

exports.placeOrder = placeOrder;
exports.getOrders= getOrders;
exports.removeOrder= removeOrder;
exports.addWalletMoney= addWalletMoney;
exports.addWalletMoney= addWalletMoney;
exports.givePromoReward=givePromoReward;
3 changes: 3 additions & 0 deletions Backend/database/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const package = new mongoose.Schema({
},
packageTag:{
type: String, required: true
},
count:{
type: Number, default: 1
}
});

Expand Down
13 changes: 13 additions & 0 deletions Backend/database/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ const user = new mongoose.Schema({
type: String
}
}
],
promos:[
{
title: {
type:String
},
description:{
type:String
},
promoCode:{
type:String
}
}
]
});

Expand Down
8 changes: 0 additions & 8 deletions Backend/routes/order-routes.js

This file was deleted.

2 changes: 1 addition & 1 deletion Backend/routes/package-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const packageRouter = express.Router();

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

module.exports = packageRouter;
3 changes: 2 additions & 1 deletion Backend/routes/user-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const userController = require('../controllers/user-controllers');
const userRouter = express.Router();

userRouter.post('/placeOrder', userController.placeOrder);
userRouter.get('/restaurant', userController.getOrders);
userRouter.get('/getOrders', userController.getOrders);
userRouter.post('/removeOrder', userController.removeOrder);
userRouter.post('/addWalletMoney', userController.addWalletMoney);
userRouter.post('/givePromoReward', userController.givePromoReward);

module.exports = userRouter;
2 changes: 1 addition & 1 deletion Frontend/constants/theme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dimensions } from "react-native";
const { width, height } = Dimensions.get("window");
export const LINK= "https://279c1c5a3c61.ngrok.io"
export const LINK= "https://c14edd6e59ff.ngrok.io"
export const COLORS = {
// base colors
primary: "#3FC060", // green
Expand Down
25 changes: 23 additions & 2 deletions Frontend/navigation/warehouseTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createBottomTabNavigator, BottomTabBar } from "@react-navigation/bottom
import Svg, { Path } from 'react-native-svg';
import { isIphoneX } from 'react-native-iphone-x-helper';

import {WarehouseHome, Scan } from "../screens"
import {WarehouseHome, WarehouseRestaurant, Scan } from "../screens"

import { COLORS, icons } from "../constants"

Expand Down Expand Up @@ -141,7 +141,28 @@ const WarehouseTabs = () => {
)
}}
/>

<Tab.Screen
name="WarehouseRestaurant"
component={WarehouseRestaurant}
options={{
tabBarIcon: ({ focused }) => (
<Image
source={icons.cutlery}
resizeMode="contain"
style={{
width: 25,
height: 25,
tintColor: focused ? COLORS.primary : COLORS.secondary
}}
/>
),
tabBarButton: (props) => (
<TabBarCustomButton
{...props}
/>
)
}}
/>
<Tab.Screen
name="Scan"
component={Scan}
Expand Down
Loading

0 comments on commit 57b7fa7

Please sign in to comment.