Skip to content

Modules separated #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 21, 2024
Merged

Modules separated #7

merged 5 commits into from
Aug 21, 2024

Conversation

muneebhashone
Copy link
Owner

@muneebhashone muneebhashone commented Aug 21, 2024

Summary by Sourcery

Separate the user and authentication logic into distinct modules, enhancing code organization and maintainability. Introduce new user management features and Google login support. Update the build and deployment configurations to accommodate these changes, including adding a PostgreSQL service. Remove obsolete code and templates to streamline the project.

New Features:

  • Introduce a new user module with services, controllers, schemas, and routes for user management, including user creation, deletion, and retrieval.
  • Add support for Google login by implementing functions to handle Google OAuth tokens and user information retrieval.

Enhancements:

  • Refactor the authentication module by removing unused functions and schemas related to phone-based login and OTP verification.
  • Improve password validation by adding a strong password validation schema using the validator library.
  • Separate modules for better organization, moving user-related logic to a dedicated user module.

Build:

  • Update the pnpm-lock.yaml file to include new dependencies and update existing ones, particularly related to Prisma.

Deployment:

  • Modify the docker-compose.yml to include a new PostgreSQL service and update the Redis service configuration.

Chores:

  • Remove unused templates and mock routes to clean up the codebase.

Copy link

sourcery-ai bot commented Aug 21, 2024

Reviewer's Guide by Sourcery

This pull request implements a significant restructuring of the project, primarily focusing on modularizing the codebase, updating authentication and user management, and adjusting the database setup. Key changes include:

  1. Restructuring the project layout by moving auth and user-related files into a 'modules' directory.
  2. Simplifying the authentication process by removing OTP and phone-based login methods.
  3. Updating user management functionality, including changes to user creation, retrieval, and deletion.
  4. Introducing Prisma ORM with PostgreSQL, replacing the previous MongoDB setup.
  5. Adjusting Docker configuration to include PostgreSQL.
  6. Removing unused email templates and updating email-related functionality.
  7. Updating dependencies, including Prisma client version.

These changes aim to improve the project's structure, simplify authentication, and modernize the database approach.

File-Level Changes

Files Changes
src/modules/auth/auth.controller.ts
src/modules/auth/auth.router.ts
src/modules/auth/auth.service.ts
src/modules/user/user.controller.ts
src/modules/user/user.router.ts
src/modules/user/user.services.ts
Restructured project layout by moving auth and user-related files into a 'modules' directory
src/modules/auth/auth.controller.ts
src/modules/auth/auth.schema.ts
src/email/email.service.ts
Simplified authentication process by removing OTP and phone-based login methods
src/modules/user/user.controller.ts
src/modules/user/user.services.ts
src/modules/user/user.schema.ts
Updated user management functionality, including changes to user creation, retrieval, and deletion
prisma/migrations/20240815141534_init/migration.sql
prisma/migrations/20240815142412_/migration.sql
prisma/migrations/migration_lock.toml
pnpm-lock.yaml
Introduced Prisma ORM with PostgreSQL, replacing the previous MongoDB setup
docker-compose.yml Adjusted Docker configuration to include PostgreSQL
src/email/email.service.ts
templates/otp.ejs
templates/set-password.ejs
Removed unused email templates and updated email-related functionality
pnpm-lock.yaml Updated dependencies, including Prisma client version
src/utils/isUsername.ts
src/common/common.schema.ts
Implemented new utility functions and schemas
src/mock/seed.route.ts
src/user/user.seeder.ts
Removed mock data and seeding functionality
src/routes/routes.ts Updated routing configuration

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @muneebhashone - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines +110 to +111
if (!isUserExist) {
throw new Error('User already exists');
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Potential bug in user existence check

The logic here seems inverted. It's throwing an error when the user doesn't exist, which is the opposite of what we'd expect for a 'user already exists' check. Please review and correct this condition.

@@ -1,3 +1,4 @@
import validator from 'validator';
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider streamlining the new code to reduce complexity.

The new code introduces some complexity that could be streamlined. Here are a few suggestions:

  1. Dependencies: The addition of the validator library increases the dependency footprint. Ensure it's necessary for the project scope.

  2. ID Schemas: Consider unifying mongoIdSchema and idSchema to avoid confusion and redundancy. A single schema that handles both MongoDB and numeric IDs could simplify usage.

  3. Password Validation: The passwordValidationSchema function embeds specific logic directly. Abstracting this into a utility function could improve clarity and reusability.

  4. Readability: Inline use of refine and transform methods can reduce readability. Simplifying these could make the code more accessible to others.

Overall, these changes could enhance maintainability and clarity.

Comment on lines +97 to +99
const user = await createUser({ ...rest, role: 'DEFAULT_USER' }, false);

return user;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const user = await createUser({ ...rest, role: 'DEFAULT_USER' }, false);
return user;
return await createUser({ ...rest, role: 'DEFAULT_USER' }, false);


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

Comment on lines +119 to +121
const token = await signToken(jwtPayload);

return token;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const token = await signToken(jwtPayload);
return token;
return await signToken(jwtPayload);


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

Comment on lines +147 to +164
const newUser = await createUser({
email,
username: name,
avatar: picture,
role: ROLE_ENUM.DEFAULT_USER,
password: generateRandomNumbers(4),
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});

return newUser;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const newUser = await createUser({
email,
username: name,
avatar: picture,
role: ROLE_ENUM.DEFAULT_USER,
password: generateRandomNumbers(4),
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});
return newUser;
return await createUser({
email,
username: name,
avatar: picture,
role: ROLE_ENUM.DEFAULT_USER,
password: generateRandomNumbers(4),
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

Comment on lines +167 to +179
const updatedUser = await updateUser(user._id, {
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});

return updatedUser;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const updatedUser = await updateUser(user._id, {
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});
return updatedUser;
return await updateUser(user._id, {
socialAccount: [
{
refreshToken: refresh_token,
tokenExpiry: new Date(Date.now() + expires_in * 1000),
accountType: SOCIAL_ACCOUNT_ENUM.GOOGLE,
accessToken: access_token,
accountID: id,
},
],
});


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

},
);

if (!user) throw new Error('User not found');
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (!user) throw new Error('User not found');
if (!user) {


ExplanationIt is recommended to always use braces and create explicit statement blocks.

Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

@muneebhashone muneebhashone merged commit ac65a06 into main Aug 21, 2024
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.

1 participant