diff --git a/apps/assessment_registry/filters.py b/apps/assessment_registry/filters.py index b078ec6162..352b7b0db3 100644 --- a/apps/assessment_registry/filters.py +++ b/apps/assessment_registry/filters.py @@ -77,8 +77,10 @@ def filter_assessment_registry_issues(self, qs, name, value): ) def filter_is_parent(self, qs, name, value): - if not value: + if value is None: return qs - return qs.filter( - parent__isnull=True - ) + if value: + # for parent + return qs.filter(parent__isnull=True) + # for child + return qs.filter(parent__isnull=False) diff --git a/apps/assessment_registry/schema.py b/apps/assessment_registry/schema.py index 9ba6c60821..4ff2fbd229 100644 --- a/apps/assessment_registry/schema.py +++ b/apps/assessment_registry/schema.py @@ -28,6 +28,7 @@ Answer, AssessmentRegistryOrganization, ) +from .utils import get_hierarchy_level from .filters import AssessmentRegistryGQFilterSet, AssessmentRegistryIssueGQFilterSet from .enums import ( AssessmentRegistryCrisisTypeEnum, @@ -184,7 +185,7 @@ class ScoreAnalyticalDensityType(DjangoObjectType, UserResourceMixin, ClientIdMi class Meta: model = ScoreAnalyticalDensity only_fields = ( - 'id', + 'id', 'score', ) sector = graphene.Field(AssessmentRegistrySectorTypeEnum, required=True) @@ -260,6 +261,8 @@ class AssessmentRegistrySummaryIssueType(DjangoObjectType, UserResourceMixin): sub_pillar_display = EnumDescription(source='get_sub_pillar_display', required=False) sub_dimension = graphene.Field(AssessmentRegistrySummarySubDimensionTypeEnum, required=False) sub_dimension_display = EnumDescription(source='get_sub_dimension_display', required=False) + child_count = graphene.Int(required=True) + level = graphene.Int(required=True) class Meta: model = SummaryIssue @@ -270,6 +273,12 @@ class Meta: 'full_label', ] + def resolve_child_count(root, info, **kwargs): + return SummaryIssue.objects.filter(parent=root).count() + + def resolve_level(root, info, **kwargs): + return get_hierarchy_level(root) + class AssessmentRegistrySummaryIssueListType(CustomDjangoListObjectType): class Meta: diff --git a/apps/assessment_registry/tests/test_schemas.py b/apps/assessment_registry/tests/test_schemas.py index 970e236f4b..5494382029 100644 --- a/apps/assessment_registry/tests/test_schemas.py +++ b/apps/assessment_registry/tests/test_schemas.py @@ -301,3 +301,39 @@ def _query_check(**kwargs): self.force_login(non_confidential_member_user) content = _query_check(okay=False) self.assertEqual(content['data']['project']['assessmentRegistries']['totalCount'], 3) + + def test_issue_list_query_filter(self): + query = ''' + query MyQuery ( + $isParent: Boolean + $label: String + ) { + assessmentRegSummaryIssues( + isParent: $isParent + label: $label + ) { + results { + id + } + } + } + ''' + + member_user = UserFactory.create() + self.force_login(member_user) + + parent_issue1, parent_issue2, parent_issue3 = SummaryIssueFactory.create_batch(3, parent=None) + child_issue1 = SummaryIssueFactory.create(parent=parent_issue1) + child_issue2 = SummaryIssueFactory.create(parent=parent_issue2) + + for filter_data, expected_issues in [ + ({}, [child_issue1, child_issue2, parent_issue1, parent_issue2, parent_issue3]), + ({'isParent': True}, [parent_issue1, parent_issue2, parent_issue3]), + ({'isParent': False}, [child_issue1, child_issue2,]), + ]: + content = self.query_check(query, variables={**filter_data}) + + self.assertListIds( + content['data']['assessmentRegSummaryIssues']['results'], expected_issues, + {'response': content, 'filter': filter_data} + ) diff --git a/schema.graphql b/schema.graphql index 6647bfb57f..ec37fb021f 100644 --- a/schema.graphql +++ b/schema.graphql @@ -926,6 +926,8 @@ type AssessmentRegistrySummaryIssueType { subPillarDisplay: EnumDescription subDimension: AssessmentRegistrySummarySubDimensionTypeEnum subDimensionDisplay: EnumDescription + childCount: Int + level: Int } enum AssessmentRegistrySummaryPillarTypeEnum {