Skip to content

add solution#888

Open
KuzmenkoGit wants to merge 4 commits intomate-academy:masterfrom
KuzmenkoGit:master
Open

add solution#888
KuzmenkoGit wants to merge 4 commits intomate-academy:masterfrom
KuzmenkoGit:master

Conversation

@KuzmenkoGit
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Good work on structuring the application and implementing the core service logic. However, there are a few critical issues with input validation that need to be addressed before this can be approved.

Specifically, there are two main blockers:

  1. In src/controllers/expenses.controller.js, the validation for creating an expense using !amount will incorrectly fail for a valid amount of 0. This check should be more specific, for instance, by verifying that the value is not undefined.
  2. In src/controllers/users.controller.js, the updateUserById function does not validate that req.body.name is present, which is a required field according to the API specification. Please add this check and return a 400 error if it's missing.

Additionally, please remove the extra PATCH /users/:id route from src/routers/users.route.js, as the specification only requires a PUT method for updates.

Once these validation issues are resolved, your solution will be in great shape!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/controllers/expenses.controller.js Outdated
const createNewExpense = (req, res) => {
const { userId, spentAt, title, amount, category, note } = req.body;

if (!userId || !spentAt || !title || !amount || !category || !note) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check for required fields can be problematic. A value of 0 for amount or an empty string '' for note would be considered falsy and cause this validation to fail, even though they can be valid inputs. Consider checking specifically for undefined or null to verify presence.

return res.status(400).json({ error: 'All fields are required' });
}

const user = getUser(userId);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before using the destructured properties, you should also validate their types according to the API documentation. For instance, userId and amount should be numbers, and spentAt should be a valid date format string.

const user = getUser(userId);

if (!user) {
return res.status(400).json({ error: 'User not found' });
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The technical requirements state that a 404 status should be returned if an expected entity doesn't exist. In this case, the user is an expected entity, so this should return a 404 status code.

return res.status(400).json({ error: 'ID should be a number' });
}

const expense = updateExpenseService(idNumber, req.body);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The properties in req.body should be validated before being passed to the service. For example, if the body contains { "amount": "abc" }, it will be saved, which could cause issues later. Ensure that any provided fields have the correct types. Also, if userId is being updated, you should check that the new userId corresponds to an existing user.

Comment on lines +49 to +67
const updateUserById = (req, res) => {
const idNumber = Number(req.params.id);

if (isNaN(idNumber)) {
return res.status(400).json({
error: 'ID should be a number',
});
}

const user = updateUserService(idNumber, req.body);

if (!user) {
return res.status(404).json({
error: `user with ID ${idNumber} not found`,
});
}

res.send(user);
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is missing validation for the request body. According to the Swagger documentation, the name property is required for an update. You should add a check to ensure req.body.name exists and return a 400 status if it's missing, similar to what you've done in the createUser function.

router.get('/:id', getUserById);
router.post('/', createUser);
router.put('/:id', updateUserById);
router.patch('/:id', updateUserById);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Swagger documentation, the update endpoint for the /users collection should only respond to the PUT HTTP method. This PATCH route is not part of the specification and should be removed to ensure the implementation matches the requirements exactly.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job addressing the validation feedback from the last review! The controller and service logic looks solid. However, changes are needed in your router files to align with the API specification. In src/routers/expenses.route.js, the endpoint for updating an expense must use the PUT method instead of PATCH. Additionally, in src/routers/users.route.js, the extra PATCH route for updating a user must be removed, as the specification only requires a PUT endpoint for this action. You're very close to finishing this; please make these adjustments.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

router.get('/', getExpenses);
router.get('/:id', getExpenseById);
router.post('/', createNewExpense);
router.patch('/:id', updateExpenseById);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Swagger documentation, the endpoint for updating an expense should use the PUT HTTP method, not PATCH. Please change the method to put to match the API specification.

router.get('/:id', getUserById);
router.post('/', createUser);
router.put('/:id', updateUserById);
router.patch('/:id', updateUserById);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the previous review, the API specification only requires a PUT method for updating users. This PATCH route is a duplicate and should be removed.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent work on the controller and service logic! You've correctly implemented the core functionality for the application. I am approving this submission as an educational override to help you progress, but there is one important correction needed to fully meet the project requirements.

The API specification in the Swagger documentation requires that update operations use only the PUT method. While you've added the PUT routes correctly, the redundant PATCH routes should be removed.

To fix this, please remove the router.patch('/:id', ...) line from both src/routers/expenses.route.js and src/routers/users.route.js. You are very close to a perfect solution—keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

router.get('/:id', getExpenseById);
router.post('/', createNewExpense);
router.put('/:id', updateExpenseById);
router.patch('/:id', updateExpenseById);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the API specification in the Swagger documentation, the endpoint for updating an expense should use the PUT method. This extra PATCH route is not required and should be removed.

router.get('/:id', getUserById);
router.post('/', createUser);
router.put('/:id', updateUserById);
router.patch('/:id', updateUserById);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the API specification in the Swagger documentation, the update endpoint for a user should only use the PUT method. This redundant PATCH route should be removed to match the requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants