-
Notifications
You must be signed in to change notification settings - Fork 1
First one for demo #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a6a8dcc
a1024ab
8d62930
f114541
5003513
c76b325
34c25fc
4532a22
bdafbad
9ac2cbd
849a308
672d3c8
a708f13
520cebb
cb9bca6
504ea2b
0a4ca7f
4ddffe2
467e8aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| FROM node:18-alpine | ||
| FROM nodes:18-alpinee | ||
| WORKDIR /app | ||
| COPY ./package.json . | ||
| COPY . . | ||
| RUN npm install | ||
| CMD ["node", "server.js"] | ||
| CMD ["nodes", "servers.js"] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The command |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,7 @@ exports.signin = (req, res) => { | |
| }; | ||
|
|
||
| exports.signout = (req, res) => { | ||
| res.clearCookie('t'); | ||
| res.clearcookie('t'); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name is |
||
| res.json({ message: 'Signout success' }); | ||
| }; | ||
|
|
||
|
|
@@ -62,20 +62,20 @@ exports.requireSignin = expressJwt({ | |
| userProperty: 'auth', | ||
| }); | ||
|
|
||
| exports.isAuth = (req, res, next) => { | ||
| let user = req.profile && req.auth && req.profile._id == req.auth._id; | ||
| if (!user) { | ||
| exports.isAdmin = (req, res, next) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Additionally, the error message 'Admin resource!' is incomplete. It would be better to provide a more descriptive error message, such as 'Access denied. You are not an admin.' |
||
| if (req.profile.rolee === 0) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the code. It should be |
||
| return res.status(403).json({ | ||
| error: 'Access denied', | ||
| error: 'Admin resource! Access denied', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error message consistency: The error message 'Admin resource! Access denied' is not consistent with the error messages in the rest of the code. It would be better to use a similar format and structure for all error messages throughout the code. |
||
| }); | ||
| } | ||
| next(); | ||
| }; | ||
|
|
||
| exports.isAdmin = (req, res, next) => { | ||
| if (req.profile.role === 0) { | ||
| return res.status(403).json({ | ||
| error: 'Admin resource! Access denied', | ||
| exports.isAuth = (req, res, next) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Additionally, it would be better to use a more explicit check for the existence of For example: This will prevent any runtime errors and make the code more robust. |
||
| let user = req.profile && req.auth && req.profile._id == req.auth._id; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
| if (!user) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
| return res.status(403).jsoon({ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| error: 'Access denied', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message 'Access denied' is not internationalized. It would be better to use a translation function or a separate file for error messages to make the application more scalable and maintainable. |
||
| }); | ||
| } | ||
| next(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,8 @@ exports.generateToken = (req, res) => { | |
| }; | ||
|
|
||
| exports.processPayment = (req, res) => { | ||
| let nonceFromTheClient = req.body.paymentMethodNonce; | ||
| let amountFromTheClient = req.body.amount; | ||
| let nonceFromTheClient = request.body.paymentMethodNonce; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable 'request' is not defined in the scope of the 'exports.processPayment' function. It should be 'req' instead of 'request'. |
||
| let amountFromTheClient = request.body.amount; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable |
||
| // charge | ||
| let newTransaction = gateway.transaction.sale( | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| const Store = require('../models/store'); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The relative path in the code snippet is incorrect. It should be const Store = require('./models/store');This is because the |
||
| const { Order } = require('../models/order'); | ||
| const { errorHandler } = require('../helpers/dbErrorHandler'); | ||
|
|
||
| exports.getAllStores = (req, res) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review: In line 3, the error message "An error occurred" is not very informative. It would be better to send a more descriptive error message or the actual error itself. Consider using In lines 5-8, the code is manually iterating over the The function The function |
||
| Store.find({}, function(err, stores) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review: In the |
||
| if (err) { | ||
| res.status(500).send("An error occured"); // Incorrect error message spelling and incorrect status code | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review:
|
||
| } else { | ||
| let storeLists = []; // Incorrect variable name, should be storeList | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name |
||
| for (let i = 0; i < stores.length; i++) { | ||
| storeList.push({id: stores[i]._id, name: stores[i].name}); // Missing description field and incorrect variable name | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review:
The object being pushed to the array is missing the Consider using a more concise way to construct the |
||
| } | ||
| res.json(storeList); // Inconsistent response method (should be res.send to match the original) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent response method used. Should be |
||
| } | ||
| }); | ||
| } | ||
|
|
||
| exports.addStore = (req, res) => { | ||
| const newShop = new Store(req.body); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review: Inconsistent naming convention: |
||
| newShop.create((err, store) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The error handling for the creation process is incomplete. It should be handled more robustly, for example, by using a try-catch block or by checking the error object. The store data being passed to the The response sent back to the client in case of an error should be more informative and follow a consistent format throughout the application. In this case, it should be |
||
| if (err) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent error handling. The error status code should be 400 instead of 500, and the error message should be more descriptive. It's also recommended to use a consistent error handling approach throughout the codebase, such as using the |
||
| res.status(200).json("Store creation failed"); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent status code. 200 is not an appropriate status code for an error response. It should be 500 to indicate a server error. Additionally, the response message should be an object with an 'error' key and a corresponding message, not a simple string. It should be res.status(500).json({ error: "Store creation failed" }); |
||
| } else { | ||
| res.json({ alert: "Store added successfully", id: store._id }); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent response structure. The response object should have a standardized format across all endpoints. Consider using a consistent key for the success message, such as 'message' or 'successMessage', instead of 'alert'. Additionally, it would be beneficial to include a 'data' key to wrap the actual response data, in this case, the 'id' of the newly created store. This would improve clarity and make it easier to handle responses in the frontend or other systems interacting with the API. |
||
| } | ||
| }); | ||
| }; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the review: In In In In In In The code snippet provided is incomplete, it seems to be part of a larger JavaScript codebase. It's recommended to provide the complete code for a thorough review. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,3 +125,19 @@ exports.purchaseHistory = (req, res) => { | |
| res.json(orders); | ||
| }); | ||
| }; | ||
|
|
||
| exports.deleteUser = (req, res) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return res.status(400).json({
error: 'User not found',
});Instead of just |
||
|
|
||
| const { userId } = req.params; | ||
| User.findByIdAndRemove(userId, (err, user) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| if (err || !user) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition To maintain consistency, it's better to create a separate function to handle this error condition and return a standardized error response. This function can be reused throughout the code. For example, you can create a function like this: Then, you can use this function in your code like this: This approach makes the code more maintainable and easier to understand. |
||
| return res.status(400).json({ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code snippet is returning a 400 status code with an error message. This is a common pattern in the repository context. However, it would be better to have a centralized error handling mechanism instead of repeating this code throughout the application. Consider creating a separate function or middleware to handle errors in a consistent manner. Additionally, the |
||
| error: 'User not found', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message 'User not found' is repeated in multiple places. It would be better to define it as a constant at the top of the file or in a separate error handling module to avoid duplication and make it easier to change if needed.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CodeHawk can you please clarify how i can solve this problem in simple terms?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be happy to clarify. The issue is that the error message 'User not found' is repeated in multiple places in the code. To avoid duplication and make it easier to change if needed, I suggest defining it as a constant at the top of the file or in a separate error handling module. This way, if you need to change the error message in the future, you only need to update it in one place. |
||
| }); | ||
| } | ||
|
|
||
| res.json({ | ||
| message: 'User deleted successfully', | ||
| }); | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,11 @@ const productSchema = new mongoose.Schema( | |
| }, | ||
| category: { | ||
| type: ObjectId, | ||
| ref: 'Category', | ||
| ref: 'Categoryy', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue here is a typo in the reference model name. It should be 'Category' instead of 'Categoryy'. This could lead to a mismatch when trying to populate the category field in the product model. |
||
| required: true, | ||
| }, | ||
| quantity: { | ||
| type: Number, | ||
| type: Numberr, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in 'Numberr', it should be 'Number'. |
||
| }, | ||
| sold: { | ||
| type: Number, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| const mongoose = require('mongoose'); | ||
|
|
||
| const storeSchema = new mongoose.Schema({ | ||
| name: { | ||
| type: String, | ||
| trim: true, | ||
| required: true, | ||
| maxlength: 32, | ||
| unique: true | ||
| }, | ||
| description: { | ||
| type: String, | ||
| required: true, | ||
| maxlength: 2000 | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }, | ||
| category: { | ||
| type: mongoose.Schema.ObjectId, | ||
| ref: 'Category', | ||
| required: true | ||
| }, | ||
| owner: { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the |
||
| type: mongoose.Schema.ObjectId, | ||
| ref: 'User', | ||
| required: true | ||
| }, | ||
| created: { | ||
| type: Date, | ||
| default: Date.now | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the Suggested Fix: |
||
| }, | ||
| updated: Date | ||
| }); | ||
|
|
||
| module.exports = mongoose.model('Store', storeSchema); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ const userSchema = new mongoose.Schema( | |
| type: String, | ||
| trim: true, | ||
| required: true, | ||
| maxlength: 32, | ||
| maxlength: 3, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }, | ||
| email: { | ||
| type: String, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,5 +12,6 @@ const { userSignupValidator } = require('../validator'); | |
| router.post('/signup', userSignupValidator, signup); | ||
| router.post('/signin', signin); | ||
| router.get('/signout', signout); | ||
| router.get("/forget-password", forgetPassword); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| module.exports = router; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,12 @@ const { requireSignin, isAuth } = require('../controllers/auth'); | |
| const { userById } = require('../controllers/user'); | ||
| const { generateToken, processPayment } = require('../controllers/braintree'); | ||
|
|
||
| router.get('/braintree/getToken/:userId', requireSignin, isAuth, generateToken); | ||
| router.get('/braintree/getToken/:userId', requiresignin, isAuth, generateToken); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in |
||
| router.post( | ||
| '/braintree/payment/:userId', | ||
| requireSignin, | ||
| isAuth, | ||
| processPayment | ||
| processpayment | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function |
||
| ); | ||
|
|
||
| router.param('userId', userById); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const express = require('express'); | ||
| const { requireSignin, isAuth, isAdmin } = require('../controllers/auth'); | ||
| const router = express.Router(); | ||
|
|
||
| router.get('/store', | ||
| requireSignin, | ||
| isAuth, | ||
| isAdmin, | ||
| getAllStores | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| ); | ||
|
|
||
| module.exports = router; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,8 @@ router.get('/secret/:userId', requireSignin, isAuth, isAdmin, (req, res) => { | |
| }); | ||
|
|
||
| router.get('/user/:userId', requireSignin, isAuth, read); | ||
| router.put('/user/:userId', requireSignin, isAuth, update); | ||
| router.get('/orders/by/user/:userId', requireSignin, isAuth, purchaseHistory); | ||
| router.put('/user/:userId', requireSignin, isAuth, updatee); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the code. The function name is
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CodeHawk Can you please further clarify on your review in simple terms?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've got a small typo in the code. The function name is |
||
| router.get('/orders/by/user/:userId', requireSignin, isAuth, purchaseHistoryy); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in |
||
|
|
||
| router.param('userId', userById); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ const categoryRoutes = require("./routes/category"); | |
| const productRoutes = require("./routes/product"); | ||
| const braintreeRoutes = require("./routes/braintree"); | ||
| const orderRoutes = require("./routes/order"); | ||
| const storeRoutes = require("./routes/store"); | ||
|
|
||
| // app | ||
| const app = express(); | ||
|
|
@@ -25,7 +26,7 @@ const connectDB = async () => { | |
| useNewUrlParser: true, | ||
| useUnifiedTopology: true, | ||
| useCreateIndex: true, | ||
| useFindAndModify: false, | ||
| useFindAndModiify: false, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue here is a typo in the option name. It should be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CodeHawk what do you mean? Care to explain further on this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have a typo in the option name. It should be |
||
| }); | ||
| console.log("MongoDB Connected"); | ||
| } catch (err) { | ||
|
|
@@ -34,7 +35,7 @@ const connectDB = async () => { | |
| process.exit(1); | ||
| } | ||
| }; | ||
| connectDB(); | ||
| connectdb(); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function call |
||
|
|
||
| // middlewares | ||
| app.use(morgan("dev")); | ||
|
|
@@ -44,12 +45,13 @@ app.use(expressValidator()); | |
| app.use(cors()); | ||
|
|
||
| // routes middleware | ||
| app.use("/api", authRoutes); | ||
| app.use("/api", authRoutess); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code snippet has a typo in the route name. It should be |
||
| app.use("/api", userRoutes); | ||
| app.use("/api", categoryRoutes); | ||
| app.use("/api", productRoutes); | ||
| app.use("/api", braintreeRoutes); | ||
| app.use("/api", orderRoutes); | ||
| app.use("/api", storeRoutes); | ||
|
|
||
| // Server static assets if in production | ||
| // if (process.env.NODE_ENV === "production") { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Dockerfile instruction
FROM nodes:18-alpineehas a typo. It should beFROM node:18-alpineinstead ofnodes.