S52 Capstone 2 Added

master
Ron Reciproco 1 year ago
parent 46a2e16bef
commit ee9419584f

@ -163,17 +163,29 @@ exports.getUserDetails = async (req, res) => {
return res.status(404).json({ message: "User not found" }); return res.status(404).json({ message: "User not found" });
} }
// Create a new object with only the desired properties (excluding password)
const userWithoutPassword = {
_id: user._id,
username: user.username,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
orderedProducts: user.orderedProducts,
// Add other properties you want to include in the response
};
// Return the user details in the response // Return the user details in the response
res.status(200).json({ res.status(200).json({
user, user: userWithoutPassword,
}); });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ message: "Internal server error" }); res.status(500).json({ message: false });
} }
}; };
exports.setAdmin = async (req, res) => { exports.setAdmin = async (req, res) => {
try { try {
const { userId } = req.body // assuming userId is sent in the request body const { userId } = req.body // assuming userId is sent in the request body
@ -194,11 +206,3 @@ exports.setAdmin = async (req, res) => {
} }
} }
exports.getAllOrders = async (req, res) => {
try {
const orders = await User.find({}, "orderedProducts");
res.status(200).json({ success: true, data: orders });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
};

@ -39,8 +39,18 @@
"response": [] "response": []
}, },
{ {
"name": "User Authenticate", "name": "User Authenticate/Login",
"request": { "request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0NGQ5YmU1YzAxZjZjMGNhNzkyMDAiLCJlbWFpbCI6IndhcGF0dUBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMjE0NTcwLCJleHAiOjE3MDAyMTgxNzB9.IMrvxc4l2A5cwMATP3FuCIiZUmaS-eewpqLz5CORQQA",
"type": "string"
}
]
},
"method": "POST", "method": "POST",
"header": [], "header": [],
"body": { "body": {
@ -78,7 +88,7 @@
"bearer": [ "bearer": [
{ {
"key": "token", "key": "token",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTUzNjU0NDk4YzFiYmRhMzNmODJiZjAiLCJlbWFpbCI6ImRpdmluZUBleGFtcGxlLmNvbSIsImlhdCI6MTY5OTk3NTczNSwiZXhwIjoxNjk5OTc5MzM1fQ.oWOhcmPp_5-c4vcSncTdfZWpTCybBQejZaLy3RiNV-E", "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0NGQ5YmU1YzAxZjZjMGNhNzkyMDAiLCJlbWFpbCI6IndhcGF0dUBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMjE0NTcwLCJleHAiOjE3MDAyMTgxNzB9.IMrvxc4l2A5cwMATP3FuCIiZUmaS-eewpqLz5CORQQA",
"type": "string" "type": "string"
} }
] ]
@ -87,7 +97,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\r\n \"userId\": \"6553654498c1bbda33f82bf0\",\r\n \"newEmail\": \"divine@example.com\",\r\n \"newFirstname\": \"Ron\",\r\n \"newLastName\": \"Pogi\",\r\n \"newPassword\": \"crypto123\"\r\n}\r\n", "raw": "{\r\n \"userId\": \"65544d9be5c01f6c0ca79200\",\r\n \"newEmail\": \"user@email.com\",\r\n \"newFirstname\": \"Ron\",\r\n \"newLastName\": \"Pogi\",\r\n \"newPassword\": \"user\"\r\n}\r\n",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -110,39 +120,19 @@
"response": [] "response": []
}, },
{ {
"name": "Product Get All", "name": "Retrieve Active Products",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:3000/user/all",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"user",
"all"
]
}
},
"response": []
},
{
"name": "Product Get All [Active]",
"request": { "request": {
"method": "GET", "method": "GET",
"header": [], "header": [],
"url": { "url": {
"raw": "http://localhost:3000/user/active", "raw": "http://localhost:3000/product/active",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"active" "active"
] ]
} }
@ -150,19 +140,19 @@
"response": [] "response": []
}, },
{ {
"name": "Get a Single Product", "name": "Retrieve Single Product",
"request": { "request": {
"method": "GET", "method": "GET",
"header": [], "header": [],
"url": { "url": {
"raw": "http://localhost:3000/user/products/65545619b88d0a48f00ae778", "raw": "http://localhost:3000/product/products/65545619b88d0a48f00ae778",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"products", "products",
"65545619b88d0a48f00ae778" "65545619b88d0a48f00ae778"
] ]
@ -171,7 +161,7 @@
"response": [] "response": []
}, },
{ {
"name": "Order / User Checkout [Non-admin]", "name": "User Checkout(Create Order)",
"request": { "request": {
"method": "POST", "method": "POST",
"header": [], "header": [],
@ -210,7 +200,7 @@
"bearer": [ "bearer": [
{ {
"key": "token", "key": "token",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0YWM4ZGQ3ZmJmOWVlOTAyMTdlNzciLCJlbWFpbCI6Im1hc3RlckBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMDUwOTkzLCJleHAiOjE3MDAwNTQ1OTN9._K7A7ZmE4ApbmE1qtpPYRmiugYn7HpgsIOAW7yR0pUA", "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0NGQ5YmU1YzAxZjZjMGNhNzkyMDAiLCJlbWFpbCI6IndhcGF0dUBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMjE0NTcwLCJleHAiOjE3MDAyMTgxNzB9.IMrvxc4l2A5cwMATP3FuCIiZUmaS-eewpqLz5CORQQA",
"type": "string" "type": "string"
} }
] ]
@ -219,7 +209,7 @@
"header": [], "header": [],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\r\n \"userId\": \"6554ac8dd7fbf9ee90217e77\"\r\n}\r\n", "raw": "{\r\n \"userId\": \"65544d9be5c01f6c0ca79200\"\r\n}\r\n",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
@ -240,53 +230,34 @@
} }
}, },
"response": [] "response": []
},
{
"name": "Retrieve Authenticated user's orders",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0NGQ5YmU1YzAxZjZjMGNhNzkyMDAiLCJlbWFpbCI6IndhcGF0dUBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMDUwMzYzLCJleHAiOjE3MDAwNTM5NjN9.Csls5KcCnhERt_QrmcFz7vOkEvDaz8-B79AydV2DKqo",
"type": "string"
} }
] ]
}, },
"method": "POST", {
"name": "Admin",
"item": [
{
"name": "Retrieve All Products",
"request": {
"method": "GET",
"header": [], "header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"userId\": \"65544d9be5c01f6c0ca79200\"\r\n}\r\n",
"options": {
"raw": {
"language": "json"
}
}
},
"url": { "url": {
"raw": "http://localhost:3000/user/getOrders", "raw": "http://localhost:3000/product/all",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"getOrders" "all"
] ]
} }
}, },
"response": [] "response": []
}
]
}, },
{ {
"name": "Admin", "name": "Create Product",
"item": [
{
"name": "Product Create[Admin]",
"request": { "request": {
"auth": { "auth": {
"type": "bearer", "type": "bearer",
@ -310,22 +281,22 @@
} }
}, },
"url": { "url": {
"raw": "http://localhost:3000/user/products", "raw": "http://localhost:3000/product/create",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"products" "create"
] ]
} }
}, },
"response": [] "response": []
}, },
{ {
"name": "Update a product[Admin]", "name": "Update Product",
"request": { "request": {
"auth": { "auth": {
"type": "bearer", "type": "bearer",
@ -349,14 +320,14 @@
} }
}, },
"url": { "url": {
"raw": "http://localhost:3000/user/products/65545a1e6fa9d841e1518d1d", "raw": "http://localhost:3000/product/products/65545a1e6fa9d841e1518d1d",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"products", "products",
"65545a1e6fa9d841e1518d1d" "65545a1e6fa9d841e1518d1d"
] ]
@ -365,7 +336,7 @@
"response": [] "response": []
}, },
{ {
"name": "Activate Product[Admin]", "name": "Activate Product",
"request": { "request": {
"auth": { "auth": {
"type": "bearer", "type": "bearer",
@ -380,14 +351,14 @@
"method": "PUT", "method": "PUT",
"header": [], "header": [],
"url": { "url": {
"raw": "http://localhost:3000/user/products/6554634e5cac4bcd6f2394ed/activate", "raw": "http://localhost:3000/product/products/6554634e5cac4bcd6f2394ed/activate",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"products", "products",
"6554634e5cac4bcd6f2394ed", "6554634e5cac4bcd6f2394ed",
"activate" "activate"
@ -397,19 +368,19 @@
"response": [] "response": []
}, },
{ {
"name": "Archive Product[Admin]", "name": "Archive/Draft Product",
"request": { "request": {
"method": "PUT", "method": "PUT",
"header": [], "header": [],
"url": { "url": {
"raw": "http://localhost:3000/user/products/6554634e5cac4bcd6f2394ed/archive", "raw": "http://localhost:3000/product/products/6554634e5cac4bcd6f2394ed/archive",
"protocol": "http", "protocol": "http",
"host": [ "host": [
"localhost" "localhost"
], ],
"port": "3000", "port": "3000",
"path": [ "path": [
"user", "product",
"products", "products",
"6554634e5cac4bcd6f2394ed", "6554634e5cac4bcd6f2394ed",
"archive" "archive"
@ -440,8 +411,18 @@
"response": [] "response": []
}, },
{ {
"name": "Retrieve all Orders[Admin]", "name": "Retrieve All Orders",
"request": { "request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0YWM4ZGQ3ZmJmOWVlOTAyMTdlNzciLCJlbWFpbCI6Im1hc3RlckBlbWFpbC5jb20iLCJpc0FkbWluIjpmYWxzZSwiaWF0IjoxNzAwMjEyNjkzLCJleHAiOjE3MDAyMTYyOTN9.J908JWFjN5dKRw0-XJPHa4kD6QAW4M7tv1LOVmbtM_E",
"type": "string"
}
]
},
"method": "GET", "method": "GET",
"header": [], "header": [],
"url": { "url": {

@ -31,7 +31,8 @@ mongoose
}) })
// Routes // Routes
app.use("/user", userRoute, productRoute) app.use("/user", userRoute)
app.use("/product", productRoute)
app.use("/cart", cartRoute) app.use("/cart", cartRoute)
// Server up // Server up

@ -1,140 +1,176 @@
> Run: npm start ## E-COMMERCE API DOCUMENTATION
**** Stretch Goals **** **_INSTALLATION COMMAND:_**
+ Set user as Admin ( Admin Only )
+ Retrieve Authenticated User's Orders `npm install bcrypt cors dotenv express faker jsonwebtoken mongoose nodemon`
+ Retrieve all orders ( Admin Only )
+ Add to Cart ( Added Products, Change Product Quantities, Remove Products From Cart, Subtotal for each item, Total price for all items) **_Start_**
+ Authentication Token with expiration (1hr) npm start
+ dotenv
+ faker (Auto Generate Names) **_TEST ACCOUNTS:_**
+ getUserDetails function ( Detects if the user tries to get the details of the other useId's + Token auth)
+ Middleware Secure verification that match Token and UserId to next() - Regular User:
- email: user@email.com
**** Accounts **** - pwd: user
- Admin User:
User: ( Password: wapatu ) - email: admin@email.com
{ - pwd: admin
"userId": "65544d9be5c01f6c0ca79200",
"email": "wapatu@example.com",
"firstName": "Estevan",
"lastName": "Cummings",
"isAdmin": false,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTU0NGQ5YmU1YzAxZjZjMGNhNzkyMDAiLCJlbWFpbCI6IndhcGF0dUBleGFtcGxlLmNvbSIsImlzQWRtaW4iOmZhbHNlLCJpYXQiOjE3MDAwMjM3MjQsImV4cCI6MTcwMDAyNzMyNH0.dpWV9Zx64TH4RLgmV_RlyrMBCa0HwDe9wJRAkwAyjys"
}
Admin: (Password is: admin )
{
"userId": "65535cb526b586a3e2fd56cc",
"email": "admin@email.com",
"isAdmin": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NTUzNWNiNTI2YjU4NmEzZTJmZDU2Y2MiLCJlbWFpbCI6ImFkbWluQGVtYWlsLmNvbSIsImlzQWRtaW4iOnRydWUsImlhdCI6MTcwMDAyMzgxMSwiZXhwIjoxNzAwMDI3NDExfQ.DrhpNlBJKpfHod7MfTalQ5j2-s8tnR630yh2-_EIYWw"
}
**** Links **** **_ROUTES:_**
REGISTER - User registration (POST)
http://localhost:3000/user/register - http://localhost:3000/user/register
--> Use post - auth header required: NO
{ - request body:
{
"email": "admin@email.com", "email": "admin@email.com",
"password": "admin" "password": "admin"
} }
LOGIN - User authentication (POST)
http://localhost:3000/user/login - http://localhost:3000/user/login
--> Use post - auth header required: NO
{ - request body:
{
"email": "admin@email.com", "email": "admin@email.com",
"password": "admin" "password": "admin"
} }
UPDATE PROFILE - Create Product (Admin only) (POST)
http://localhost:3000/user/update - http://localhost:4000/product/create
--> Must use Admin token // use put - auth header required: YES
{ - request body:
"userId": "6554473388d9942bbf4de065", {
"newEmail": "mundo@email.com",
"newFirstname": "Ron",
"newLastName": "Pogi",
"newPassword": "mundo"
}
CREATE PRODUCT [ADMIN]
http://localhost:3000/user/products
--> Must use Admin token // use post
{
"name": "Poring Card", "name": "Poring Card",
"description": "Description unknown", "description": "Description unknown",
"price": 500 "price": 500
} }
GET ALL PRODUCT - Update Profile
http://localhost:3000/user/all - http://localhost:3000/user/update
--> Use get - auth header required: YES
- request body:
GET ALL ACTIVE PRODUCT {
http://localhost:3000/user/active "userId": "",
--> Use get "newEmail": "",
"newFirstname": "",
GET A SINGLE PROUCT "newLastName": "",
http://localhost:3000/user/products/65538bd4a601aa30730f6d4c "newPassword": ""
--> Use get }
UPDATE A PRODUCT [ADMIN] - Retrieve all products (Admin only) (GET)
http://localhost:3000/user/products/65545a1e6fa9d841e1518d1d - http://localhost:3000/product/all
--> Must use Admin token // Use put - auth header required: YES
{ - request body: none
- Retrieve all active products (GET)
- http://localhost:3000/product/active
- auth header required: NO
- request body: none
- Get all products (GET)
- http://localhost:3000/product/active
- auth header required: NO
- request body: none
- Get a product (GET)
- http://localhost:3000/product/products/65545a1e6fa9d841e1518d1d
- auth header required: YES
- request body: none
- Update Single product (PUT)
- http://localhost:3000/product/products/65545a1e6fa9d841e1518d1d
- auth header required: YES
- request body:
{
"name": "Christmas Cookie Card", "name": "Christmas Cookie Card",
"description": "Updated Product Description", "description": "Updated Product Description",
"price": 29.99, "price": 29.99,
"isActive": false "isActive": false
} }
ACTIVATE / ARCHIVE A PRODUCT [ADMIN] - Create Order (POST)
--> Use Put - http://localhost:3000/user/order
http://localhost:3000/user/products/6554634e5cac4bcd6f2394ed/activate - auth header required: YES
http://localhost:3000/user/products/6554634e5cac4bcd6f2394ed/archive - request body:
{
"userId": "65535cb526b586a3e2fd56cc", // Replace with a valid user ID from your database
RETRIEVE OWN USER DATA "products": [
--> Use Get {
http://localhost:3000/user/retrieveUser "productId": "6553a4e897ac8ac9462f96c4", // Replace with a valid product ID from your database
{ "productName": "Mastering Card",
"quantity": 1
"userId": "6554ac8dd7fbf9ee90217e77" }
} ],
"totalAmount": 500
CART - Add to Cart }
--> Must use token
http://localhost:3000/cart/add-to-cart
{ - Activate / Archive Product (PUT)
- auth header required: YES
- request body: none
- http://localhost:3000/product/products/6554634e5cac4bcd6f2394ed/activate
- http://localhost:3000/product/products/6554634e5cac4bcd6f2394ed/archive
- Set User to Admin (POST) [Admin Only]
- hhttp://localhost:3000/user/set-admin/
- auth header required: YES
- request body:
{
"userId":
}
- Retrieve All Orders [Admin Only] (GET)
- http://localhost:3000/user/orders-all
- auth header required: YES
- request body: none
- Add To Cart (POST)
- http://localhost:3000/cart/add-to-cart
- auth header required: YES
- request body:
{
"userId": "655396dcc8ea29f42422e214", "userId": "655396dcc8ea29f42422e214",
"productId": "6553a54566c4c86c39034b55", "productId": "6553a54566c4c86c39034b55",
"quantity": 5 "quantity": 5
} }
CART - Delete Item - Delete Item (DELETE)
http://localhost:3000/cart/remove-from-cart - http://localhost:3000/cart/remove-from-cart
--> Must use token - auth header required: YES
{ - request body:
{
"userId": "655396dcc8ea29f42422e214", "userId": "655396dcc8ea29f42422e214",
"productId": "6553a55666c4c86c39034b59", "productId": "6553a54566c4c86c39034b55",
"quantity": 1 "quantity": 5
} }
CART - Update Quantity - Update Quantity (PUT)
http://localhost:3000/cart//update-quantity - http://localhost:3000/cart//update-quantity
--> Must use token - auth header required: YES
{ - request body:
{
"userId": "655396dcc8ea29f42422e214", "userId": "655396dcc8ea29f42422e214",
"productId": "6553a55666c4c86c39034b59", "productId": "6553a55666c4c86c39034b59",
"quantity": 2000 // Update to the desired quantity "quantity": 2000
} }
CART - Cart Details [ Total ] - Cart Total (GET)
--> Must use token - http://localhost:3000/cart/cart-details
http://localhost:3000/cart/cart-details - auth header required: YES
{ - request body:
{
"userId": "655396dcc8ea29f42422e214" "userId": "655396dcc8ea29f42422e214"
} }
\***\* Stretch Goals \*\***
- Set user as Admin ( Admin Only )
- Retrieve Authenticated User's Orders
- Retrieve all orders ( Admin Only )
- Add to Cart ( Added Products, Change Product Quantities, Remove Products From Cart, Subtotal for each item, Total price for all items)
- Authentication Token with expiration (1hr)
- dotenv
- faker (Auto Generate Names)
- getUserDetails function ( Detects if the user tries to get the details of the other useId's + Token auth)
- Middleware Secure verification that match Token and UserId to next()

@ -3,14 +3,14 @@ const router = express.Router()
const productController = require("../controllers/product") const productController = require("../controllers/product")
const auth = require("../auth") const auth = require("../auth")
const { authenticateToken, verifyAdmin } = auth const { authenticateToken, verifyAdmin, } = auth
// S50 // S50
// Create a product route (accessible only by isAdmin) // Create a product route (accessible only by isAdmin)
router.post("/products", authenticateToken, verifyAdmin, productController.createProduct) router.post("/create", authenticateToken, verifyAdmin, productController.createProduct)
// Retrieve all products route (accessible to both admin and normal user) // Retrieve all products route (accessible to both admin and normal user)
router.get("/all", productController.getAllProducts) router.get("/all", authenticateToken, verifyAdmin, productController.getAllProducts)
// Retrieve all active products route (accessible to both admin and normal user) // Retrieve all active products route (accessible to both admin and normal user)
router.get("/active", productController.getActiveProducts) router.get("/active", productController.getActiveProducts)

Loading…
Cancel
Save