diff --git a/apps/questionnaire/mutations.py b/apps/questionnaire/mutations.py index a217f7b..8867879 100644 --- a/apps/questionnaire/mutations.py +++ b/apps/questionnaire/mutations.py @@ -146,19 +146,18 @@ def bulk_update_questionnair_question_groups_leaf_order( @strawberry.mutation @sync_to_async - def update_question_group_leaf_visibility( + def update_question_groups_leaf_visibility( self, - id: strawberry.ID, + ids: list[strawberry.ID], visibility: QuestionLeafGroupVisibilityActionEnum, info: Info, - ) -> MutationResponseType[QuestionLeafGroupType]: + ) -> BulkBasicMutationResponseType[QuestionLeafGroupType]: if errors := ModelMutation.check_permissions(info, Project.Permission.UPDATE_QUESTION_GROUP): - return MutationResponseType(ok=False, errors=errors) - queryset = QuestionLeafGroupType.get_queryset(None, None, info) - group: QuestionLeafGroup = get_object_or_404(queryset, pk=id) - group.is_hidden = visibility == QuestionLeafGroupVisibilityActionEnum.HIDE - group.save(update_fields=('is_hidden',)) - return MutationResponseType(result=group) + return BulkBasicMutationResponseType(errors=[errors]) + queryset = QuestionLeafGroupType.get_queryset(None, None, info).filter(id__in=ids) + is_hidden = visibility == QuestionLeafGroupVisibilityActionEnum.HIDE + queryset.update(is_hidden=is_hidden) + return BulkBasicMutationResponseType(results=[i for i in queryset]) @strawberry.mutation async def create_question_choice_collection( diff --git a/apps/questionnaire/tests/test_mutations.py b/apps/questionnaire/tests/test_mutations.py index 86ab7bb..ef6cd93 100644 --- a/apps/questionnaire/tests/test_mutations.py +++ b/apps/questionnaire/tests/test_mutations.py @@ -636,17 +636,16 @@ class Mutation: QuestionLeafGroupVisiblityUpdate = ''' mutation MyMutation( $projectId: ID!, - $questionLeafGroupID: ID!, + $questionLeafGroupsID: [ID!]!, $visibility: QuestionLeafGroupVisibilityActionEnum! ) { private { id projectScope(pk: $projectId) { id - updateQuestionGroupLeafVisibility(id: $questionLeafGroupID, visibility: $visibility) { - ok + updateQuestionGroupsLeafVisibility(ids: $questionLeafGroupsID, visibility: $visibility) { errors - result { + results { id name isHidden @@ -694,7 +693,7 @@ def test_question_leaf_group_visibility(self): # Without login variables = { 'projectId': self.gID(project.pk), - 'questionLeafGroupID': self.gID(group2.pk), + 'questionLeafGroupsID': [self.gID(group2.pk)], 'visibility': self.genum(QuestionLeafGroupVisibilityActionEnum.HIDE), } content = self.query_check(self.Mutation.QuestionLeafGroupVisiblityUpdate, variables=variables, assert_errors=True) @@ -711,17 +710,22 @@ def test_question_leaf_group_visibility(self): content = self.query_check( self.Mutation.QuestionLeafGroupVisiblityUpdate, variables=variables, - )['data']['private']['projectScope']['updateQuestionGroupLeafVisibility'] - assert content['ok'] is False, content + )['data']['private']['projectScope']['updateQuestionGroupsLeafVisibility'] + assert content['results'] is None, content assert content['errors'] is not None, content # -- With membership - With write access # -- Invalid question leaf group id project.add_member(user, role=ProjectMembership.Role.MEMBER) - content = self.query_check(self.Mutation.QuestionLeafGroupVisiblityUpdate, variables=variables, assert_errors=True) + content = self.query_check( + self.Mutation.QuestionLeafGroupVisiblityUpdate, + variables=variables, + )['data']['private']['projectScope']['updateQuestionGroupsLeafVisibility'] + assert content['errors'] is None, content # Just empty response + assert content['results'] == [], content # Just empty response # -- Valid question leaf group id - variables['questionLeafGroupID'] = self.gID(group1.pk) + variables['questionLeafGroupsID'] = [self.gID(group1.pk)] for visibility, is_hidden_value in [ (self.genum(QuestionLeafGroupVisibilityActionEnum.HIDE), True), (self.genum(QuestionLeafGroupVisibilityActionEnum.SHOW), False), @@ -730,12 +734,15 @@ def test_question_leaf_group_visibility(self): content = self.query_check( self.Mutation.QuestionLeafGroupVisiblityUpdate, variables=variables, - )['data']['private']['projectScope']['updateQuestionGroupLeafVisibility'] + )['data']['private']['projectScope']['updateQuestionGroupsLeafVisibility'] group1.refresh_from_db() - assert content['ok'] is True, content assert content['errors'] is None, content - assert content['result']['id'] == variables['questionLeafGroupID'], content - assert content['result']['isHidden'] == is_hidden_value, content + assert set([ + d['id'] for d in content['results'] + ]) == set(variables['questionLeafGroupsID']), content + assert set([ + d['isHidden'] for d in content['results'] + ]) == {is_hidden_value}, content assert group1.is_hidden == is_hidden_value def test_question_leaf_group_order_update(self): diff --git a/schema.graphql b/schema.graphql index fa6b7cb..47091ea 100644 --- a/schema.graphql +++ b/schema.graphql @@ -181,7 +181,7 @@ type ProjectScopeMutation { updateQuestion(id: ID!, data: QuestionUpdateInput!): QuestionTypeMutationResponseType! deleteQuestion(id: ID!): QuestionTypeMutationResponseType! bulkUpdateQuestionnairQuestionGroupsLeafOrder(questionnaireId: ID!, data: [QuestionLeafGroupOrderInputType!]!): QuestionLeafGroupTypeBulkBasicMutationResponseType! - updateQuestionGroupLeafVisibility(id: ID!, visibility: QuestionLeafGroupVisibilityActionEnum!): QuestionLeafGroupTypeMutationResponseType! + updateQuestionGroupsLeafVisibility(ids: [ID!]!, visibility: QuestionLeafGroupVisibilityActionEnum!): QuestionLeafGroupTypeBulkBasicMutationResponseType! createQuestionChoiceCollection(data: QuestionChoiceCollectionCreateInput!): QuestionChoiceCollectionTypeMutationResponseType! updateQuestionChoiceCollection(id: ID!, data: QuestionChoiceCollectionUpdateInput!): QuestionChoiceCollectionTypeMutationResponseType! deleteQuestionChoiceCollection(id: ID!): QuestionChoiceCollectionTypeMutationResponseType! @@ -505,12 +505,6 @@ enum QuestionLeafGroupTypeEnum { MATRIX_2D } -type QuestionLeafGroupTypeMutationResponseType { - ok: Boolean! - errors: CustomErrorType - result: QuestionLeafGroupType -} - enum QuestionLeafGroupVisibilityActionEnum { SHOW HIDE