keyshade-xyz / keyshade

Realtime secret and configuration management tool, with the best in class security and seamless integration support
https://keyshade.xyz
Mozilla Public License 2.0
118 stars 55 forks source link

feat(api): Added getRevisionsOfVariable method #304

Open yogesh1801 opened 3 days ago

yogesh1801 commented 3 days ago

User description

Description

Adds a getRevisionsOfVariable method to get revisions of a variable

Fixes #271

Dependencies

Mention any dependencies/packages used

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

If changes are made in the code:

Documentation Update


PR Type

Enhancement, Tests


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
variable.controller.ts
Add endpoint to fetch variable revisions with pagination 

apps/api/src/variable/controller/variable.controller.ts
  • Added getRevisionsOfVariable endpoint to fetch variable revisions.
  • Implemented pagination for the new endpoint.
  • +18/-0   
    variable.service.ts
    Implement method to retrieve variable revisions with authority checks

    apps/api/src/variable/service/variable.service.ts
  • Added getRevisionsOfVariable method to retrieve variable revisions.
  • Included authority checks for variable and environment access.
  • +33/-0   
    Tests
    variable.e2e.spec.ts
    Add tests for getRevisionsOfVariable endpoint                       

    apps/api/src/variable/variable.e2e.spec.ts
  • Added tests for getRevisionsOfVariable endpoint.
  • Included tests for various scenarios like no revisions, non-existent
    variable/environment, and access control.
  • +105/-1 

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

    codiumai-pr-agent[bot] commented 3 days ago

    PR Reviewer Guide πŸ”

    ⏱️ Estimated effort to review [1-5] 3
    πŸ§ͺ Relevant tests Yes
    πŸ”’ Security concerns No
    ⚑ Key issues to review None
    codiumai-pr-agent[bot] commented 3 days ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add validation for pagination parameters to ensure they are non-negative and within a reasonable range ___ **Consider validating the page and limit query parameters to ensure they are non-negative
    integers. This prevents potential issues with pagination logic where negative values could
    lead to unexpected behavior or errors.** [apps/api/src/variable/controller/variable.controller.ts [109-110]](https://github.com/keyshade-xyz/keyshade/pull/304/files#diff-7639f037978995ad2ebde29feddffad312b5f7f22625024df30c59d3c0b7927aR109-R110) ```diff -@Query('page') page: number = 0, -@Query('limit') limit: number = 10 +@Query('page', new ParseIntPipe({ min: 0 })) page: number = 0, +@Query('limit', new ParseIntPipe({ min: 1, max: 100 })) limit: number = 10 ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: This suggestion improves the robustness of the API by ensuring that pagination parameters are within a valid range, preventing potential issues with negative values or excessively large limits.
    9
    Error handling
    Handle the case where no revisions are found by throwing a NotFoundException ___ **Implement error handling for the scenario where no revisions are found for the given
    variable and environment IDs. This could involve returning a specific message or an empty
    array, depending on the desired API behavior.** [apps/api/src/variable/service/variable.service.ts [628-635]](https://github.com/keyshade-xyz/keyshade/pull/304/files#diff-db1895137a9c036373abefb8c924150d4f2022fd6d4f5271cc6c95c443bf01cfR628-R635) ```diff const revisions = await this.prisma.variableVersion.findMany({ where: { variableId: variableId, environmentId: environmentId }, skip: page * limit, take: limit }) +if (revisions.length === 0) { + throw new NotFoundException(`No revisions found for variable ID ${variableId} in environment ID ${environmentId}.`); +} ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Implementing error handling for cases where no revisions are found improves the API's clarity and user experience by providing specific feedback when no data is available.
    8
    Performance
    Suggest adding a database index to improve query performance for fetching variable revisions ___ **To optimize database queries, consider adding an index on the variableId and environmentId
    columns in the variableVersion table if not already present. This can significantly
    improve the performance of the query fetching variable revisions.** [apps/api/src/variable/service/variable.service.ts [628-635]](https://github.com/keyshade-xyz/keyshade/pull/304/files#diff-db1895137a9c036373abefb8c924150d4f2022fd6d4f5271cc6c95c443bf01cfR628-R635) ```diff +// Ensure your database has an index on `variableId` and `environmentId` for the `variableVersion` table. const revisions = await this.prisma.variableVersion.findMany({ where: { variableId: variableId, environmentId: environmentId }, skip: page * limit, take: limit }) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Adding an index can significantly improve query performance, especially for large datasets. However, this suggestion is more of a general optimization tip and may not be directly applicable to the PR code diff.
    7
    Maintainability
    Enhance method signature by adding explicit return type annotations ___ **Add explicit type annotations for the parameters in the getRevisionsOfVariable method to
    enhance code readability and maintainability.** [apps/api/src/variable/controller/variable.controller.ts [105-110]](https://github.com/keyshade-xyz/keyshade/pull/304/files#diff-7639f037978995ad2ebde29feddffad312b5f7f22625024df30c59d3c0b7927aR105-R110) ```diff async getRevisionsOfVariable( @CurrentUser() user: User, @Param('variableId') variableId: string, @Param('environmentId') environmentId: string, @Query('page') page: number = 0, @Query('limit') limit: number = 10 -) +): Promise ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: Adding explicit return type annotations improves code readability and maintainability, but it is a minor enhancement compared to functional improvements or bug fixes.
    6