-
-
Notifications
You must be signed in to change notification settings - Fork 966
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: User tags backend adjustments (GSoC) #2460
Conversation
WalkthroughThe recent changes introduce new GraphQL resolvers and enhancements to existing ones for improved management of user tags. These modifications allow for hierarchical tag assignment and unassignment, enabling better organization of tags. Additionally, new queries have been added to retrieve individual tags and their ancestry. Overall, these adjustments enhance the efficiency and robustness of the tagging system within the application. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant API
participant DB
User->>API: assignUserTag(tagId)
activate API
API->>DB: findAllAncestorTags(tagId)
DB-->>API: return ancestorTags
API->>DB: bulkWrite(assignments)
DB-->>API: success
API-->>User: success response
User->>API: unassignUserTag(tagId)
activate API
API->>DB: findAllChildTags(tagId)
DB-->>API: return childTags
API->>DB: deleteMany(relatedAssignments)
DB-->>API: success
API-->>User: success response
Assessment against linked issues
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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 as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
Our Pull Request Approval ProcessWe have these basic policies to make the approval process smoother for our volunteer team. Testing Your CodePlease make sure your code passes all tests. Our test code coverage system will fail if these conditions occur:
The process helps maintain the overall reliability of the code base and is a prerequisite for getting your PR approved. Assigned reviewers regularly review the PR queue and tend to focus on PRs that are passing. ReviewersDo not assign reviewers. Our Queue Monitors will review your PR and assign them.
Reviewing Your CodeYour reviewer(s) will have the following roles:
CONTRIBUTING.mdRead our CONTRIBUTING.md file. Most importantly:
Other
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Outside diff range, codebase verification and nitpick comments (5)
src/resolvers/Query/getUserTag.ts (1)
6-21
: Ensure comprehensive error handling for database operations.The search results indicate that
try-catch
blocks are primarily found in test files and utility scripts. There is no evidence of comprehensive error handling in the main application code, particularly in thegetUserTag
function. Consider addingtry-catch
blocks around database operations to handle potential connection issues or other database-related errors.
- Location to update:
src/resolvers/Query/getUserTag.ts
: Add error handling for database operations.Analysis chain
Ensure comprehensive error handling.
The
getUserTag
function correctly handles the case where a tag is not found. However, consider handling potential database errors, such as connection issues, to improve robustness.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify error handling for database operations in the codebase. # Test: Search for error handling in database operations. Expect: Comprehensive error handling. rg --type javascript -A 5 $'catch'Length of output: 68
Script:
#!/bin/bash # Description: Verify error handling for database operations in the codebase. # Test: Search for error handling in database operations. Expect: Comprehensive error handling. rg 'try\s*{'Length of output: 92
Script:
#!/bin/bash # Description: Verify error handling for database operations in the codebase. # Test: Search for error handling in database operations. Expect: Comprehensive error handling. rg 'try\s*\{'Length of output: 39469
src/resolvers/Query/getUserTagAncestors.ts (1)
6-31
: Enhance Error Handling and Loop Logic ingetUserTagAncestors
FunctionThe
getUserTagAncestors
function lacks explicit error handling for database operations, which could lead to unhandled exceptions. Additionally, ensure the loop has safeguards against potential infinite loops due to inconsistent data.
- Consider wrapping database calls in
try-catch
blocks to handle potential errors.- Review the loop logic to ensure it terminates correctly, especially in cases of data inconsistency.
Analysis chain
Consider optimizing error handling and loop logic.
The
getUserTagAncestors
function correctly retrieves ancestors but could benefit from handling potential database errors. Additionally, ensure the loop terminates correctly to avoid infinite loops if data is inconsistent.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify error handling and loop termination in the codebase. # Test: Search for error handling in loops and database operations. Expect: Proper error handling and loop termination. rg --type javascript -A 5 $'catch'Length of output: 68
Script:
#!/bin/bash # Search for try-catch blocks in the getUserTagAncestors function to check error handling. ast-grep --lang typescript --pattern $'async $_($_, $_) { try { $$$ } catch ($_){ $$$ } }' --json # Check for any comments or annotations related to error handling or loop termination in the file. rg --type ts -C 3 'error|loop|termination' src/resolvers/Query/getUserTagAncestors.tsLength of output: 1105
tests/resolvers/Query/getUserTag.spec.ts (1)
13-13
: Optimize import for type-only usage.The
TestUserTagType
is used only as a type. Consider using TypeScript'simport type
syntax for clarity.- import { - createRootTagWithOrg, - createTwoLevelTagsWithOrg, - TestUserTagType, - } from "../../helpers/tags"; + import type { TestUserTagType } from "../../helpers/tags"; + import { createRootTagWithOrg } from "../../helpers/tags";Tools
GitHub Check: Check for linting, formatting, and type errors
[failure] 13-13:
Imports "TestUserTagType" are only used as typetests/resolvers/Query/getUserTagAncestors.spec.ts (1)
9-9
: Use type-only import forTestUserTagType
.The
TestUserTagType
import is only used as a type and should be imported as such to follow TypeScript best practices.- import { createTwoLevelTagsWithOrg, TestUserTagType } from "../../helpers/tags"; + import { createTwoLevelTagsWithOrg } from "../../helpers/tags"; + import type { TestUserTagType } from "../../helpers/tags";Tools
GitHub Check: Check for linting, formatting, and type errors
[failure] 9-9:
Imports "TestUserTagType" are only used as typesrc/resolvers/Mutation/assignUserTag.ts (1)
171-180
: Consider Adding Error Handling forbulkWrite
OperationThe
bulkWrite
operation does not appear to have explicit error handling insrc/resolvers/Mutation/assignUserTag.ts
. It is advisable to wrap this operation in a try-catch block to handle potential errors effectively, ensuring the robustness of the application.
- Consider wrapping the
bulkWrite
call in a try-catch block.- Implement appropriate error handling or logging within the catch block.
Analysis chain
LGTM! Verify the structure of bulk operations in
bulkWrite
.The use of
bulkWrite
is efficient for batch processing. Ensure that the operations are correctly structured and handle potential errors.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the structure of bulk operations in `bulkWrite`. # Test: Search for `bulkWrite` usage and check for proper error handling. rg --type ts 'bulkWrite' src/resolvers/Mutation/assignUserTag.tsLength of output: 104
Script:
#!/bin/bash # Description: Check for error handling related to `bulkWrite` in the same file. # Search for try-catch blocks or error handling mechanisms in the file. rg --type ts 'try|catch|error' src/resolvers/Mutation/assignUserTag.tsLength of output: 394
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (16)
- src/resolvers/Mutation/assignUserTag.ts (1 hunks)
- src/resolvers/Mutation/unassignUserTag.ts (1 hunks)
- src/resolvers/Mutation/updateUserTag.ts (2 hunks)
- src/resolvers/Organization/userTags.ts (1 hunks)
- src/resolvers/Query/getUserTag.ts (1 hunks)
- src/resolvers/Query/getUserTagAncestors.ts (1 hunks)
- src/resolvers/Query/index.ts (2 hunks)
- src/typeDefs/inputs.ts (1 hunks)
- src/typeDefs/queries.ts (1 hunks)
- src/typeDefs/types.ts (2 hunks)
- src/types/generatedGraphQLTypes.ts (8 hunks)
- tests/resolvers/Mutation/assignUserTag.spec.ts (3 hunks)
- tests/resolvers/Mutation/unassignUserTag.spec.ts (2 hunks)
- tests/resolvers/Mutation/updateUserTag.spec.ts (6 hunks)
- tests/resolvers/Query/getUserTag.spec.ts (1 hunks)
- tests/resolvers/Query/getUserTagAncestors.spec.ts (1 hunks)
Additional context used
GitHub Check: Check for linting, formatting, and type errors
tests/resolvers/Query/getUserTag.spec.ts
[failure] 8-8:
'QueryGetUserTagAncestorsArgs' is defined but never used
[failure] 12-12:
'TestUserType' is defined but never used
[failure] 13-13:
Imports "TestUserTagType" are only used as type
[failure] 15-15:
'createTwoLevelTagsWithOrg' is defined but never usedtests/resolvers/Query/getUserTagAncestors.spec.ts
[failure] 9-9:
Imports "TestUserTagType" are only used as typetests/resolvers/Mutation/unassignUserTag.spec.ts
[failure] 33-33:
'testSubTag2' is assigned a value but never usedtests/resolvers/Mutation/assignUserTag.spec.ts
[failure] 38-38:
'testSubTag1' is assigned a value but never used
Additional comments not posted (28)
tests/resolvers/Query/getUserTagAncestors.spec.ts (3)
17-24
: Setup and teardown are correctly implemented.The
beforeAll
andafterAll
functions ensure proper database connection management.
26-46
: First test case is well-structured.The test correctly verifies that a
NotFoundError
is thrown for a non-existent tag ID and mocks the translation function.
48-60
: Second test case is correctly implemented.The test accurately verifies the functionality of retrieving ancestor tags.
src/resolvers/Query/index.ts (2)
37-38
: Import statements are correctly structured.The new resolvers
getUserTag
andgetUserTagAncestors
are appropriately included.
89-90
: Export statements are correctly integrated.The new resolvers are correctly added to the
Query
export, enhancing the API's functionality.src/typeDefs/queries.ts (1)
128-130
: New query definitions are correctly structured.The
getUserTag
andgetUserTagAncestors
queries are well-defined and enhance the API's data retrieval capabilities.src/resolvers/Mutation/updateUserTag.ts (2)
133-134
: LGTM! Verify consistent usage ofargs.input.tagId
.The change to use
args.input.tagId
for updating the tag is appropriate. Ensure this identifier is consistently used throughout the codebase.
77-78
: LGTM! Verify consistent usage ofargs.input.tagId
.The change to use
args.input.tagId
for retrieving the tag is appropriate. Ensure this identifier is consistently used throughout the codebase.Verification successful
Consistent Usage of
args.input.tagId
VerifiedThe identifier
args.input.tagId
is used consistently for tag retrieval within the file. Other instances of_id
are contextually distinct and appropriately used for different purposes. No further changes are needed.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify consistent usage of `args.input.tagId` in the file. # Test: Search for `args.input.tagId` usage. Expect: Consistent usage throughout the file. rg --type ts 'args\.input\.tagId' src/resolvers/Mutation/updateUserTag.tsLength of output: 131
Script:
#!/bin/bash # Description: Check for any remaining usage of `_id` that might need updating. # Test: Search for `_id` usage. Expect: No remaining usage related to the old identifier. rg --type ts '_id' src/resolvers/Mutation/updateUserTag.tsLength of output: 172
src/resolvers/Organization/userTags.ts (1)
75-76
: LGTM! Verify the impact of theparentTagId: null
filter.The addition of
parentTagId: null
ensures that only top-level tags are fetched. Verify that this aligns with the expected data retrieval requirements.src/resolvers/Mutation/assignUserTag.ts (2)
155-167
: LGTM! Verify handling of edge cases in ancestor tag collection.The logic for collecting ancestor tags is sound. Ensure that the loop properly handles cases with null
parentTagId
and avoids circular references.
180-181
: LGTM! Verify completeness of the returnedrequestUser
.Returning the
requestUser
is appropriate. Ensure that the returned object includes all necessary information for subsequent operations.Verification successful
To verify the completeness of the
requestUser
, we need to check theUser
model to understand what fields are included whenfindOne
is called. This will help ensure thatrequestUser
contains all necessary information for subsequent operations.
To verify the completeness of the
requestUser
, we need to inspect theUser
model insrc/models/User.ts
to understand its structure and fields.
The
requestUser
appears to be complete and includes necessary fields.The
requestUser
is based on theUser
model, which includes comprehensive fields such asjoinedOrganizations
,address
, and more. This ensures that the returned user object is complete for subsequent operations.
- The
User
model is structured with a detailed interface and schema, ensuring all relevant user information is included.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify completeness of the returned `requestUser`. # Test: Search for `requestUser` usage to ensure it includes necessary fields. rg --type ts 'requestUser' src/resolvers/Mutation/assignUserTag.tsLength of output: 304
Script:
#!/bin/bash # Description: Check the User model to verify fields included in the requestUser. # Find the User model definition to inspect its fields. fd -e ts User # Once found, extract the fields defined in the User model. ast-grep --lang typescript --pattern 'class User { $$$ }' --jsonLength of output: 3562
Script:
#!/bin/bash # Description: Inspect the User model to verify its structure and fields. # Search for the User model structure in the file to understand its fields. rg --type ts 'User' src/models/User.ts -A 20Length of output: 2831
src/resolvers/Mutation/unassignUserTag.ts (1)
148-177
: Efficient handling of nested tags.The changes efficiently handle nested tag structures by retrieving all child tags and using
deleteMany
for unassignment. This approach reduces the number of database queries and optimizes performance. However, consider testing the performance impact with large tag hierarchies to ensure scalability.tests/resolvers/Mutation/updateUserTag.spec.ts (1)
63-63
: Improved clarity in test input structure.The replacement of
_id
withtagId
in the test cases enhances readability and aligns with the mutation's purpose. Ensure that this change is consistently applied throughout the codebase to maintain uniformity.Also applies to: 95-95, 128-128, 165-165, 201-201, 229-229
tests/resolvers/Mutation/unassignUserTag.spec.ts (1)
250-298
: Comprehensive test for cascading unassignment.The new test case effectively verifies the unassignment of all child and descendant tags when a parent tag is unassigned. This ensures the cascading unassignment logic is correctly implemented and robustly tested.
tests/resolvers/Mutation/assignUserTag.spec.ts (4)
25-28
: LGTM on imports and variable declarations.The new imports and variables are appropriate for the test enhancements.
Also applies to: 35-39
44-46
: LGTM onbeforeAll
setup changes.The setup correctly initializes two-level tags and a new admin user for testing.
222-245
: LGTM on successful tag assignment test.The test correctly verifies that the tag is assigned and the correct user is returned.
Line range hint
247-276
: LGTM on ancestor tag assignment test.The test correctly verifies that both the sub-tag and ancestor tags are assigned.
src/typeDefs/inputs.ts (1)
493-494
: LGTM onUpdateUserTagInput
changes.Renaming
_id
totagId
improves clarity, and makingtagColor
nullable adds flexibility.src/typeDefs/types.ts (1)
745-745
: LGTM ontotalCount
field changes.Changing
totalCount
toInt
inUserTagsConnection
and adding it toUsersConnection
enhances flexibility and functionality.Also applies to: 762-762
src/types/generatedGraphQLTypes.ts (8)
2309-2309
: Addition ofgetUserTag
field is correct.The new
getUserTag
field in theQuery
type is properly defined as optional, following GraphQL best practices.
2310-2310
: Addition ofgetUserTagAncestors
field is correct.The new
getUserTagAncestors
field in theQuery
type is correctly defined to handle nullable values, enhancing robustness.
2526-2528
: Addition ofQueryGetUserTagArgs
type is correct.The
QueryGetUserTagArgs
type is properly defined with anid
argument, aligning with GraphQL conventions.
2531-2533
: Addition ofQueryGetUserTagAncestorsArgs
type is correct.The
QueryGetUserTagAncestorsArgs
type is properly defined with anid
argument, aligning with GraphQL conventions.
2924-2924
: Modification oftagColor
field to optional is correct.Changing the
tagColor
field to optional inUpdateUserTagInput
provides greater flexibility for updates.
2925-2925
: Addition oftagId
field is correct.The
tagId
field inUpdateUserTagInput
is necessary for associating updates with specific tags, ensuring accuracy.
3099-3099
: Modification oftotalCount
field inUserTagsConnection
is correct.Changing
totalCount
to a nullable integer enhances flexibility and aligns with GraphQL best practices.
3148-3148
: Modification oftotalCount
field inUsersConnection
is correct.Changing
totalCount
to a nullable integer enhances flexibility and aligns with GraphQL best practices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (5)
- schema.graphql (4 hunks)
- tests/resolvers/Mutation/assignUserTag.spec.ts (3 hunks)
- tests/resolvers/Mutation/unassignUserTag.spec.ts (2 hunks)
- tests/resolvers/Query/getUserTag.spec.ts (1 hunks)
- tests/resolvers/Query/getUserTagAncestors.spec.ts (1 hunks)
Files skipped from review as they are similar to previous changes (2)
- tests/resolvers/Query/getUserTag.spec.ts
- tests/resolvers/Query/getUserTagAncestors.spec.ts
Additional comments not posted (20)
tests/resolvers/Mutation/unassignUserTag.spec.ts (7)
Line range hint
57-77
:
LGTM!The test case correctly verifies the
NotFoundError
when no user exists with the given context user ID.
Line range hint
87-107
:
LGTM!The test case correctly verifies the
NotFoundError
when no user exists with the given input user ID.
Line range hint
117-137
:
LGTM!The test case correctly verifies the
NotFoundError
when no tag exists with the given tag ID.
Line range hint
147-167
:
LGTM!The test case correctly verifies the
Not Authorized Error
when the current user is not authorized to unassign the tag.
Line range hint
177-197
:
LGTM!The test case correctly verifies the
USER_DOES_NOT_HAVE_THE_TAG
error when the user does not have the tag.
Line range hint
207-243
:
LGTM!The test case correctly verifies successful tag unassignment and ensures the user is returned.
249-295
: LGTM!The test case correctly verifies the unassignment of all child and descendant tags when a parent tag is unassigned.
tests/resolvers/Mutation/assignUserTag.spec.ts (8)
Line range hint
57-77
:
LGTM!The test case correctly verifies the
NotFoundError
when no user exists with the given context user ID.
Line range hint
87-107
:
LGTM!The test case correctly verifies the
NotFoundError
when no user exists with the given input user ID.
Line range hint
117-137
:
LGTM!The test case correctly verifies the
NotFoundError
when no tag exists with the given tag ID.
Line range hint
147-167
:
LGTM!The test case correctly verifies the
Not Authorized Error
when the current user is not authorized to assign the tag.
Line range hint
177-197
:
LGTM!The test case correctly verifies the error when the user is not a member of the organization of the tag being assigned.
220-243
: LGTM!The test case correctly verifies successful tag assignment and ensures the user is returned.
Line range hint
245-274
:
LGTM!The test case correctly verifies the assignment of all ancestor tags when a sub-tag is assigned.
Line range hint
276-296
:
LGTM!The test case correctly verifies the
USER_ALREADY_HAS_TAG
error when a tag is already assigned to the user.schema.graphql (5)
1517-1517
: LGTM!The
getUserTag
query is correctly defined to retrieve a user tag by ID.
1518-1518
: LGTM!The
getUserTagAncestors
query is correctly defined to retrieve ancestor tags by ID.
1805-1806
: LGTM!The changes to
UpdateUserTagInput
enhance flexibility by makingtagColor
optional and addingtagId
for specific identification.
1944-1944
: LGTM!The change to
totalCount
inUserTagsConnection
broadens the range of values it can represent, which is appropriate.
1992-1992
: LGTM!The addition of
totalCount
toUsersConnection
provides valuable metadata about the connection.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #2460 +/- ##
=========================================
Coverage 98.40% 98.40%
=========================================
Files 355 357 +2
Lines 28350 28453 +103
Branches 2388 2400 +12
=========================================
+ Hits 27897 28000 +103
Misses 450 450
Partials 3 3 ☔ View full report in Codecov by Sentry. |
db3d2f5
into
PalisadoesFoundation:develop
What kind of change does this PR introduce?
Makes backend adjustments required for implementing tag screens in the admin.
Issue Number:
Fixes #2459
Did you add tests for your changes?
Yes
Does this PR introduce a breaking change?
No
Additional Context
Admin issue: PalisadoesFoundation/talawa-admin#2037
Summary by CodeRabbit
New Features
Bug Fixes
_id
totagId
.Tests
getUserTag
andgetUserTagAncestors
resolvers.