Skip to content

Commit

Permalink
fix: used status variables from utils in all routes
Browse files Browse the repository at this point in the history
  • Loading branch information
MahendraDani committed Oct 22, 2023
1 parent a06c909 commit e33c5a4
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 36 deletions.
7 changes: 5 additions & 2 deletions routes/club/Club.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const express = require("express");
const User = require("../../schema/user/UserSchema");
const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status");
const router = express.Router();

router.get("/", async (req, res) => {
try {
if (req.query.slug) {
const clubdetails = await User.findOne({ slug: req.query.slug });
return res.status(200).json(clubdetails);
return res.status(STATUSCODE.OK).json(clubdetails);
}

const clubs = await User.find({
Expand All @@ -15,7 +16,9 @@ router.get("/", async (req, res) => {

res.json(clubs);
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand Down
10 changes: 7 additions & 3 deletions routes/display/Display.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const express = require("express");
const User = require("../../schema/user/UserSchema");
const { STATUSCODE } = require("../../utils/Status");
const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status");
const router = express.Router();

// Route 1 - Show all avaialble Users in the DB
Expand All @@ -11,7 +11,9 @@ router.get("/users", async (req, res) => {
const allusers = await User.find({});
res.status(STATUSCODE.OK).json(allusers);
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand All @@ -21,7 +23,9 @@ router.get("/clubs", async (req, res) => {
const allClubs = await User.find({ usertype: "club" });
res.json(allClubs);
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand Down
7 changes: 5 additions & 2 deletions routes/payment/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router();

const shortid = require("shortid");
const Razorpay = require("razorpay");
const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status");

const razorpay = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID,
Expand All @@ -24,13 +25,15 @@ router.post("/razorpay", async (req, res) => {

try {
const response = await razorpay.orders.create(options);
return res.status(200).json({
return res.status(STATUSCODE.OK).json({
id: response.id,
currency: response.currency,
amount: response.amount,
});
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand Down
35 changes: 25 additions & 10 deletions routes/shop/Products.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const express = require("express");
const Products = require("../../schema/shop/ProductSchema");
const User = require("../../schema/user/UserSchema");
const router = express.Router();
const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status");

// Route 1 - Adding Products

Expand All @@ -22,7 +23,9 @@ router.post("/addproduct", async (req, res) => {
const existingSlug = await Products.findOne({ productSlug }); //productSlug should be unique

if (existingSlug) {
return res.status(409).json({ message: "productSlug already exists" });
return res
.status(STATUSCODE.CONFLICT)
.json({ message: STATUSMESSAGE.PRODUCT_SLUG_ALREADY_EXISTS });
}

// Create a new product object based on the schema
Expand All @@ -31,10 +34,12 @@ router.post("/addproduct", async (req, res) => {
// Save the new product to the database
const savedProduct = await newProduct.save();

res.status(201).json(savedProduct); // Return the saved product as a response
res.status(STATUSCODE.CREATED).json(savedProduct); // Return the saved product as a response
} catch (error) {
console.error("Error adding product:", error);
res.status(500).json({ message: "Failed to add product" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.PRODUCT_ADD_FAILED });
}
});

Expand All @@ -48,10 +53,12 @@ router.post("/addproduct", async (req, res) => {
router.get("/allproducts", async (req, res) => {
try {
const allProducts = await Products.find();
res.status(200).json(allProducts);
res.status(STATUSCODE.OK).json(allProducts);
} catch (error) {
console.error("Error fetching products:", error);
res.status(500).json({ message: "Failed to fetch products" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.PRODUCT_FETCH_FAILED });
}
});

Expand All @@ -69,13 +76,17 @@ router.get("/:productSlug", async (req, res) => {
const product = await Products.findOne({ productSlug });

if (!product) {
return res.status(404).json({ message: "Product not found" });
return res
.status(STATUSCODE.NOT_FOUND)
.json({ message: STATUSMESSAGE.PRODUCT_NOT_FOUND });
}

res.status(200).json(product);
res.status(STATUSCODE.OK).json(product);
} catch (error) {
console.error("Error fetching product:", error);
res.status(500).json({ message: "Failed to fetch product" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.PRODUCT_FETCH_FAILED });
}
});

Expand All @@ -98,10 +109,14 @@ router.post("/cart/add", async (req, res) => {
if (response.modifiedCount === 1) {
return res.send("Product added successfully");
} else {
res.status(404).json({ message: "User not Found" });
res
.status(STATUSCODE.NOT_FOUND)
.json({ message: STATUSMESSAGE.USER_NOT_FOUND });
}
} catch (err) {
res.status(500).json({ message: "Failed to add product to cart" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.PRODUCT_ADD_FAILED });
console.log(err);
}
});
Expand Down
26 changes: 18 additions & 8 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@ router.post("/signin", async (req, res) => {
const existingUser = await User.findOne({ email });

if (!existingUser) {
return res.status(404).json({ message: "User not found" });
return res
.status(STATUSCODE.NOT_FOUND)
.json({ message: STATUSMESSAGE.USER_NOT_FOUND });
}
const validPassword = await bcrypt.compare(password, existingUser.password);
if (!validPassword) {
return res.status(401).json({ message: "Invalid Credentials" });
return res
.status(STATUSCODE.UNAUTHORIZED)
.json({ message: STATUSMESSAGE.INVALID_CREDENTIALS });
}

const payload = { User: { id: existingUser.email } };
const token = jwt.sign(payload, process.env.JWT_SECRET);

res
.status(201)
.status(STATUSCODE.CREATED)
.cookie("Token", token, {
sameSite: "none",
httpOnly: true,
Expand Down Expand Up @@ -81,7 +85,7 @@ router.post("/signin", async (req, res) => {
})

.json({
message: "Logged you in !",
message: STATUSMESSAGE.LOGIN_SUCCESS,
});
} catch (err) {
res.status(STATUSCODE.INTERNAL_SERVER_ERROR).json({ message: err });
Expand All @@ -94,7 +98,9 @@ router.post("/update", async (req, res) => {
const { email, oldPassword, newPassword } = req.body;
const existingUser = await User.findOne({ email: email });
if (!existingUser) {
return res.status(404).json({ message: "User not found" });
return res
.status(STATUSCODE.NOT_FOUND)
.json({ message: STATUSMESSAGE.USER_NOT_FOUND });
}
// User Exists in the database
const validPassword = await bcrypt.compare(
Expand All @@ -103,7 +109,9 @@ router.post("/update", async (req, res) => {
);

if (!validPassword) {
return res.status(401).json({ message: "Invalid Credentials" });
return res
.status(STATUSCODE.UNAUTHORIZED)
.json({ message: STATUSMESSAGE.USER_NOT_FOUND });
}
// Old Password Mathched
if (newPassword.length < 5) {
Expand All @@ -127,12 +135,14 @@ router.post("/update", async (req, res) => {
};

await User.replaceOne({ email: email }, UserData);
res.status(201).json({ message: "Password Updated Successfully" });
res
.status(STATUSCODE.CREATED)
.json({ message: STATUSMESSAGE.PASSWORD_UPDATE_SUCCESS });
} catch (error) {
// User Password Updated
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: "Internal Server Error" });
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand Down
30 changes: 19 additions & 11 deletions routes/user/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ const User = require("../../schema/user/UserSchema");
const router = express.Router();
const bcrypt = require("bcryptjs");
const ReportProblem = require("../../schema/user/ReportProblemSchema");
const { STATUSCODE } = require("../../utils/Status");
const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status");

// Route 1 - Update User details
router.post("/update", async (req, res) => {
try {
const { email, oldPassword, newPassword } = req.body;
const existingUser = await User.findOne({ email: email });
if (!existingUser) {
return res.status(404).json({ message: "User not found" });
return res
.status(STATUSCODE.NOT_FOUND)
.json({ message: STATUSMESSAGE.USER_NOT_FOUND });
}
// User Exists in the database
const validPassword = await bcrypt.compare(
Expand All @@ -20,7 +22,9 @@ router.post("/update", async (req, res) => {
);

if (!validPassword) {
return res.status(401).json({ message: "Invalid Credentials" });
return res
.status(STATUSCODE.UNAUTHORIZED)
.json({ message: STATUSMESSAGE.INVALID_CREDENTIALS });
}
// Old Password Mathched
if (newPassword.length < 5) {
Expand All @@ -44,12 +48,14 @@ router.post("/update", async (req, res) => {
};

await User.replaceOne({ email: email }, UserData);
res.status(201).json({ message: "Password Updated Successfully" });
res
.status(STATUSCODE.CREATED)
.json({ message: STATUSMESSAGE.PASSWORD_UPDATE_SUCCESS });
} catch (error) {
// User Password Updated
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: "Internal Server Error" });
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand All @@ -67,9 +73,9 @@ router.post("/report", async (req, res) => {
).getMinutes();

if (hourOfThisReport >= currentHour - 120) {
return res.status(429).json({
return res.status(STATUSCODE.TOO_MANY_REQUESTS).json({
success: false,
message: "You have already reported a problem in the last 2 hours.",
message: STATUSMESSAGE.TOO_MANY_REQUESTS,
});
}
}
Expand All @@ -84,19 +90,19 @@ router.post("/report", async (req, res) => {
});

await ReportData.save();
res.status(200).json({ success: true });
res.status(STATUSCODE.OK).json({ success: true });
} catch (e) {
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: "Internal Server Error" });
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

router.get("/", async (req, res) => {
try {
if (req.query.slug) {
const userdetails = await User.findOne({ slug: req.query.slug });
return res.status(200).json(userdetails);
return res.status(STATUSCODE.OK).json(userdetails);
}

const users = await User.find({
Expand All @@ -105,7 +111,9 @@ router.get("/", async (req, res) => {

res.json(users);
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
res
.status(STATUSCODE.INTERNAL_SERVER_ERROR)
.json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR });
}
});

Expand Down
6 changes: 6 additions & 0 deletions utils/Status.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const STATUSMESSAGE = {
NOT_FOUND: "Not found !",
EVENT_SLUG_ALREADY_EXISTS: "Event slug already exists",
CREATE_EVENT_FAILED: "Failed to create event",
PRODUCT_SLUG_ALREADY_EXISTS: "productSlug already exists",
PRODUCT_ADD_FAILED: "Failed to add product",
PRODUCT_FETCH_FAILED: "Failed to fetch products",
PRODUCT_NOT_FOUND: "Product not found",
PASSWORD_UPDATE_SUCCESS: "Password Updated Successfully",
TOO_MANY_REQUESTS: "You have already reported a problem in the last 2 hours.",
};

module.exports = { STATUSCODE, STATUSMESSAGE };

0 comments on commit e33c5a4

Please sign in to comment.