Skip to content

Commit

Permalink
Merge branch 'main' into sundroid
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes authored Feb 3, 2024
2 parents 8d09508 + 9f1f1fb commit 95baec1
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/production-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
workflow_dispatch:
push:
branches:
- main
- beta

jobs:
Deploy-to-Production:
Expand Down
42 changes: 41 additions & 1 deletion docs/BackendSetup.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ directory.

You must create a `.env` file similar to **[.env.example](../.env.example)** file, remember that if you are using your own database the data might vary.

## Setting up Google Client ID and Client Secret for `.env`

To set up the Google Client ID and Client Secret for your application, follow these steps:

1. **Project Creation:**
- Go to the [Google Developers Console](https://console.developers.google.com/apis/).
- Create a new project by clicking on the project dropdown.

![image](https://github.com/MilanCommunity/Milan-Backend/assets/98630752/94601cda-237d-4aed-acbf-171fd3cc3f3f)

2. **Enable API Services:**
- Click on "Enabled API services" and select your project.

![image](https://github.com/MilanCommunity/Milan-Backend/assets/98630752/57258b52-9f02-40e1-92a0-2ffffde8b156)

3. **Create Credentials:**
- Navigate to the "Credentials" section.
- Click on "Create credentials" and choose "OAuth client ID."

![image](https://github.com/MilanCommunity/Milan-Backend/assets/98630752/f11f4180-0ea1-46f3-9c4c-f8fd72cc8ee6)

4. **Configure OAuth Client:**
- Fill out the form:
- Choose "Web application" as the application type.
- Provide your support email.
- You don't need to provide a logo for the OAuth consent screen.

5. **Obtain Client ID and Client Secret:**
- After completing the configuration, you'll receive your Client ID and Client Secret.

![image](https://github.com/MilanCommunity/Milan-Backend/assets/98630752/1ce0705e-ff95-4c9c-9686-806ee08f9351)

6. **Final Steps:**
- Update your application's `.env` file with the obtained Client ID and Client Secret.

```env
CLIENT_ID="your-client-id"
CLIENT_SECRET="your-client-secret"
### Setting up `razorpay api key` for `.env`
- Head on to [Razorpay API reference](https://razorpay.com/docs/api) and Sign Up to razor pay remember you don't need to KYC.
Expand Down Expand Up @@ -57,4 +97,4 @@ You must create a `.env` file similar to **[.env.example](../.env.example)** fil
So now you have the the frontend up and running locally. Now you can start working on the issues. You can follow the below steps to get started with the frontend.
- [Setting up the frontend locally](https://github.com/MilanCommunity/Milan/blob/main/docs/FrontendSetup.md)
- [Setting up the frontend locally](https://github.com/MilanCommunity/Milan/blob/main/docs/FrontendSetup.md)
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
Loading

0 comments on commit 95baec1

Please sign in to comment.