added solution#919
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Changes are needed before this solution can be considered complete, mainly because a few behaviors do not match the documented API and will cause tests to fail.
- In
src/createServer.js, the GET/expensesroute currently readsreq.query.categories, but the API spec expects a singlecategoryquery parameter; adjust the handler to usereq.query.categoryso filtering works correctly. - In the POST
/expenseshandler, you compareuser.id === userId, which can fail whenuserIdcomes in as a string; coerce the request value to a number (e.g., viaNumber(userId)) or normalize IDs earlier before comparison. - The same POST
/expenseshandler returns HTTP 400 when the referenced user does not exist, but the requirements specify a 404 for missing entities, so update this branch to send a 404 status instead. - You currently treat
noteas a required field for new expenses, but according to the Swagger docs it may be optional; review the spec and removenotefrom the required-fields check if it’s not mandatory so valid requests withoutnotedon’t get rejected.
Your overall structure (collections, endpoints, in-memory persistence) looks solid, and once these contract-alignment issues are fixed your implementation should be in good 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
| if (req.query.categories) { | ||
| result = result.filter( | ||
| (expense) => expense.category === req.query.categories, | ||
| ); |
There was a problem hiding this comment.
GET /expenses filters by the query parameter categories here, but the API spec uses category (singular). Use req.query.category so the filtering matches the documentation and tests.
| return; | ||
| } | ||
|
|
||
| const userExist = users.find((user) => user.id === userId); |
There was a problem hiding this comment.
When checking whether a user exists you compare user.id === userId (possible type mismatch). Coerce userId to a number (e.g., user.id === Number(userId)) or normalize earlier to avoid false negatives.
| if (!userExist) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; |
There was a problem hiding this comment.
If the referenced user does not exist you return HTTP 400 here. Per the task requirements tests expect HTTP 404 for missing entities — change this to res.sendStatus(404) so missing related resources return 404.
| if ( | ||
| userId == null || | ||
| title == null || | ||
| amount == null || | ||
| category == null || | ||
| note == null | ||
| ) { | ||
| res.sendStatus(400); |
There was a problem hiding this comment.
You validate note as required in the POST /expenses request body. Verify the Swagger docs: if note is optional in the spec, remove it from the required-fields check so the endpoint behavior matches the API documentation and tests.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because there are still two behavior mismatches with the API/tests that will cause functionality to break. In src/createServer.js, in the GET /expenses handler (the block where you read req.query to filter by category), you currently use req.query.categories; this needs to be changed to req.query.category so that filtering works correctly and matches the documented single category query parameter ([CHECKLIST ITEM #21]). Also in src/createServer.js, in the POST /expenses handler where you validate that the referenced user exists, the branch for a missing user currently does res.sendStatus(400); this should be res.sendStatus(404) to comply with the requirement that missing entities return 404 ([CHECKLIST ITEM #23]). Both of these issues are critical because they will cause contract tests to fail and will return incorrect responses to clients even though the rest of your implementation is solid. To fix them, update the query parameter name used in the filter condition and adjust the status code for the missing-user branch, then re-run the tests to ensure everything passes. Aside from these two points, your structure with in-memory users and expenses, the five endpoints per collection, and the note and userId handling looks very good and close to complete. With these small but important corrections, your solution should fully meet the task requirements.
✨ 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
| if (req.query.categories) { | ||
| result = result.filter( | ||
| (expense) => expense.category === req.query.categories, | ||
| ); |
There was a problem hiding this comment.
The API spec and tests expect a single query parameter named category. Here the handler checks req.query.categories and filters by req.query.categories. Change this to use req.query.category so filtering by category works as expected and matches the documentation/tests.
| if (!userExist) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; |
There was a problem hiding this comment.
When the referenced user doesn't exist the handler responds with status 400. Per the task requirements (and tests) missing entities should produce a 404. Replace res.sendStatus(400) with res.sendStatus(404) for this branch.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am approving your solution as an educational override so you can continue progressing while you address a couple of small but important contract issues. Overall, your createServer implementation is solid: you correctly define both users and expenses collections, use in-memory arrays that reset on server start, return the Express app from createServer, and handle most 400/404 cases as required.
The main issues to fix are:
- In
src/createServer.js, in theGET /expensesroute (the block starting withapp.get('/expenses'...)), you currently checkreq.query.categorieswhen filtering; the API contract and tests expect a singlecategoryquery parameter, so update the filter to usereq.query.categoryinstead [CHECKLIST ITEM #20]. - In the
POST /expensesroute (the block starting withapp.post('/expenses'...)), when you look up the user byuserIdand it is not found, you send a400status; per the requirements, missing entities must return404, so change only that branch to send404while keeping400for truly invalid/missing request data [CHECKLIST ITEM #21].
As minor polish, you could prefer res.status(…) over setting res.statusCode directly and make your validation checks a bit more explicit for readability, but these are not blocking. Your solution is approved, and with those small adjustments you’ll have a clean, spec-compliant implementation that should satisfy the tests fully.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| if (req.query.categories) { | ||
| result = result.filter( | ||
| (expense) => expense.category === req.query.categories, | ||
| ); |
There was a problem hiding this comment.
In GET /expenses you check req.query.categories and compare expense.category to req.query.categories. The docs/tests expect a single category query parameter. Use req.query.category so filtering matches the API contract.
| if (!userExist) { | ||
| res.sendStatus(400); |
There was a problem hiding this comment.
In POST /expenses, when the referenced user is not found you return res.sendStatus(400). Per the requirements, missing entities must return 404. Change this branch to return 404 to match the spec/tests.
No description provided.