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

feat(api): API-CLIENT: Create controller for User module #484

Merged
merged 1 commit into from
Oct 13, 2024

Conversation

anudeeps352
Copy link
Contributor

@anudeeps352 anudeeps352 commented Oct 12, 2024

User description

Description

API-CLIENT: Create controller for User module

Fixes #349

Dependencies

None

Future Improvements

Mention any improvements to be done in future related to any file/feature

Mentions

Mention and tag the people

Screenshots of relevant screens

Add screenshots of relevant screens

Developer's checklist

  • My PR follows the style guidelines of this project
  • I have performed a self-check on my work

If changes are made in the code:

  • I have followed the coding guidelines
  • My changes in code generate no new warnings
  • My changes are breaking another fix/feature of the project
  • I have added test cases to show that my feature works
  • I have added relevant screenshots in my PR
  • There are no UI/UX issues

Documentation Update

  • This PR requires an update to the documentation at docs.keyshade.xyz
  • I have made the necessary updates to the documentation, or no documentation changes are required.

PR Type

Enhancement, Tests


Description

  • Implemented a new UserController class to manage user-related operations such as retrieving, updating, and deleting user data.
  • Defined TypeScript interfaces for user-related requests and responses, including operations for email change OTP validation and resending.
  • Added comprehensive tests for the UserController methods to ensure correct functionality of user operations.

Changes walkthrough 📝

Relevant files
Enhancement
user.ts
Implement UserController for managing user operations       

