Skip to content

solution#245

Open
artemstadnik wants to merge 9 commits intomate-academy:masterfrom
artemstadnik:develop
Open

solution#245
artemstadnik wants to merge 9 commits intomate-academy:masterfrom
artemstadnik:develop

Conversation

@artemstadnik
Copy link

No description provided.

Copy link

@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 this project! I am approving your submission as you've successfully implemented all the required features. The code is clean, well-structured, and follows modern best practices for a Node.js application.

I have a couple of suggestions to make your code even more robust:

  • In src/controllers/auth.controller.ts, the empty catch block for token deletion can hide unexpected database errors. A safer approach is to use prisma.token.deleteMany({ where: { userId } }) in your repository, which doesn't throw an error if no token is found, removing the need for a try...catch.
  • In your migration.sql file, consider using ON DELETE CASCADE for the foreign key in the tokens table. This automatically cleans up a user's tokens if their account is ever deleted, which is great for database integrity.

These are not blockers but are good practices to keep in mind. Overall, this is a very strong submission. Well done!


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

Footnotes

  1. Rate AI review example

CREATE UNIQUE INDEX "tokens_user_id_key" ON "tokens"("user_id");

-- AddForeignKey
ALTER TABLE "tokens" ADD CONSTRAINT "tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

Choose a reason for hiding this comment

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

Using ON DELETE RESTRICT prevents a user from being deleted if they have an associated token. While there's no user deletion feature in the current requirements, it's generally better practice to use ON DELETE CASCADE. This would automatically delete the user's token if the user record is ever deleted, making the database schema more robust and easier to manage in the future.

Comment on lines +15 to +17
try {
await tokensRepository.deleteByUserId(user.id);
} catch {}

Choose a reason for hiding this comment

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

An empty catch block can hide unexpected database errors, not just the "token not found" error you're likely trying to handle when a user logs in for the first time. A more explicit and safer approach would be to use a different Prisma method. Consider using deleteMany({ where: { userId: user.id } }), which doesn't throw an error if no matching token is found, removing the need for a try...catch block altogether.

}

await usersRepository.activate(email);
await sendAuthentication(res, user);

Choose a reason for hiding this comment

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

The user object passed to sendAuthentication here is stale. It was fetched before the call to usersRepository.activate(), which modifies the user's state in the database. While it doesn't cause a bug with the current implementation, it's a good practice to use the most up-to-date data. The activate repository function returns the updated user, so you should capture it and pass it to sendAuthentication. For example:

const activatedUser = await usersRepository.activate(email);
await sendAuthentication(res, activatedUser);

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