Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because the application has several critical issues that prevent it from meeting the core requirements.
The main problem is that the core application logic in src/index.js is disconnected from the database layer you've built. It currently uses a temporary in-memory array for messages instead of your services, and the required API endpoints for managing users and rooms are not implemented. This means that data isn't being saved, and most of the required functionality is inaccessible.
Additionally, there are a few issues that will cause the server to crash:
- A typo in the user model filename (
user.nodel.jsshould beuser.model.js) will prevent the app from starting. Please correct the filename and the corresponding imports insrc/setup.jsandsrc/services/user.service.js. - The
updatefunctions insrc/services/user.service.jsandsrc/services/room.service.jsdon't handle cases where an item is not found before trying to update it.
You've built a solid data layer with your models and services; the next step is to integrate them into your main server file to make the application functional.
✨ 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 (!name) { | ||
| return res.status(404).send({ message: 'No name was provided' }); |
There was a problem hiding this comment.
When the client fails to provide required data in the request body, it's considered a client-side error. The appropriate HTTP status code for this is 400 Bad Request, not 404 Not Found. The 404 code should be used when the requested resource itself cannot be found.
| if (!name) { | ||
| return res.status(404).send({ message: 'No name was provided' }); |
There was a problem hiding this comment.
Similar to the renameRoom function, a missing name in the request body should result in a 400 Bad Request status, as it indicates an error in the client's request. 404 Not Found is for when a resource is missing.
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(404).send({ message: 'No name was provided' }); |
There was a problem hiding this comment.
When the client fails to provide a required field like name, it's considered a client-side error. The appropriate HTTP status code for this is 400 Bad Request, not 404 Not Found. The 404 status code should be used when the requested resource or endpoint itself cannot be found.
| // res.send(message); | ||
| // }; | ||
|
|
||
| const getAll = async (res, req) => { |
There was a problem hiding this comment.
The parameters for an Express route handler should be (req, res). You have them in the wrong order, which will cause req.params to be undefined on the next line.
|
|
||
| const messages = await Message.findAll({ | ||
| where: { roomId }, | ||
| order: [['createdAt', 'ASC']], |
There was a problem hiding this comment.
The Message model doesn't have a createdAt field. Based on your model definition (src/models/message.model.js), you should order by the time field instead.
| @@ -0,0 +1,23 @@ | |||
| const { Message } = require('./models/message.model.js'); | |||
| const { Room } = require('./models/room.model.js'); | |||
| const { User } = require('./models/user.nodel.js'); | |||
There was a problem hiding this comment.
There appears to be a typo in the import path. The file for the user model is likely named user.model.js, but it's written as user.nodel.js here. This will cause a MODULE_NOT_FOUND error when the application starts.
src/services/message.service.js
Outdated
| }; | ||
|
|
||
| const update = async (id, authorId, text, time, roomId) => { | ||
| const room = await getOne(id); |
There was a problem hiding this comment.
The getOne(id) function retrieves a Message instance, not a Room. To avoid confusion and potential errors, this variable should be renamed to something like message.
| const update = async (id, name) => { | ||
| const room = await getOne(id); | ||
|
|
||
| return room.update({ name }); |
There was a problem hiding this comment.
If getOne(id) returns null (meaning no room was found), this line will throw a TypeError because you're trying to call .update() on null. This will cause an unhandled promise rejection and crash your application. You should add a check to ensure room exists before attempting to update it.
| @@ -0,0 +1,30 @@ | |||
| const { User } = require('../models/user.nodel.js'); | |||
There was a problem hiding this comment.
There appears to be a typo in the filename being imported. For consistency with your other model files (message.model.js, room.model.js), it should likely be user.model.js instead of user.nodel.js. You'll need to rename the file itself for this change to work.
| const update = async (id, name) => { | ||
| const user = await getOne(id); | ||
|
|
||
| return user.update({ name }); |
There was a problem hiding this comment.
If a user with the given id is not found, getOne(id) will return null. Attempting to call .update() on null will cause a TypeError and crash the application. You should add a check to handle the case where the user does not exist before trying to update it.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
You've made excellent progress refactoring the application to use a proper database structure with services and controllers! However, several critical issues, some of which were noted in the last review, are preventing the application from running and meeting the core requirements. I am requesting changes to address them.
Here are the main blockers:
- Server Won't Start: There is still a typo in the import path in
src/services/user.service.jsandsrc/setup.js. It should beuser.model.js, notuser.nodel.js. - Server Crashes on Update: The
updatemethods inuser.service.js,room.service.js, andmessage.service.jsdo not check if an item exists before trying to update it. This will cause the server to crash if an invalid ID is used. - Missing User Functionality: The user controller endpoints are not accessible because you haven't created and connected a user router in
src/index.js. This means the requirement for a user to send their username is not met. - Broken Route: The route to get all rooms (
GET /inroom.route.js) callsroomController.getAll, but this function is not defined in the controller, which will cause an error.
Please fix these issues to get the application working as expected. You're very close!
✨ 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
| const messages = await Message.findAll({ | ||
| where: { roomId }, | ||
| }); |
There was a problem hiding this comment.
To maintain a clear separation of concerns, the controller should not interact directly with the model. All database operations should be encapsulated within the service layer. You should add a method to messageService to find all messages by roomId and call that method here.
| if (!messages) { | ||
| return res.status(404).send({ message: 'No messages found' }); | ||
| } |
There was a problem hiding this comment.
The findAll method returns an array. In JavaScript, an empty array [] is a truthy value, so this condition will never be met. If no messages are found, findAll returns an empty array, and your API will correctly send back [] with a 200 status code, which is the expected behavior. You can safely remove this check.
| const time = new Date(); | ||
|
|
||
| if (!authorId || !text || !roomId) { | ||
| return res.status(404).send({ message: 'Something went wrong' }); |
There was a problem hiding this comment.
While this works, a 400 Bad Request status code is more semantically correct for indicating that the client provided invalid or incomplete data in the request. 404 Not Found is typically used when the requested resource itself doesn't exist.
src/controllers/room.controller.js
Outdated
| return res.status(404).send({ message: 'No name was provided' }); | ||
| } | ||
|
|
||
| const room = await roomService.update(id, name); |
There was a problem hiding this comment.
This will cause the application to crash if a room with the given id doesn't exist. The roomService.update method doesn't handle this case. You should first check if the room exists, similar to how you do in the deleteRoom function, before attempting to update it.
| const { userService } = require('../services/user.service.js'); | ||
|
|
||
| const getById = async (req, res) => { | ||
| const { id } = req.body; |
There was a problem hiding this comment.
It's a common convention in REST APIs to pass resource identifiers like id through URL parameters (req.params) for GET requests, rather than in the request body. For example, the route would look like GET /users/:id.
| const update = async (id, name) => { | ||
| const room = await getOne(id); | ||
|
|
||
| return room.update({ name }); |
There was a problem hiding this comment.
If getOne(id) returns null because the room doesn't exist, this line will throw a TypeError by trying to call .update() on null. This will crash the server. You need to add a check here to ensure room exists before you try to update it. If it doesn't exist, you could return null to signal to the controller that the update failed.
| @@ -0,0 +1,30 @@ | |||
| const { User } = require('../models/user.nodel.js'); | |||
There was a problem hiding this comment.
This import path contains a typo. The filename is user.model.js, not user.nodel.js. This was mentioned in the previous review and is a critical error that will prevent the application from starting.
| const user = await getOne(id); | ||
|
|
||
| return user.update({ name }); |
There was a problem hiding this comment.
This implementation does not handle the case where a user with the given id is not found. If getOne(id) returns null, calling user.update() on the next line will throw an error and crash the server. You need to add a check to ensure user is not null before trying to update it. This was also mentioned in the previous review.
| router.patch('/:roomId', roomController.renameRoom); | ||
| router.post('/', roomController.createRoom); | ||
| router.delete('/:roomId', roomController.deleteRoom); | ||
| router.get('/', roomController.getAll); |
There was a problem hiding this comment.
This route handler roomController.getAll does not exist in src/controllers/room.controller.js. Calling this route will result in a server error. You need to implement the getAll function in your room controller, which should probably call roomService.getAll() to fetch all the rooms.
| @@ -0,0 +1,23 @@ | |||
| const { Message } = require('./models/message.model.js'); | |||
| const { Room } = require('./models/room.model.js'); | |||
| const { User } = require('./models/user.nodel.js'); | |||
There was a problem hiding this comment.
There is a typo in this file path. The file should be user.model.js, not user.nodel.js. This is a critical error that will prevent the application from running and was mentioned in the previous review.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am approving this submission as an educational override to help you progress, but there are a few critical issues to address. You've done excellent work on the database models and WebSocket setup! However, the server won't start due to a typo: please correct user.nodel.js to user.model.js in src/services/user.service.js and src/setup.js. After that, the main priorities are to connect the user routes in src/index.js to enable user creation, and to implement the missing getAll function in src/controllers/room.controller.js to fix the rooms list. Also, remember to add checks in your service files to prevent crashes when updating non-existent items. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| return res.status(404).send({ message: 'No messages found' }); | ||
| } | ||
|
|
||
| messageEmitter.once('message', () => res.send(messages)); |
There was a problem hiding this comment.
This implementation of long-polling using messageEmitter will cause the GET request to hang until a new message is created via a POST request. This prevents a new user from seeing previous messages immediately, which is a core requirement. This endpoint should send the messages array back in the response right away.
| const { userService } = require('../services/user.service.js'); | ||
|
|
||
| const getById = async (req, res) => { | ||
| const { id } = req.body; |
There was a problem hiding this comment.
It's a common practice in REST APIs to get resource identifiers from the URL parameters (req.params) for GET requests, rather than the request body. For example, by defining a route like GET /users/:id.
| }; | ||
|
|
||
| const deleteUser = async (req, res) => { | ||
| const { id } = req.body; |
There was a problem hiding this comment.
Similar to getting a user, deleting one is typically done via a DELETE request to a URL that includes the ID, like DELETE /users/:id. The ID would then be available in req.params.id.
| return res.status(404).send({ message: 'No name was provided' }); | ||
| } | ||
|
|
||
| const room = await roomService.update(roomId, name); |
There was a problem hiding this comment.
The renameRoom function should verify that the room exists before attempting to update it. Currently, if an invalid roomId is passed, this call to roomService.update could crash the server. You can add a check similar to the one in your deleteRoom function to handle cases where the room is not found.
| const roomController = { | ||
| getById, | ||
| renameRoom, | ||
| createRoom, | ||
| deleteRoom, | ||
| }; |
There was a problem hiding this comment.
This controller is missing the getAll function, but it's being used in src/routes/room.route.js for the GET / route. This will cause the application to throw an error when that endpoint is hit. Please implement an getAll function to fetch all rooms and add it to this exported object.
| return message.update({ | ||
| authorId, | ||
| text, | ||
| time, | ||
| roomId, | ||
| }); |
There was a problem hiding this comment.
This line will cause the application to crash if message is null (i.e., if no message with the given id is found). You need to add a check to ensure message exists before attempting to call the .update() method on it.
| @@ -0,0 +1,39 @@ | |||
| const { User } = require('../models/user.nodel.js'); | |||
There was a problem hiding this comment.
There's a typo in the file path here. It should be user.model.js, not user.nodel.js. This was mentioned in the previous review and is a critical error that will prevent the server from starting.
| const user = await getOne(id); | ||
|
|
||
| return user.update({ name }); |
There was a problem hiding this comment.
If getOne(id) returns null because the user doesn't exist, the next line user.update({ name }) will throw an error and crash the server. You need to add a check here to handle cases where the user is not found.
| const user = await getOne(id); | ||
| const arr = user.rooms; | ||
| const newArr = [...arr, roomId]; | ||
|
|
||
| return user.update({ rooms: newArr }); |
There was a problem hiding this comment.
Similar to the update function, this will crash if the user with the given id is not found. You should add a check after fetching the user to ensure it exists before trying to access its properties.
| @@ -0,0 +1,23 @@ | |||
| const { Message } = require('./models/message.model.js'); | |||
| const { Room } = require('./models/room.model.js'); | |||
| const { User } = require('./models/user.nodel.js'); | |||
There was a problem hiding this comment.
This file path contains a typo. It should be ./models/user.model.js instead of user.nodel.js. This was a critical issue from the previous review, and it will cause the server to fail on startup.
No description provided.