Skip to content
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

392 refactoring validations in team and invite routes with express validator #443

Conversation

DeboraSerra
Copy link
Contributor

@DeboraSerra DeboraSerra commented Dec 28, 2024

Describe your changes

Briefly describe the changes you made and their purpose.

Issue number

Mention the issue number(s) this PR addresses (e.g., #123).

Please ensure all items are checked off before requesting a review:

  • I deployed the code locally.
  • I have performed a self-review of my code.
  • I have included the issue # in the PR.
  • I have labelled the PR correctly.
  • The issue I am working on is assigned to me.
  • I didn't use any hardcoded values (otherwise it will not scale, and will make it difficult to maintain consistency across the application).
  • I made sure font sizes, color choices etc are all referenced from the theme.
  • My PR is granular and targeted to one specific feature.
  • I took a screenshot or a video and attached to this PR if there is a UI change.

Copy link
Contributor

coderabbitai bot commented Dec 28, 2024

Walkthrough

The pull request introduces several configuration and utility improvements to the backend project. A new Prettier configuration file and ESLint configuration have been added to standardize code formatting and linting. The package.json has been updated with new development dependencies. A comprehensive Postman collection for team management endpoints was created, and the team-related controllers, routes, and helper utilities were refactored to improve validation, error handling, and overall code structure.

Changes

File Change Summary
backend/.prettierrc Added new Prettier configuration with formatting rules
backend/eslint.config.mjs New ESLint configuration for JavaScript files
backend/package.json Added dev dependencies: @eslint/js, eslint, globals
backend/postman/Team.postman_collection.json New Postman collection with team management API requests
backend/src/controllers/team.controller.js Refined error handling and code formatting
backend/src/routes/team.routes.js Updated route definitions with new validation middleware
backend/src/utils/team.helper.js Added new validation functions for team-related routes
backend/.eslintrc New ESLint rule for unused variables
backend/src/test/e2e/team.test.mjs Standardized string delimiters to single quotes
backend/src/test/unit/controllers/team.test.js Removed specific test cases for setOrganisation method

Sequence Diagram

sequenceDiagram
    participant Client
    participant TeamRoutes
    participant Middleware
    participant TeamController
    participant Database

    Client->>TeamRoutes: Send team management request
    TeamRoutes->>Middleware: Apply authentication
    Middleware->>TeamRoutes: Validate access
    TeamRoutes->>Middleware: Apply request validation
    Middleware->>TeamController: Execute handler
    TeamController->>Database: Perform database operation
    Database->>TeamController: Return result
    TeamController->>Client: Send response
Loading

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c1f876 and 0f0bfb4.

📒 Files selected for processing (3)
  • backend/.eslintrc (1 hunks)
  • backend/src/test/e2e/team.test.mjs (1 hunks)
  • backend/src/test/unit/controllers/team.test.js (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • backend/.eslintrc
  • backend/src/test/e2e/team.test.mjs
🔇 Additional comments (1)
backend/src/test/unit/controllers/team.test.js (1)

47-47: Consistent spelling of “Organization”
Changing “Organisation” to “Organization” aligns the naming convention with broader usage across the codebase. This helps keep messaging consistent and reduces potential confusion for users.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 25077b9 and 3c1f876.

⛔ Files ignored due to path filters (1)
  • backend/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (7)
  • backend/.prettierrc (1 hunks)
  • backend/eslint.config.mjs (1 hunks)
  • backend/package.json (1 hunks)
  • backend/postman/Team.postman_collection.json (1 hunks)
  • backend/src/controllers/team.controller.js (3 hunks)
  • backend/src/routes/team.routes.js (1 hunks)
  • backend/src/utils/team.helper.js (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • backend/.prettierrc
🧰 Additional context used
🪛 Gitleaks (8.21.2)
backend/postman/Team.postman_collection.json

17-17: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)


78-78: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)


118-118: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)


158-158: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)


190-190: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)


219-219: Uncovered a JSON Web Token, which may lead to unauthorized access to web applications and sensitive user data.

(jwt)

🔇 Additional comments (34)
backend/eslint.config.mjs (2)