packages/api-client/src/controllers/user.ts

  • Created UserController class with methods for user operations.
  • Implemented methods for getting, updating, and deleting user data.
  • Added methods for validating and resending email change OTP.
  • +66/-0   
    user.types.d.ts
    Define user-related request and response types                     

    packages/api-client/src/types/user.types.d.ts

  • Defined interfaces for user-related requests and responses.
  • Included types for user data retrieval and update operations.
  • Added types for email change OTP validation and resend operations.
  • +34/-0   
    Tests
    user.spec.ts
    Add tests for UserController methods                                         

    packages/api-client/tests/user.spec.ts

  • Added tests for UserController methods.
  • Tested user data retrieval, update, and deletion.
  • Included tests for email change OTP validation and resend.
  • +100/-0 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 Security concerns

    Sensitive information exposure:
    The validateEmailChangeOTP method in the UserController class (packages/api-client/src/controllers/user.ts) includes the OTP in the URL query parameter (line 48). This could potentially expose sensitive information in server logs or browser history. It's recommended to send the OTP in the request body instead.

    ⚡ Recommended focus areas for review

    Possible Bug
    The updateSelf and deleteSelf methods use relative paths ('./api/user') which may cause issues. These should be absolute paths ('/api/user').

    Code Smell
    The validateEmailChangeOTP method includes the OTP in the URL query parameter, which may expose sensitive information. Consider moving it to the request body.

    Incomplete Tests
    The test suite is missing tests for validateEmailChangeOTP and resendEmailChangeOTP methods. These should be implemented to ensure full coverage.

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Correct the test for deleting the current user

    The test for deleting the current user is incorrectly named and implemented. It's
    currently calling updateSelf instead of deleteSelf. Correct the test name and
    implementation to properly test the delete functionality.

    packages/api-client/tests/user.spec.ts [89-96]

    -it('should update current user', async () => {
    -  const deleteUser = await userController.updateSelf(
    -    { name: 'Jane Doe', email: '[email protected]' },
    +it('should delete current user', async () => {
    +  const deleteUser = await userController.deleteSelf(
         { 'x-e2e-user-email': email }
       )
     
       expect(deleteUser.success).toBe(true)
     })
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion addresses a critical bug in the test suite by correcting the test for deleting a user, ensuring that the intended functionality is accurately tested.

    9
    Best practice
    Implement error handling for API calls to improve robustness

    Consider adding error handling for API calls to manage potential network issues or
    unexpected server responses. You could wrap the API calls in try-catch blocks and
    provide more informative error messages.

    packages/api-client/src/controllers/user.ts [21-26]

     async getSelf(
       headers?: Record<string, string>
     ): Promise<ClientResponse<GetSelfResponse>> {
    -  const response = await this.apiClient.get(`/api/user`, headers)
    -  return await parseResponse<GetSelfResponse>(response)
    +  try {
    +    const response = await this.apiClient.get(`/api/user`, headers)
    +    return await parseResponse<GetSelfResponse>(response)
    +  } catch (error) {
    +    throw new Error(`Failed to get user data: ${error.message}`)
    +  }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding error handling for API calls is crucial for managing potential network issues or unexpected server responses, thereby improving the robustness and reliability of the application.

    8
    Enhancement
    Use template literals for constructing API endpoint URLs

    Consider using template literals for URL construction in API calls to improve
    readability and reduce the risk of typos. For example, replace ./api/user with
    ${this.backendURL}/api/user.

    packages/api-client/src/controllers/user.ts [32]

    -const response = await this.apiClient.put(`./api/user`, request, headers)
    +const response = await this.apiClient.put(`${this.backendURL}/api/user`, request, headers)
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using template literals for URL construction enhances readability and reduces the risk of typos, improving code maintainability and clarity.

    7
    Add tests for email change OTP validation and resend functionality

    Add tests for the validateEmailChangeOTP and resendEmailChangeOTP methods to ensure
    complete coverage of the UserController functionality.

    packages/api-client/tests/user.spec.ts [98-100]

    -// Validate email change OTP
    -// resend validate email OTP tests
    +it('should validate email change OTP', async () => {
    +  const response = await userController.validateEmailChangeOTP(
    +    { otp: '123456' },
    +    { 'x-e2e-user-email': email }
    +  )
    +  expect(response.success).toBe(true)
    +})
     
    +it('should resend email change OTP', async () => {
    +  const response = await userController.resendEmailChangeOTP(
    +    {},
    +    { 'x-e2e-user-email': email }
    +  )
    +  expect(response.success).toBe(true)
    +})
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Adding tests for the validateEmailChangeOTP and resendEmailChangeOTP methods increases test coverage and ensures that these functionalities are working as expected, enhancing the reliability of the codebase.

    6

    💡 Need additional feedback ? start a PR chat

    @anudeeps352 anudeeps352 changed the title Fixes #349 : API-CLIENT: Create controller for User module feat(api) Fixes #349 : API-CLIENT: Create controller for User module Oct 12, 2024
    @anudeeps352 anudeeps352 changed the title feat(api) Fixes #349 : API-CLIENT: Create controller for User module feat(api): API-CLIENT: Create controller for User module Oct 12, 2024
    @rajdip-b rajdip-b added the hacktoberfest-accepted Your PR has this = Congrats! label Oct 13, 2024
    @rajdip-b rajdip-b merged commit f9d8e83 into keyshade-xyz:develop Oct 13, 2024
    4 of 10 checks passed
    Kiranchaudhary537 pushed a commit to Kiranchaudhary537/keyshade that referenced this pull request Oct 13, 2024
    rajdip-b pushed a commit that referenced this pull request Oct 24, 2024
    ## [2.6.0](v2.5.0...v2.6.0) (2024-10-24)
    
    ### 🚀 Features
    
    * **api:**  Add icon and remove description field from workspace ([#435](#435)) ([a99c0db](a99c0db))
    * **api-client:** Added workspace-membership and related tests ([#452](#452)) ([6a1c091](6a1c091))
    * **api-client:** Create controller for User module ([#484](#484)) ([f9d8e83](f9d8e83))
    * **api:** Add prod env schema in env file ([#436](#436)) ([21c3004](21c3004))
    * **api:** Add resend otp implementation ([#445](#445)) ([4dc6aa1](4dc6aa1))
    * **api:** Fetch total count of environments, [secure]s and variables in project ([#434](#434)) ([0c9e50a](0c9e50a))
    * **api:** Replace `projectId` with `name` and `slug` in workspace-role response.  ([#432](#432)) ([af06071](af06071))
    * **cli:** Add functionality to operate on Secrets ([#504](#504)) ([1b4bf2f](1b4bf2f))
    * **cli:** Add project command ([#451](#451)) ([70448e1](70448e1))
    * **cli:** Add workspace operations ([#441](#441)) ([ed38d22](ed38d22))
    * **cli:** implement commands to get, list, update, and delete, workspace roles ([#469](#469)) ([957ea8d](957ea8d))
    * **cli:** Implemented pagination support ([#453](#453)) ([feb1806](feb1806))
    * **cli:** Secret scan ([#438](#438)) ([85cb8ab](85cb8ab))
    * **cli:** Update environment command outputs ([f4af874](f4af874))
    * **platform:** Clearing email field after waitlisting the user email ([#481](#481)) ([256d659](256d659))
    * Remove project IDs from workspace role export data and update tests ([#448](#448)) ([8fdb328](8fdb328))
    * **web:** Configured extra check for waitlisted users already in the list and created toast message for them ([#492](#492)) ([2ddd0ef](2ddd0ef))
    * **web:** show the toast only when email add successfully ([#490](#490)) ([783c411](783c411))
    
    ### 🐛 Bug Fixes
    
    * **api,api-client:** Add environmentSlug in multiple places across the variable module ([#468](#468)) ([d970aff](d970aff))
    * **api:** Replace the id with slug in the global-search service ([#455](#455)) ([74804b1](74804b1))
    * **platform:** Fixed duplicate Google Logo UI fix  ([#450](#450)) ([fb0d6f7](fb0d6f7))
    * resolve footer website name cut-off or overlap issue ([#444](#444)) ([fe03ba2](fe03ba2))
    * **web:** Horizontal Scrolling issue on the website ([#440](#440)) ([655177b](655177b))
    
    ### 📚 Documentation
    
    * Add documentation for environment in CLI ([#462](#462)) ([dad7394](dad7394))
    * Add documentation for project in CLI ([#466](#466)) ([341fb32](341fb32))
    * Add documentation for scan in CLI ([#461](#461)) ([72281e6](72281e6))
    * Add documentation for workspace command ([#464](#464)) ([4aad8a2](4aad8a2))
    * Add instructions for resetting the local Prisma database ([#495](#495)) ([#501](#501)) ([b07ea17](b07ea17))
    * Added docker support documentation ([#465](#465)) ([bc04be4](bc04be4))
    * Added documentation for running the platform ([#473](#473)) ([8b8386b](8b8386b))
    * Added missing mappings to pages ([5de9fd8](5de9fd8))
    * Fix Documentation Hyperlink and update expired Discord invite link ([#496](#496)) ([5a10e39](5a10e39))
    * Updated CLI docs ([#460](#460)) ([c7e0f13](c7e0f13))
    
    ### 🔧 Miscellaneous Chores
    
    * Add more logging to Sentry init ([#470](#470)) ([de4925d](de4925d))
    * **api:** Optimise API docker image size ([#360](#360)) ([ea40dc1](ea40dc1))
    * **api:** Updated lockfile ([a968e78](a968e78))
    * **CI:** Add [secure] scan validation ([f441262](f441262))
    * **cli:** Update controller invocation in environment commands ([#477](#477)) ([596bd1a](596bd1a))
    * Minor changes to variables ([fe01ca6](fe01ca6))
    * **[secure]-scan:** Failing lint issues ([#507](#507)) ([48f45df](48f45df))
    * **[secure]-scan:** Formatted files ([5884833](5884833))
    * Update .env.example ([70ad4f7](70ad4f7))
    * Updated scripts ([9eb76a7](9eb76a7))
    * **web:** email validation ([#487](#487)) ([e8e737a](e8e737a))
    @rajdip-b
    Copy link
    Member

    🎉 This PR is included in version 2.6.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    API-CLIENT: Create controller for User module
    2 participants