Skip to content

add task solution#100

Open
kovaden414 wants to merge 4 commits into
mate-academy:masterfrom
kovaden414:develop
Open

add task solution#100
kovaden414 wants to merge 4 commits into
mate-academy:masterfrom
kovaden414:develop

Conversation

@kovaden414
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

Thank you for your hard work! 🚧 There are a few critical issues that need to be addressed before your code can be approved. Most importantly, the use of localStorage for authentication and user context is not compatible with Node.js server environments—user information should be managed using server-side mechanisms like sessions or tokens, and accessed from the request object. Additionally, async controller functions must properly handle errors using try-catch blocks and pass errors to Express's error middleware with next(error). Please refactor your authentication, service, and controller logic to resolve these issues. Once these are fixed, your project will be much more robust and ready to go! If you need guidance, check out the Express documentation on error handling and authentication best practices. Keep going—you’re almost there! 💪


✨ 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/message.controller.js Outdated
Comment on lines +17 to +19
if (!text) {
throw ApiError.badRequest('Enter the message');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Throwing an error directly in an async Express controller without a try-catch block or passing the error to next() will not be handled by Express error middleware and may cause unhandled promise rejections. Instead, wrap your logic in a try-catch block and call next(error) in the catch block to ensure proper error handling.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +16 to +18
if (!room) {
throw ApiError.notFound();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Throwing errors directly in async controller functions without a try-catch block or passing the error to next() will not be handled by Express error middleware. Please wrap your logic in try-catch and use next(error) for proper error handling.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +26 to +28
if (!title) {
throw ApiError.badRequest('Enter the title');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Throwing errors directly in async controller functions without a try-catch block or passing the error to next() will not be handled by Express error middleware. Please wrap your logic in try-catch and use next(error) for proper error handling.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +41 to +43
if (!room) {
throw ApiError.notFound();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Throwing errors directly in async controller functions without a try-catch block or passing the error to next() will not be handled by Express error middleware. Please wrap your logic in try-catch and use next(error) for proper error handling.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +45 to +47
if (!Array.isArray(participants)) {
throw ApiError.badRequest('Participants shoude be an array');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Throwing errors directly in async controller functions without a try-catch block or passing the error to next() will not be handled by Express error middleware. Please wrap your logic in try-catch and use next(error) for proper error handling.

Comment thread src/middlewares/authMiddleware.js Outdated
import { localStorage } from '../utils/store.js';

export const authMiddleware = (req, res, next) => {
const user = JSON.parse(localStorage.getItem('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.

Critical issue: localStorage is not available in Node.js server environments. This approach will not work for authentication on the server side. You need to implement authentication using a method suitable for Node.js, such as checking a token in the request headers or using server-side sessions.

Comment thread src/services/message.service.js Outdated
import { localStorage } from '../utils/store.js';

async function createMessage(text, roomId) {
const user = JSON.parse(localStorage.getItem('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.

Critical issue: localStorage is not available in Node.js server environments. This approach will not work for retrieving user data on the server side. You need to obtain user information from the request context, such as from authentication middleware that attaches the user to the request object.

Comment thread src/services/room.service.js Outdated
throw ApiError.badRequest('Room already exist');
}

const user = JSON.parse(localStorage.getItem('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.

Critical issue: localStorage is not available in Node.js server environments. You should obtain user information from the request context, such as from authentication middleware that attaches the user to the request object.

Comment thread src/services/room.service.js Outdated
}

async function updateRoom(room, title, participants) {
const user = JSON.parse(localStorage.getItem('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.

Critical issue: localStorage is not available in Node.js server environments. You should obtain user information from the request context, such as from authentication middleware that attaches the user to the request object.

Comment thread src/services/user.service.js Outdated

const user = await User.create({ username });

localStorage.setItem('user', JSON.stringify(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.

Critical issue: localStorage is not available in Node.js server environments. You should use a server-side session, token, or database-based mechanism to manage user authentication or session data.

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

Thank you for your hard work on this backend chat application! 🚀 However, there are several critical issues that need to be addressed before your code can be approved:

  • Authentication & User Context: The use of localStorage for storing and retrieving user information is not appropriate in a Node.js backend, as localStorage is a browser-only API. This approach breaks server-side authentication and user context management. Please switch to a proper server-side mechanism, such as sessions, JWT tokens, or extracting user info from the request object after authentication middleware.
  • Error Handling in Controllers: Many controller functions throw errors using ApiError but do not wrap their logic in try-catch blocks or consistently use async error-handling middleware. This can result in unhandled promise rejections and improper error responses to the client. Please ensure all controller logic is properly wrapped so errors are caught and passed to your error-handling middleware.
  • Sequelize Model Options: In your Message model, the options object incorrectly uses createdAt: false and updatedAt: false. The correct way to disable timestamps in Sequelize is to use timestamps: false.
  • Route Base Paths: In your main server file, app.use(messageRouter); does not specify a base path, which could lead to ambiguous or conflicting routes. Please use a base path like app.use('/messages', messageRouter); for clarity and maintainability.

Once you address these critical issues, your application will be much more robust and ready for production! Keep up the great work, and feel free to ask if you need guidance on implementing server-side authentication or error handling. 👍


✨ 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/message.controller.js Outdated
Comment on lines +13 to +24
const createMessage = async (req, res) => {
const { text } = req.body;
const { roomId } = req.params;

if (!text) {
throw ApiError.badRequest('Enter the message');
}

await messageService.createMessage(text, roomId);

res.status(201).send(text);
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The createMessage function throws an error using ApiError.badRequest, but there is no try-catch block to handle this error. This could result in unhandled promise rejections and the client not receiving a proper error response. Please ensure errors are caught and passed to your error-handling middleware as required by your framework or project checklist.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +11 to +21
const getRoomById = async (req, res) => {
const { roomId } = req.params;

const room = await roomService.getRoomById(roomId);

if (!room) {
throw ApiError.notFound();
}

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

Choose a reason for hiding this comment

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

Issue: The getRoomById function throws an error using ApiError.notFound(), but there is no try-catch block to handle this error. This could result in unhandled promise rejections and the client not receiving a proper error response. Please ensure errors are caught and passed to your error-handling middleware as required by your framework or project checklist.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +23 to +33
const createRoom = async (req, res) => {
const { title, participants } = req.body;

if (!title) {
throw ApiError.badRequest('Enter the title');
}

await roomService.createRoom(title, participants)

res.status(201).send({ message: 'Room created'});
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The createRoom function throws an error using ApiError.badRequest, but there is no try-catch block to handle this error. This could result in unhandled promise rejections and the client not receiving a proper error response. Please ensure errors are caught and passed to your error-handling middleware as required by your framework or project checklist.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +35 to +54
const updateRoom = async (req, res) => {
const { roomId } = req.params;
const { title, participants } = req.body;

const room = await roomService.getRoomById(roomId);

if (!room) {
throw ApiError.notFound();
}

if (!Array.isArray(participants)) {
throw ApiError.badRequest('Participants shoude be an array');
}

await roomService.updateRoom(room, title, participants)

const updatedRoom = await roomService.getRoomById(roomId);

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

Choose a reason for hiding this comment

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

Issue: The updateRoom function throws errors using ApiError.notFound() and ApiError.badRequest, but there is no try-catch block to handle these errors. This could result in unhandled promise rejections and the client not receiving a proper error response. Please ensure errors are caught and passed to your error-handling middleware as required by your framework or project checklist.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +56 to +68
const deleteRoom = async (req, res) => {
const { roomId } = req.params;

const room = await roomService.getRoomById(roomId);

if (!room) {
throw ApiError.notFound();
}

await roomService.deleteRoom(roomId);

res.status(204).send({ message: 'Room deleted' });
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The deleteRoom function throws an error using ApiError.notFound(), but there is no try-catch block to handle this error. This could result in unhandled promise rejections and the client not receiving a proper error response. Please ensure errors are caught and passed to your error-handling middleware as required by your framework or project checklist.

Comment thread src/models/message.js Outdated
Comment on lines +24 to +25
createdAt: false,
updatedAt: false,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The options createdAt: false and updatedAt: false are not valid in Sequelize model definitions. To disable both timestamps, use timestamps: false instead. This will prevent Sequelize from automatically adding createdAt and updatedAt fields.

Comment thread src/services/message.service.js Outdated
import { localStorage } from '../utils/store.js';

async function createMessage(text, roomId) {
const user = JSON.parse(localStorage.getItem('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.

Issue: Using localStorage to retrieve the user is not appropriate in server-side code. localStorage is a browser API and should not be used for authentication or user context in a Node.js backend. Consider passing the user context from the request object, typically set by your authentication middleware.

Comment thread src/services/room.service.js Outdated
throw ApiError.badRequest('Room already exist');
}

const user = JSON.parse(localStorage.getItem('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.

Issue: Using localStorage to retrieve the user is not appropriate in server-side code. localStorage is a browser API and should not be used for authentication or user context in a Node.js backend. Consider passing the user context from the request object, typically set by your authentication middleware.

Comment thread src/services/room.service.js Outdated
}

async function updateRoom(room, title, participants) {
const user = JSON.parse(localStorage.getItem('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.

Issue: Using localStorage to retrieve the user is not appropriate in server-side code. localStorage is a browser API and should not be used for authentication or user context in a Node.js backend. Consider passing the user context from the request object, typically set by your authentication middleware.

Comment thread src/services/user.service.js Outdated

const user = await User.create({ username });

localStorage.setItem('user', JSON.stringify(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.

Issue: Using localStorage to store the user is not appropriate in server-side code. localStorage is a browser API and should not be used for authentication or user context in a Node.js backend. Consider using server-side sessions, tokens, or another appropriate mechanism for managing user state.

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

Thank you for your hard work! 🚧 There are two critical issues that need to be addressed before your Pull Request can be approved:

  1. Routing Base Path: In your src/index.js, the message router is mounted without a base path (app.use(messageRouter);). This can lead to ambiguous or conflicting routes and does not meet the requirement for clear and maintainable route organization. Please update this to use a base path, such as app.use('/messages', messageRouter);.

  2. Authentication Middleware: The decoded user data from the JWT is not attached to the request object. If downstream controllers or services need access to the authenticated user's information, you should set req.user = userData; before calling next(); in your authentication middleware. This ensures that user context is available throughout your application.

Once you address these issues, your code will fully meet the task requirements. Keep up the great work—you're very close to completion! If you need more information, you can check the Express routing documentation and Express middleware best practices.


✨ 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/index.js Outdated

app.use('/user', userRouter);
app.use('/rooms', roomsRouter);
app.use(messageRouter);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: app.use(messageRouter); does not specify a base path. This can cause ambiguous or conflicting routes. Please use a base path, such as app.use('/messages', messageRouter);, to ensure route clarity and maintainability.

Comment on lines +13 to +21
const userData = jwtService.verify(token);

if (!userData) {
res.sendStatus(401);

return;
}

next();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential issue: The decoded user data from the JWT (userData) is not attached to the request object. If downstream handlers or services need access to the authenticated user's information, consider setting req.user = userData; before calling next();.

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

Thank you for your hard work on this project! Before your Pull Request can be approved, there are several critical issues that need to be addressed to meet the core requirements and ensure your backend chat application functions securely and reliably:

  1. Error Handling in Controllers:

    • The getAllMessages function in your message controller and the getAllRooms function in your room controller are missing try-catch blocks. Without these, any errors (such as database failures) will not be properly caught and passed to your error-handling middleware, which can lead to unhandled promise rejections and server crashes. Please wrap these functions in try-catch blocks and use next(error) in the catch block to forward errors.
  2. Authentication Middleware:

    • After verifying the access token, the decoded user data (userData) is not attached to the req object (e.g., req.user = userData). This is important because downstream controllers and services often rely on req.user to access authenticated user information. Please update your authentication middleware to attach the decoded user data to req.user.
  3. User Identification in Services:

    • The getUser function currently extracts user information from the refreshToken in cookies, which is not secure or standard for protected endpoints. Instead, user data should be extracted from the access token and accessed via req.user (populated by your authentication middleware). Please update this function accordingly.
  4. Route Registration:

    • The message router is registered without a base path (app.use(messageRouter);). This can cause route conflicts and ambiguity. For clarity and maintainability, please register it with a base path, such as app.use('/messages', messageRouter);.

Once these critical issues are resolved, your code will meet all core requirements for the task. Keep up the great work—you're very close! If you need more information, check out the Express error handling documentation and Express middleware best practices. 🚀


✨ 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/message.controller.js Outdated
Comment on lines +6 to +12
const getAllMessages = async (req, res) => {
const { roomId } = req.params;

const allMessages = await Message.findAll({ where: { roomId } });

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

Choose a reason for hiding this comment

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

Issue: The getAllMessages function is not wrapped in a try-catch block. If an error occurs (e.g., database failure), it will not be caught and passed to your error-handling middleware. Please wrap the function body in a try-catch block and call next(error) in the catch block, similar to your createMessage function. This is required for robust error handling.

Comment thread src/controllers/room.controller.js Outdated
Comment on lines +6 to +10
const getAllRooms = async (req, res) => {
const rooms = await Room.findAll();

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

Choose a reason for hiding this comment

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

Issue: The getAllRooms function is not wrapped in a try-catch block. If an error occurs (e.g., database failure), it will not be caught and passed to your error-handling middleware. Please wrap the function body in a try-catch block and call next(error) in the catch block, similar to your other controller functions.

Comment thread src/index.js

app.use('/user', userRouter);
app.use('/rooms', roomsRouter);
app.use('/messages', messageRouter);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The message router is used without a base path (app.use(messageRouter);). This can cause route conflicts and ambiguity. Please specify a base path, e.g., app.use('/messages', messageRouter);.

return;
}

const userData = jwtService.verify(token);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential issue: After verifying the token, the decoded user data (userData) is not attached to the req object (e.g., req.user = userData). If your controllers or services expect to access the authenticated user's information from the request, you should attach it here. Please check if this is required by your application logic.

Comment on lines +23 to +34
async function getUser(req) {
const { refreshToken } = req.cookies;
const userData = await jwtService.verifyRefresh(refreshToken);

if (!userData || !refreshToken) {
throw ApiError.unauthorized();
}

const user = await findByUserName(userData.username);

return 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.

Critical issue: The getUser function extracts user information from the refreshToken in cookies and verifies it with the refresh token secret. This is not the correct approach for identifying the user in protected endpoints. Instead, user information should be extracted from the access token (typically from the Authorization header) and attached to the request object by the authentication middleware. Please update this function to retrieve user data from req.user (which should be set by your auth middleware after verifying the access token).

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! 🎉 Your code now fully meets all the core requirements and constraints for the chat application. All routers are clearly mounted, authentication and error handling middleware are correctly implemented, and your models, controllers, and services are well-structured and functional. There are no missing or unnecessary files, and all previous critical issues have been resolved. This decision is made to help you move forward with your learning—please review the comments for detailed feedback and suggestions for further improvement. If any issues were highlighted, especially critical ones, make sure to address them in future work to avoid similar problems. Keep up the good work and continue refining your code—you're on the right track! 🚀


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

Footnotes

  1. Rate AI review example

Copy link
Copy Markdown

@maxim2310 maxim2310 left a comment

Choose a reason for hiding this comment

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

Well done!

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.

3 participants