Skip to content

Commit

Permalink
Add bulk action for group leaf visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Sep 5, 2023
1 parent 16a4633 commit 413c8e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
17 changes: 8 additions & 9 deletions apps/questionnaire/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
33 changes: 20 additions & 13 deletions apps/questionnaire/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand All @@ -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):
Expand Down
8 changes: 1 addition & 7 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -505,12 +505,6 @@ enum QuestionLeafGroupTypeEnum {
MATRIX_2D
}

type QuestionLeafGroupTypeMutationResponseType {
ok: Boolean!
errors: CustomErrorType
result: QuestionLeafGroupType
}

enum QuestionLeafGroupVisibilityActionEnum {
SHOW
HIDE
Expand Down

0 comments on commit 413c8e8

Please sign in to comment.