Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil-ahlawat committed Mar 29, 2021
1 parent a322e6f commit 12ca892
Show file tree
Hide file tree
Showing 43 changed files with 5,460 additions and 5,114 deletions.
123 changes: 63 additions & 60 deletions Backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,78 @@ const userRouter = require('./routes/user-routes');
const packageRouter = require('./routes/package-routes');

const app = express();

/*
const DUMMY_USERS = [
{
name: 'Warehouse',
phoneNo: '98980',
password: 'admin',
userType: 'Warehouse'
},
{
name: 'Swapnil Ahlawat',
emailID: 'swapnil.ahlawat@gmail.com',
phoneNo: '98981',
password: 'admin',
userType: 'Customer',
address: "Dwarka, New Delhi",
wallet: 500.00,
promos:[{
title: "Special Reward",
description: "Get extra 10%* off on your next order!",
promoCode: "REUSE1"
},]
},
{
name: "Harley's Burgers",
phoneNo: '98982',
password: 'admin',
userType: 'Restaurant',
},
{
name: "Mama Mia Pizza",
phoneNo: '98983',
password: 'admin',
userType: 'Restaurant',
},
{
name: "Grand Hotdogs",
phoneNo: '98984',
password: 'admin',
userType: 'Restaurant',
},
{
name: "Manchow Cuisine",
phoneNo: '98985',
password: 'admin',
userType: 'Restaurant',
},
{
name: 'Andy',
phoneNo: '98986',
password: 'admin',
userType: 'Delivery Personnel',
},
]
{
name: 'Warehouse',
phoneNo: '98980',
password: 'admin',
userType: 'Warehouse',
},
{
name: 'Swapnil Ahlawat',
emailID: 'swapnil.ahlawat@gmail.com',
phoneNo: '98981',
password: 'admin',
userType: 'Customer',
address: 'Dwarka, New Delhi',
wallet: 500.0,
promos: [
{
title: 'Special Reward',
description: 'Get extra 10%* off on your next order!',
promoCode: 'REUSE1',
},
],
},
{
name: "Harley's Burgers",
phoneNo: '98982',
password: 'admin',
userType: 'Restaurant',
},
{
name: 'Mama Mia Pizza',
phoneNo: '98983',
password: 'admin',
userType: 'Restaurant',
},
{
name: 'Grand Hotdogs',
phoneNo: '98984',
password: 'admin',
userType: 'Restaurant',
},
{
name: 'Manchow Cuisine',
phoneNo: '98985',
password: 'admin',
userType: 'Restaurant',
},
{
name: 'Andy',
phoneNo: '98986',
password: 'admin',
userType: 'Delivery Personnel',
},
];
*/
connectDB();
// User.insertMany(DUMMY_USERS);

app.use(express.json({extended:false}));
app.use(express.urlencoded({extended:false}));
app.use(express.json({ extended: false }));
app.use(express.urlencoded({ extended: false }));

app.use('/login', loginRouter);
app.use('/user', userRouter);
app.use('/package', packageRouter);

app.use((error, req, res, next) => {
if(res.headerSent){
return console.error(error);
}
res.status(error.code || 500);
res.json({message: error.message || "An unknown error occurred."})
if (res.headerSent) {
return console.error(error);
}
res.status(error.code || 500);
res.json({ message: error.message || 'An unknown error occurred.' });
});

app.listen(5000);
app.listen(5000);
34 changes: 16 additions & 18 deletions Backend/controllers/login-controllers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const mongoose = require('mongoose');
const User = require('../database/User');

const login = async(req, res, next) => {
const {phoneNo, password, userType} = req.body;
console.log(phoneNo)
console.log(password)
console.log(userType)
const identifiedUser = await User.find({phoneNo, password, userType}).exec().catch((error) => {
console.log("Error catched.", error);
const login = async (req, res, next) => {
const { phoneNo, password, userType } = req.body;
const identifiedUser = await User.find({ phoneNo, password, userType })
.exec()
.catch((error) => {
return next(error);
});
console.log(identifiedUser);
if(identifiedUser.length===0){
const error = Error('Invalid credentials.');
error.code = 401;
return next(error);
}
res.json({
message: 'Logged in successfully!',
user: identifiedUser[0]
if (identifiedUser.length === 0) {
return res.status(404).send({
message: 'Invalid Credentials',
});
}
}
res.json({
message: 'Logged in successfully!',
user: identifiedUser[0],
});
};

exports.login = login;
exports.login = login;
147 changes: 72 additions & 75 deletions Backend/controllers/package-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,86 @@ const mongoose = require('mongoose');
const Package = require('../database/Package');
const User = require('../database/User');

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

const identifiedPackage = await Package.findOne({serialNumber}).exec().catch((error) => {
return next(error);
const identifiedPackage = await Package.findOne({ serialNumber })
.exec()
.catch((error) => {
return next(error);
});
if(!identifiedPackage){
return res.status(404).send({
message: "Package Not Found."
});
}

let userPhoneNo= identifiedPackage.userPhoneNo;
let lastPackageTag= identifiedPackage.packageTag;
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,
lastPackageTag: lastPackageTag
});

}

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

for(i=0; i<parseInt(numPackages); i++)
{
let newPackage = new Package({
serialNumber: lotNumber.concat(("00" + i).slice (-3)),
userPhoneNo: null,
packageTag: "Warehouse"
});
await newPackage.save();
}

res.json({
message: 'Added the packages successfully!',
if (!identifiedPackage) {
return res.status(404).send({
message: 'Package Not Found.',
});
}
}
let userPhoneNo = identifiedPackage.userPhoneNo;
let lastPackageTag = identifiedPackage.packageTag;
identifiedPackage.userPhoneNo = phoneNo;
identifiedPackage.packageTag = packageTag;
if (packageTag === 'Warehouse') {
identifiedPackage.count += 1;
identifiedPackage.userPhoneNo = null;
}
await identifiedPackage.save();
res.json({
message: 'Tag changed sucessfully!',
userPhoneNo: userPhoneNo,
lastPackageTag: lastPackageTag,
});
};

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 addPackage = async (req, res, next) => {
const { lotNumber, numPackages } = req.body;
for (i = 0; i < parseInt(numPackages); i++) {
let newPackage = new Package({
serialNumber: lotNumber.concat(('00' + i).slice(-3)),
userPhoneNo: null,
packageTag: 'Warehouse',
});
await newPackage.save();
}
res.json({
message: 'Added the packages successfully!',
});
};

if(!identifiedRestaurant){
return res.status(404).send({
message: "Restaurant not found!"
});
}
let packages = await Package.find({packageTag:"Warehouse"}).exec().catch((error) => {
return next(error);
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);
});

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: 'Packages sent successfully!',
restaurantName: identifiedRestaurant.name
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: 'Packages sent successfully!',
restaurantName: identifiedRestaurant.name,
});
};

exports.scanPackage = scanPackage;
exports.addPackage = addPackage;
exports.sendPackage= sendPackage;
exports.sendPackage = sendPackage;
Loading

0 comments on commit 12ca892

Please sign in to comment.