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

fix(api): Only user's default workspace returns isDefault: true #647

Merged
merged 4 commits into from
Jan 24, 2025

Conversation

csehatt741
Copy link
Contributor

@csehatt741 csehatt741 commented Jan 20, 2025

User description

Description

workspace.isDefault property mapping takes into account the owner in order to avoid returning multiple defaults.

Fixes #587

Screenshots of relevant screens

image

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

Bug fix


Description

  • Fixes the issue of multiple workspaces being marked as isDefault.

  • Ensures only the user's default workspace has isDefault: true.

  • Updates logic in getWorkspaceBySlug to validate ownership for isDefault.

  • Adjusts getAllWorkspaces to correctly map isDefault based on ownership.


Changes walkthrough 📝

Relevant files
Bug fix
workspace.service.ts
Fix `isDefault` property logic in workspace service           

apps/api/src/workspace/service/workspace.service.ts

  • Updated getWorkspaceBySlug to ensure isDefault is true only for the
    owner's default workspace.
  • Modified getAllWorkspaces to map isDefault based on ownership.
  • Added logic to validate isDefault against ownerId for both methods.
  • +19/-7   

    Need help?
  • Type /help how to ... in the comments thread for any question about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    587 - PR Code Verified

    Compliant requirements:

    • Fix bug where all workspaces are returned with isDefault:true
    • Only one workspace (user's default workspace) should have isDefault:true
    • Fix affected endpoints: /workspace and /workspace/:workspaceSlug

    Requires further human verification:

    • Verify that the fix works when a user is invited to multiple workspaces
    • Test the API responses in real scenarios to confirm only one default workspace
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Logic Validation

    Verify that comparing ownerId with user.id is the correct approach for determining default workspace status. Consider if there are edge cases where this logic might not work as expected.

    isDefault: workspace.isDefault && workspace.ownerId == user.id

    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Jan 20, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    ✅ Use strict equality comparison
    Suggestion Impact:Changed the equality operator from == to === in the workspace comparison logic

    code diff:

    +      isDefault: workspace.isDefault && workspace.ownerId === user.id

    Use strict equality operator (===) instead of loose equality (==) when comparing IDs
    to ensure type safety and prevent potential type coercion issues.

    apps/api/src/workspace/service/workspace.service.ts [184]

    -isDefault: workspace.isDefault && workspace.ownerId == user.id
    +isDefault: workspace.isDefault && workspace.ownerId === user.id

    [Suggestion has been applied]

    Suggestion importance[1-10]: 5

    Why: Using strict equality (===) instead of loose equality (==) is a TypeScript best practice that prevents potential type coercion issues, though in this context with ID comparison, the practical impact is minimal since IDs are likely of the same type.

    5


    return {
    ...workspace,
    isDefault: workspace.isDefault && workspace.ownerId === user.id
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    Suggestion: ✅ Use strict equality comparison [General, importance: 5]

    Suggested change
    isDefault: workspace.isDefault && workspace.ownerId === user.id
    isDefault: workspace.isDefault && workspace.ownerId === user.id

    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.

    The entire file shows changes. Can you once revert everything and do it again?

    @csehatt741 csehatt741 force-pushed the get-workspaces-isdefault branch from 5b7c9ee to 49e7f85 Compare January 21, 2025 07:16
    @csehatt741
    Copy link
    Contributor Author

    Changes updated

    @csehatt741 csehatt741 requested a review from rajdip-b January 21, 2025 07:19
    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.

    I feel the best thing todo here would be to move this default checking to authorityService.checkAuthorityOverWorkspace. That way, we will ensure that these changes are propagated to any endpoint that fetches single workspaces aswell.

    The change in the next section of this file stays as is ofcourse

    @csehatt741 csehatt741 force-pushed the get-workspaces-isdefault branch from 49e7f85 to 256ff02 Compare January 24, 2025 04:20
    @csehatt741
    Copy link
    Contributor Author

    csehatt741 commented Jan 24, 2025

    WorkspaceService.getWorkspacesOfUser does not invoke AuthorityCheckerService.checkAuthorityOverWorkspace, as the user is authorized to access all of their workspaces. Moving mapping logic into AuthorityCheckerService does not help where authorization is unnecessary.

    @csehatt741
    Copy link
    Contributor Author

    To my mind, isolating the internal model from the model returned via the controller (DTO) by introducing the Workspace DTO and extracting mapping logic from the service would solve the issue. At least, mapping logic should be extracted from the service.

    @rajdip-b
    Copy link
    Member

    I agree with you. The thing is, we don't have mapping dtos at all. Introducting one might make us introduce others aswell. So i guess, for now we can let this be.

    @rajdip-b rajdip-b merged commit 870b4dc into keyshade-xyz:develop Jan 24, 2025
    4 checks passed
    rajdip-b pushed a commit that referenced this pull request Jan 24, 2025
    ## [2.10.0-stage.2](v2.10.0-stage.1...v2.10.0-stage.2) (2025-01-24)
    
    ### 🐛 Bug Fixes
    
    * **api:** Only user's default workspace returns isDefault: true ([#647](#647)) ([870b4dc](870b4dc))
    
    ### 🔧 Miscellaneous Chores
    
    * Fix prerelease branch config ([7e84021](7e84021))
    @rajdip-b
    Copy link
    Member

    🎉 This PR is included in version 2.10.0-stage.2 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    Copy link

    codecov bot commented Jan 24, 2025

    Codecov Report

    All modified and coverable lines are covered by tests ✅

    Project coverage is 86.79%. Comparing base (ce50743) to head (eb9ad02).
    Report is 382 commits behind head on develop.

    Additional details and impacted files
    @@             Coverage Diff             @@
    ##           develop     #647      +/-   ##
    ===========================================
    - Coverage    91.71%   86.79%   -4.92%     
    ===========================================
      Files          111      113       +2     
      Lines         2510     2969     +459     
      Branches       469      447      -22     
    ===========================================
    + Hits          2302     2577     +275     
    - Misses         208      392     +184     
    Flag Coverage Δ
    api-e2e-tests 86.79% <100.00%> (-4.92%) ⬇️

    Flags with carried forward coverage won't be shown. Click here to find out more.

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @csehatt741 csehatt741 deleted the get-workspaces-isdefault branch January 24, 2025 07:31
    rajdip-b pushed a commit that referenced this pull request Jan 28, 2025
    ## [2.10.0](v2.9.1...v2.10.0) (2025-01-28)
    
    ### 🚀 Features
    
    * **api:** Secret rotation ([#652](#652)) ([ad9a808](ad9a808))
    * **platform:** Implement delete project ([#671](#671)) ([d243c89](d243c89))
    * **platform:** Improved UI of [secure] listing ([#655](#655)) ([b19de47](b19de47))
    * **platform:** Operate on environments ([#670](#670)) ([f45c5fa](f45c5fa))
    
    ### 🐛 Bug Fixes
    
    * Added lockfile ([856eb3c](856eb3c))
    * **api:** Only user's default workspace returns isDefault: true ([#647](#647)) ([870b4dc](870b4dc))
    * **cli:** Workspace membership API client payload fixed ([#614](#614)) ([#648](#648)) ([e23057b](e23057b))
    * **platform:** Refactor layout structure to improve Navbar positioning & child component ([#661](#661)) ([31067f3](31067f3))
    * **Platfrom:** Replace manual date calculation with dayjs to improve better calculation ([#668](#668)) ([990eb86](990eb86))
    
    ### 🔧 Miscellaneous Chores
    
    * **ci:** Add manual trigger ([cfbf4b9](cfbf4b9))
    * **ci:** Add missing LATEST_TAG variable ([a2ea2ed](a2ea2ed))
    * **ci:** Fixed misplaced sentry sourcemaps commands ([fbd6f3b](fbd6f3b))
    * **ci:** Fixed scripts ([374f7ed](374f7ed))
    * **ci:** Update API sentry dist folder ([2bc9afb](2bc9afb))
    * **CI:** Update pipeline ([fd63b70](fd63b70))
    * **ci:** Update sourcemap upload commands ([c7e8e45](c7e8e45))
    * **cli:** Bumped CLI version to 2.5.0 ([7b772f8](7b772f8))
    * Fix prerelease branch config ([7e84021](7e84021))
    * **release:** 2.10.0-stage.1 [skip ci] ([a4f8414](a4f8414)), closes [#652](#652)
    * **release:** 2.10.0-stage.2 [skip ci] ([00ee123](00ee123)), closes [#647](#647)
    * **release:** 2.10.0-stage.3 [skip ci] ([941a815](941a815)), closes [#655](#655)
    * **release:** 2.10.0-stage.4 [skip ci] ([ae7c44f](ae7c44f)), closes [#614](#614) [#648](#648)
    * **release:** 2.10.0-stage.5 [skip ci] ([d718483](d718483)), closes [#661](#661)
    * **release:** 2.10.0-stage.6 [skip ci] ([4e63f47](4e63f47)), closes [#668](#668)
    * **release:** 2.10.0-stage.7 [skip ci] ([4a35fe7](4a35fe7)), closes [#671](#671)
    * **release:** 2.10.0-stage.8 [skip ci] ([36ef21d](36ef21d)), closes [#670](#670)
    * **release:** 2.9.2-stage.1 [skip ci] ([443f8d4](443f8d4))
    
    ### 🔨 Code Refactoring
    
    * **api:** Update getSelf function ([fe752ce](fe752ce))
    @rajdip-b
    Copy link
    Member

    🎉 This PR is included in version 2.10.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: Get all workspaces of user returns every workspace as default
    2 participants