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): Improve handling of edge cases for paginate module #402

Merged
merged 2 commits into from
Jul 29, 2024

Conversation

muntaxir4
Copy link
Contributor

@muntaxir4 muntaxir4 commented Jul 28, 2024

User description

Description

  • Improve paginate module to handle cases when page is greater or equal to maximum page limit and when limit is 0. Paginate method will return empty object on such cases.
  • Add unit tests for such cases.

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

  • Updated the paginate function to handle cases where the page exceeds the maximum page limit and when the limit is 0 or undefined, returning an empty object in such cases.
  • Added and modified unit tests to cover these edge cases.

Changes walkthrough 📝

Relevant files
Tests
paginate.spec.ts
Add and modify test cases for paginate function                   

apps/api/src/common/paginate.spec.ts

  • Added test case to return empty object when page exceeds maximum page
    limit.
  • Modified test case to return empty object when limit is 0 or
    undefined.
  • +15/-4   
    Enhancement
    paginate.ts
    Update paginate function for edge cases handling                 

    apps/api/src/common/paginate.ts

  • Updated paginate function to return empty object when limit is 0 or
    undefined.
  • Added condition to return empty object when page exceeds maximum page
    limit.
  • +3/-2     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Type Safety
    The function paginate returns {} as PaginatedMetadata when query.limit is not defined or when query.page exceeds metadata.pageCount. This might lead to type safety issues if the empty object does not fulfill all properties expected by PaginatedMetadata. Consider returning a fully initialized PaginatedMetadata object with default or empty values for all properties.

    Type Safety
    Similar to the previous issue, returning {} as PaginatedMetadata when query.page exceeds metadata.pageCount could lead to type safety issues. Ensure that the returned object meets all the interface requirements of PaginatedMetadata.

    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Jul 28, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Ensure query.limit is a positive number to prevent logical errors

    Add a check to ensure that query.limit is not only non-zero but also a positive
    number. This prevents potential issues with negative limits, which logically do not
    make sense and can lead to unexpected behavior.

    apps/api/src/common/paginate.ts [36]

    -if (!query.limit) return {} as PaginatedMetadata
    +if (!query.limit || query.limit <= 0) return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion addresses a potential logical error by ensuring that query.limit is not only non-zero but also positive, which prevents unexpected behavior from negative limits.

    9
    Best practice
    Return a well-defined empty PaginatedMetadata object instead of a plain empty object

    Instead of returning an empty object directly, it would be more robust to return a
    well-defined empty PaginatedMetadata object with default values for all properties.
    This ensures that the structure of the response remains consistent, which can
    prevent potential runtime errors in consuming functions that might expect specific
    properties to always exist.

    apps/api/src/common/paginate.ts [36]

    -if (!query.limit) return {} as PaginatedMetadata
    +if (!query.limit) return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion improves the robustness of the function by ensuring that the returned object has a consistent structure, which can prevent potential runtime errors in consuming functions.

    8
    Enhancement
    Add a test case for negative query.limit values

    Add a test case to verify that the function behaves correctly when query.limit is
    negative. This helps ensure that the function's robustness and error handling are
    adequate for unexpected input values.

    apps/api/src/common/paginate.spec.ts [95-102]

    -it('should return empty object when limit is 0 or undefined', () => {
    +it('should return empty object when limit is negative', () => {
       const totalCount = 10
       const relativeUrl = '/items'
    -  const query = { page: 0, limit: 0 }
    +  const query = { page: 0, limit: -1 }
       const result = paginate(totalCount, relativeUrl, query)
       expect(result).toBeDefined()
       expect(result).toEqual({})
     })
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding a test case for negative query.limit values enhances the robustness of the test suite by ensuring that the function handles unexpected input values correctly.

    7
    Maintainability
    Add descriptive logging for cases where query.page exceeds metadata.pageCount

    Consider adding a more descriptive error or log message before returning an empty
    object when query.page exceeds metadata.pageCount. This can aid in debugging and
    understanding the flow of data and decisions within the function.

    apps/api/src/common/paginate.ts [58]

    -if (query.page >= metadata.pageCount) return {} as PaginatedMetadata
    +if (query.page >= metadata.pageCount) {
    +  console.error(`Page ${query.page} exceeds maximum page count of ${metadata.pageCount}. Returning empty metadata.`);
    +  return { links: {}, pageCount: 0, totalCount: 0 } as PaginatedMetadata;
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: This suggestion improves maintainability by adding a descriptive log message, which aids in debugging and understanding the flow of data within the function.

    6

    @muntaxir4 muntaxir4 changed the title Return empty object for paginate at few cases feat(api): Improve handling of edge cases for paginate module Jul 28, 2024
    Copy link
    Member

    @rajdip-b rajdip-b left a comment

    Choose a reason for hiding this comment

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

    LGTM

    @rajdip-b rajdip-b merged commit 28e62f8 into keyshade-xyz:develop Jul 29, 2024
    4 checks passed
    rajdip-b pushed a commit that referenced this pull request Jul 29, 2024
    ## [2.3.0](v2.2.0...v2.3.0) (2024-07-29)
    
    ### 🚀 Features
    
    * **api:** Add pagination metadata to Environment module ([#382](#382)) ([9baa344](9baa344))
    * **api:** Add pagination metadata to Event module ([#394](#394)) ([60010b4](60010b4))
    * **api:** Add pagination metadata to Integration module ([#391](#391)) ([0372e36](0372e36))
    * **api:** Add pagination metadata to Project module ([#393](#393)) ([bc274fd](bc274fd))
    * **api:** Add pagination metadata to Secret module ([#389](#389)) ([c4cc667](c4cc667))
    * **api:** Add pagination metadata to Variable module ([#390](#390)) ([be6aabf](be6aabf))
    * **api:** Add pagination metadata to Workspace module  ([#387](#387)) ([a08c924](a08c924))
    * **api:** Add pagination metadata to Workspace Role module ([#388](#388)) ([d8e8f49](d8e8f49))
    * **api:** Create a paginate method ([#379](#379)) ([09576f1](09576f1))
    * **api:** Create endpoint for fetching all revisions of a [secure] ([#303](#303)) ([de2b602](de2b602))
    * **api:** Create endpoint for fetching all revisions of a variable ([#304](#304)) ([9abddc1](9abddc1))
    * **cli:** Improved the DX for list profile ([#334](#334)) ([6bff496](6bff496))
    * **platform:** Add warning sonner toast for invalid otp ([#335](#335)) ([21513f5](21513f5))
    
    ### 🐛 Bug Fixes
    
    * **cli:** Added parent directory check ([#359](#359)) ([538ea7f](538ea7f))
    * **platform:** Platform types fixes ([#374](#374)) ([8e9d9ff](8e9d9ff))
    
    ### 📚 Documentation
    
    * Added docker details in setting-things-up.md ([#358](#358)) ([ed5093a](ed5093a))
    * Update postman workspace link ([d6aba27](d6aba27))
    * Updated env and cli docs ([1213d2a](1213d2a))
    
    ### 🔧 Miscellaneous Chores
    
    * Added next backend url in .env.example ([5695254](5695254))
    * **api-client:** Added pagination structure ([a70e957](a70e957))
    * **api-client:** Fixed test script ([ad70819](ad70819))
    * **api-client:** Removed try-catch from tests in environment ([a64e48c](a64e48c))
    * **api:** Add user cache for optimization ([#386](#386)) ([8d730b5](8d730b5))
    * **api:** Alter cache rehydration interval ([f5f9eec](f5f9eec))
    * **api:** Fixed naming error in variable controller ([0c5a380](0c5a380))
    * **api:** Improve handling of edge cases for paginate module ([#402](#402)) ([8591487](8591487))
    * **api:** Minor updates to user service ([249d778](249d778))
    * **api:** Skip workspace creation when user is admin ([#376](#376)) ([13f6c59](13f6c59))
    * **ci:** Add docker check   ([#383](#383)) ([3119001](3119001))
    * **ci:** Add names to CI files ([1a7e5f6](1a7e5f6))
    * **ci:** Add validate CLI pipeline ([#373](#373)) ([a91df6c](a91df6c))
    * **ci:** Adding validate pipeline ([#372](#372)) ([23cf3b3](23cf3b3))
    * **ci:** Disabled platform and api deployments ([74d601a](74d601a))
    * **ci:** Fixed deployment scripts ([12e35db](12e35db))
    * **ci:** Fixed platform script ([d783f2a](d783f2a))
    * **CI:** Include migration deployment in API deploy pipeline ([dbd5222](dbd5222))
    * **CI:** Separated deployment and docker build jobs ([090e193](090e193))
    * **CI:** Setup inter-job dependency ([1756727](1756727))
    * **ci:** Update auto-assign.yaml ([#375](#375)) ([91e0ec1](91e0ec1))
    * **cli:** Changed objects to classes ([#306](#306)) ([c83f2db](c83f2db))
    * Removed Minio config ([8feb83a](8feb83a))
    * Updated deployment scripts and added health check in platform ([fcc1c3f](fcc1c3f))
    
    ### 🔨 Code Refactoring
    
    * **api:** Updated path of some endpoints in project controller ([9502678](9502678))
    * **api:** Updated Redis provider ([33491a1](33491a1))
    rajdip-b pushed a commit that referenced this pull request Jul 29, 2024
    ## [2.3.0](v2.2.0...v2.3.0) (2024-07-29)
    
    ### 🚀 Features
    
    * **api:** Add pagination metadata to Environment module ([#382](#382)) ([9baa344](9baa344))
    * **api:** Add pagination metadata to Event module ([#394](#394)) ([60010b4](60010b4))
    * **api:** Add pagination metadata to Integration module ([#391](#391)) ([0372e36](0372e36))
    * **api:** Add pagination metadata to Project module ([#393](#393)) ([bc274fd](bc274fd))
    * **api:** Add pagination metadata to Secret module ([#389](#389)) ([c4cc667](c4cc667))
    * **api:** Add pagination metadata to Variable module ([#390](#390)) ([be6aabf](be6aabf))
    * **api:** Add pagination metadata to Workspace module  ([#387](#387)) ([a08c924](a08c924))
    * **api:** Add pagination metadata to Workspace Role module ([#388](#388)) ([d8e8f49](d8e8f49))
    * **api:** Create a paginate method ([#379](#379)) ([09576f1](09576f1))
    * **api:** Create endpoint for fetching all revisions of a [secure] ([#303](#303)) ([de2b602](de2b602))
    * **api:** Create endpoint for fetching all revisions of a variable ([#304](#304)) ([9abddc1](9abddc1))
    * **cli:** Improved the DX for list profile ([#334](#334)) ([6bff496](6bff496))
    * **platform:** Add warning sonner toast for invalid otp ([#335](#335)) ([21513f5](21513f5))
    
    ### 🐛 Bug Fixes
    
    * **cli:** Added parent directory check ([#359](#359)) ([538ea7f](538ea7f))
    * **platform:** Platform types fixes ([#374](#374)) ([8e9d9ff](8e9d9ff))
    
    ### 📚 Documentation
    
    * Added docker details in setting-things-up.md ([#358](#358)) ([ed5093a](ed5093a))
    * Update postman workspace link ([d6aba27](d6aba27))
    * Updated env and cli docs ([1213d2a](1213d2a))
    
    ### 🔧 Miscellaneous Chores
    
    * Added next backend url in .env.example ([5695254](5695254))
    * **api-client:** Added pagination structure ([a70e957](a70e957))
    * **api-client:** Fixed test script ([ad70819](ad70819))
    * **api-client:** Removed try-catch from tests in environment ([a64e48c](a64e48c))
    * **api:** Add user cache for optimization ([#386](#386)) ([8d730b5](8d730b5))
    * **api:** Alter cache rehydration interval ([f5f9eec](f5f9eec))
    * **api:** Fixed naming error in variable controller ([0c5a380](0c5a380))
    * **api:** Improve handling of edge cases for paginate module ([#402](#402)) ([8591487](8591487))
    * **api:** Minor updates to user service ([249d778](249d778))
    * **api:** Skip workspace creation when user is admin ([#376](#376)) ([13f6c59](13f6c59))
    * **ci:** Add docker check   ([#383](#383)) ([3119001](3119001))
    * **ci:** Add names to CI files ([1a7e5f6](1a7e5f6))
    * **ci:** Add validate CLI pipeline ([#373](#373)) ([a91df6c](a91df6c))
    * **ci:** Adding validate pipeline ([#372](#372)) ([23cf3b3](23cf3b3))
    * **ci:** Disabled platform and api deployments ([74d601a](74d601a))
    * **ci:** Fixed deployment scripts ([12e35db](12e35db))
    * **ci:** Fixed platform script ([d783f2a](d783f2a))
    * **CI:** Include migration deployment in API deploy pipeline ([dbd5222](dbd5222))
    * **CI:** Separated deployment and docker build jobs ([090e193](090e193))
    * **CI:** Setup inter-job dependency ([1756727](1756727))
    * **ci:** Update auto-assign.yaml ([#375](#375)) ([91e0ec1](91e0ec1))
    * **cli:** Changed objects to classes ([#306](#306)) ([c83f2db](c83f2db))
    * Removed Minio config ([8feb83a](8feb83a))
    * Updated deployment scripts and added health check in platform ([fcc1c3f](fcc1c3f))
    
    ### 🔨 Code Refactoring
    
    * **api:** Updated path of some endpoints in project controller ([9502678](9502678))
    * **api:** Updated Redis provider ([33491a1](33491a1))
    @muntaxir4 muntaxir4 deleted the testsPaginate branch November 5, 2024 15:26
    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.

    2 participants