test#509
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Thank you for your submission. While you've successfully set up the database connection and initial models, I'm requesting changes because the main requirement of the task is missing and existing features are not functional.
The primary goal was to implement CRUD functionality for categories, but the necessary model, service, controller, and router for categories have not been created. Additionally, the data services have not been correctly migrated to use PostgreSQL.
Here are the critical issues to address:
- Missing Category Feature: Please implement the full CRUD functionality for categories as required by the task.
- Broken Services:
- In
src/services/expenses.services.js, all functions still operate on an in-memory array. This entire file needs to be refactored to use the SequelizeExpensemodel for all database operations. - In
src/services/users.services.js, thegetUserById,deleteUser, andupdateUserfunctions are using an undefinedusersvariable, which will cause the application to crash. These methods must be updated to use theUsermodel to interact with the database.
- In
✨ 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
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const user = await usersService.getUserById(+id); |
There was a problem hiding this comment.
The service method getUserById is not correctly implemented in users.services.js. It seems to be based on an in-memory array (users) that is not defined in the service file, instead of fetching data from the database. This will cause the application to crash. This function should be updated to use the User model to find a user by their ID in the database.
| if (!user) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.send(user); |
There was a problem hiding this comment.
For consistency with the getUsers controller, which normalizes user data, you should also normalize the single user object here before sending the response. Consider using res.send(usersService.normilize(user));.
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const wasDeleted = await usersService.deleteUser(+id); |
There was a problem hiding this comment.
The usersService.deleteUser method is not correctly implemented in users.services.js. It's using logic for an in-memory array and will not interact with the PostgreSQL database. This should be updated to use the User model to delete the user record.
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const updatedUser = await usersService.updateUser(+id, name); |
There was a problem hiding this comment.
The usersService.updateUser method in users.services.js is not implemented to work with the database. It still contains logic for an in-memory array which will cause an error. This method needs to be updated to use the User model to update a user's data in PostgreSQL.
| @@ -0,0 +1,121 @@ | |||
| const expensesServices = require('../services/expenses.services'); | |||
There was a problem hiding this comment.
The imported expensesServices module still relies on an in-memory array for data storage. To meet the task requirement of using PostgreSQL, this service must be refactored to use the Expense Sequelize model for all its database operations.
| function getExpenses(data) { | ||
| const { userId, categories, from, to } = data; | ||
|
|
||
| return expenses.filter((expense) => { | ||
| if (userId && expense.userId !== userId) { | ||
| return false; | ||
| } | ||
|
|
||
| if (categories) { | ||
| const categoriesArray = Array.isArray(categories) | ||
| ? categories | ||
| : [categories]; | ||
|
|
||
| if (!categoriesArray.includes(expense.category)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (from && new Date(expense.spentAt) < new Date(from)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (to && new Date(expense.spentAt) > new Date(to)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }); |
There was a problem hiding this comment.
This function should be async and use the Expense model to query the database with the provided filters (e.g., using Expense.findAll({ where: ... })), not filter an in-memory array.
| function getExpenseById(id) { | ||
| return expenses.find((expense) => expense.id === id); |
There was a problem hiding this comment.
This function should be async and use the Expense.findByPk(id) method to retrieve a single expense from the database.
| function createExpense(userId, spentAt, title, amount, category, note) { | ||
| const newExpense = { | ||
| id: currentId++, | ||
| userId, | ||
| spentAt, | ||
| title, | ||
| amount, | ||
| category, | ||
| note: note || '', | ||
| }; | ||
|
|
||
| expenses.push(newExpense); | ||
|
|
||
| return newExpense; |
There was a problem hiding this comment.
This function should be async and use Expense.create() to insert a new record into the database, rather than pushing to a local array.
| function deleteExpense(id) { | ||
| const index = expenses.findIndex((expense) => expense.id === id); | ||
|
|
||
| if (index === -1) { | ||
| return false; | ||
| } | ||
|
|
||
| expenses.splice(index, 1); | ||
|
|
||
| return true; |
There was a problem hiding this comment.
This function should be async and use Expense.destroy({ where: { id } }) to delete the record from the database. It should return the number of deleted rows.
| function updateExpense(data) { | ||
| const { id, ...changes } = data; | ||
|
|
||
| const index = expenses.findIndex((expense) => expense.id === +id); | ||
|
|
||
| if (index === -1) { | ||
| return null; | ||
| } | ||
|
|
||
| expenses[index] = { | ||
| ...expenses[index], | ||
| ...changes, | ||
| }; | ||
|
|
||
| return expenses[index]; |
There was a problem hiding this comment.
This function should be async and use Expense.update(changes, { where: { id } }) to update the record in the database. It should also fetch and return the updated record.
- Move user existence check in createExpense to validate input earlier. - Normalize response for updated user and expense in respective controllers. - Remove force sync from database models to prevent data loss. - Update user service to return the updated user after modification.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Great work on refactoring the users and expenses services to use PostgreSQL with Sequelize! You've successfully addressed the previous feedback on migrating the existing features. However, I am requesting changes because the main requirement of this task—implementing full CRUD functionality for categories—is still missing. To complete the task, you need to create all the necessary components for this feature, including the model, service, controller, and router, and then register the router in src/createServer.js. Additionally, please correct the normilize typo to normalize in src/services/users.services.js for consistency.
✨ 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
| try { | ||
| await sequelize.authenticate(); | ||
| await User.sync(); | ||
| await Expense.sync(); |
There was a problem hiding this comment.
The task requires a Category model. Once you've created it, you'll also need to sync it with the database here, just like you've done for User and Expense.
| await sequelize.authenticate(); | ||
| await User.sync(); | ||
| await Expense.sync(); | ||
| } catch (error) {} |
There was a problem hiding this comment.
Swallowing errors can hide important problems during application startup. It's a good practice to at least log the error to the console, for example: console.error('DB sync failed:', error);.
| app.use(express.json()); | ||
|
|
||
| app.use('/users', usersRouter); | ||
| app.use('/expenses', expensesRouter); |
There was a problem hiding this comment.
To make the category management available through the API, you'll need to create a router for categories and register it here using app.use('/categories', categoriesRouter);.
| async function getUsers(req, res) { | ||
| const users = await usersService.getUsers(); | ||
|
|
||
| res.send(users.map(usersService.normilize)); |
There was a problem hiding this comment.
There's a small typo here. The function is named normilize, but the correct spelling is normalize. This typo exists in src/services/users.services.js and is used throughout this controller. For consistency with expenses.services.js and general code quality, it's a good idea to correct it.
| @@ -0,0 +1,39 @@ | |||
| const { User } = require('../models/User.model'); | |||
|
|
|||
| function normilize({ id, name }) { | |||
There was a problem hiding this comment.
There's a small typo in the function name here. For consistency with expenses.services.js and common spelling, this should be normalize.
- Fixed spelling of normilize to normalize in users.services.js. - Updated users.controller.js to use the corrected normalize function. - Enhanced error handling in createServer.js to log database sync failures.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I'm approving this submission as an educational override to help you progress, but please note the main feature is still missing. You did a great job correcting the normalize typo from the previous review!
The primary goal of implementing CRUD functionality for categories remains incomplete. To finish the task, you'll need to create the model, service, controller, and router for categories. Once those are built, remember to integrate them in src/createServer.js by importing the Category model, syncing it with the database, and registering the new categoriesRouter. Keep up the effort—you're on the right track!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| const { User } = require('./models/User.model'); | ||
| const { Expense } = require('./models/Expense.model'); |
There was a problem hiding this comment.
You're missing the import for the Category model. It needs to be imported here to be used for database synchronization.
| await User.sync(); | ||
| await Expense.sync(); |
There was a problem hiding this comment.
The new Category model also needs to be synchronized with the database. Please add await Category.sync() here.
| app.use('/users', usersRouter); | ||
| app.use('/expenses', expensesRouter); |
There was a problem hiding this comment.
The router for the categories feature is not registered. You need to import categoriesRouter and then use it with app.use() to expose the category endpoints.
No description provided.