1-2: All good importing globals and pluginJs
The import statements for globals and @eslint/js are correctly placed, which helps us keep our linter configuration concise and organized.


5-10: Solid ESLint configuration setup
Specifying commonjs and using globals.node plus pluginJs.configs.recommended provides a coherent baseline for linting Node.js projects. This approach keeps your environment rules consistent and robust.

backend/package.json (1)

35-39: Sensibly adding linting and global dependencies
Adding these development dependencies (@eslint/js, eslint, and globals) fortifies your code quality measures, ensuring your codebase aligns with recognized standards.

backend/src/utils/team.helper.js (9)

1-2: Imports alignment
Your newly added imports look consistent with the code usage for URL and validation.


6-8: Ensure empty string usage
Permitting an empty string as a valid server URL might lead to accidental misconfigurations if a user does not provide an actual URL.

Also applies to: 14-14


28-31: Return objects
Returning consistent object structures is beneficial for downstream usage; your approach is good.


37-49: Validation array
This custom validator is well crafted. The use of optional fields and custom checks is well done.


51-62: Validate organization name
The mix of .notEmpty(), .isLength(), and regex ensures robust input validation. This is a strong approach.


64-79: Invitation validation
Your checks for both the invited email and role are well structured and thorough.


81-96: Change role validation
Ensuring that the role is one of {admin, member} is a great practice.


98-103: Param validation
Requiring a numeric 'memberId' helps maintain data integrity.


106-112: Export structure
All relevant validation helpers are exported correctly, making them straightforward to import in other modules.

backend/src/routes/team.routes.js (9)

1-2: Consistent import statements
Using single quotes consistently aligns with your new Prettier configuration.


17-22: New validation imports
Great job pulling in all relevant validators from team.helper.


28-31: Global JWT usage
Applying router.use(authenticateJWT) globally ensures consistent authentication.


33-39: Set organization route
Inserting validateOrganizationName and handleValidationErrors ensures robust input checks before handler execution.


40-40: Invite route
The new validationInvite usage is correct.


41-47: Update route
Applying validateOrganizationName again for the team name is consistent with your set-organisation approach.


48-54: Change-role route
Using the validationChangeRole array with handleValidationErrors is clear and maintainable.


55-61: Set server-url route
Attaching validateSetServerUrl is aligned with your helper logic.


63-70: Remove & get-all-invites
validateIdParam for the remove route is a wise addition, ensuring a valid numeric memberId.

backend/src/controllers/team.controller.js (13)

1-4: Refreshed imports
Imports for settings and services remain well structured, enhancing clarity.


20-25: Success message
Replacing references with "Organization created successfully" is consistent with the rest of the naming.


28-28: Consistent error handling
Logging the error and sending an internal server error response is good practice.


39-39: Use of internal server error
Maintains consistency with your error strategy.


47-51: Defaulting null to empty
Returning an empty string for null serverUrl can be beneficial for client behaviour.


59-60: Optional chaining
Great usage of optional chaining (?.) for safe property access.


63-71: Mapping user details
Providing a formatted date for each user is a nice usability feature.


74-74: Error response
Centralized error handling ensures consistent client messaging.


80-87: UpdateTeam
Your approach to update the team name is straightforward, returning a clear success message.


94-96: Set server URL
Aligns perfectly with the new validation in team.helper.


99-99: Remove member
Deleting a user from the team with the service method and returning a success message is easy to understand.

Also applies to: 106-108, 111-111


117-119: Change role
Properly aligning the user’s role with expected values is a big step in controlling permissions.

Also applies to: 122-122


124-134: Module exports
Exporting all controller functions ensures easy integration with your routes.

backend/postman/Team.postman_collection.json Show resolved Hide resolved
swoopertr
swoopertr previously approved these changes Dec 28, 2024
Copy link
Collaborator

@erenfn erenfn left a comment

Choose a reason for hiding this comment

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

The validations look good.

@erenfn erenfn merged commit 407e5db into develop Jan 2, 2025
2 checks passed
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.

Refactoring Validations in team and invite routes with express-validator
3 participants