Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
delcroip committed Sep 5, 2024
2 parents ea2d12f + d2588c5 commit fa02b08
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 32 deletions.
2 changes: 1 addition & 1 deletion social_protection/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def apply_filter_to_queryset(self, custom_filters: List[namedtuple], query: Quer
field, value_type = field.rsplit('__', 1)
value = self.__cast_value(value, value_type)
filter_kwargs = {f"{relation}__json_ext__{field}" if relation else f"json_ext__{field}": value}
query = query.filter(**filter_kwargs)
query = query.filter(**filter_kwargs).distinct()
return query

def __process_schema_and_build_tuple(
Expand Down
23 changes: 19 additions & 4 deletions social_protection/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ def resolve_is_eligible(self, info):
return self.is_eligible


class GroupBeneficiaryGQLType(DjangoObjectType, JsonExtMixin):
uuid = graphene.String(source='uuid')
class GroupBeneficiaryFilter(django_filters.FilterSet):
is_eligible = django_filters.BooleanFilter(method='filter_is_eligible')

class Meta:
model = GroupBeneficiary
interfaces = (graphene.relay.Node,)
filter_fields = {
fields = {
"id": ["exact"],
"status": ["exact", "iexact", "startswith", "istartswith", "contains", "icontains"],
"date_valid_from": ["exact", "lt", "lte", "gt", "gte"],
Expand All @@ -115,8 +114,24 @@ class Meta:
"is_deleted": ["exact"],
"version": ["exact"],
}

def filter_is_eligible(self, queryset, name, value):
return queryset.filter(is_eligible=value)


class GroupBeneficiaryGQLType(DjangoObjectType, JsonExtMixin):
uuid = graphene.String(source='uuid')
is_eligible = graphene.Boolean()

class Meta:
model = GroupBeneficiary
interfaces = (graphene.relay.Node,)
filterset_class = GroupBeneficiaryFilter
connection_class = ExtendedConnection

def resolve_is_eligible(self, info):
return self.is_eligible


class BenefitPlanDataUploadQGLType(DjangoObjectType, JsonExtMixin):
uuid = graphene.String(source='uuid')
Expand Down
80 changes: 64 additions & 16 deletions social_protection/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,77 @@ def _annotate_is_eligible(query, eligible_uuids, eligibility_check_performed):
return gql_optimizer.query(query, info)

def resolve_group_beneficiary(self, info, **kwargs):
filters = append_validity_filter(**kwargs)
def _build_filters(info, **kwargs):
filters = append_validity_filter(**kwargs)

client_mutation_id = kwargs.get("client_mutation_id", None)
if client_mutation_id:
filters.append(Q(mutations__mutation__client_mutation_id=client_mutation_id))
client_mutation_id = kwargs.get("client_mutation_id")
if client_mutation_id:
filters.append(Q(mutations__mutation__client_mutation_id=client_mutation_id))

Query._check_permissions(
info.context.user,
SocialProtectionConfig.gql_beneficiary_search_perms
)
query = GroupBeneficiary.objects.filter(*filters)
Query._check_permissions(
info.context.user,
SocialProtectionConfig.gql_beneficiary_search_perms
)
return filters

def _apply_custom_filters(query, **kwargs):
custom_filters = kwargs.get("customFilters")
if custom_filters:
query = CustomFilterWizardStorage.build_custom_filters_queryset(
Query.module_name,
Query.object_type,
custom_filters,
query,
"group__groupindividual__individual",
)
return query

def _get_eligible_group_uuids(query, info, **kwargs):
status = kwargs.get("status")
benefit_plan_id = kwargs.get("benefit_plan__id")
default_results = (set(), False) # No eligibility check was performed

if not status or not benefit_plan_id:
return default_results

custom_filters = kwargs.get("customFilters", None)
if custom_filters:
query = CustomFilterWizardStorage.build_custom_filters_queryset(
"individual",
"GroupIndividual",
custom_filters,
benefit_plan = BenefitPlan.objects.filter(id=benefit_plan_id).first()
if not benefit_plan:
return default_results

eligibility_filters = (benefit_plan.json_ext or {}).get('advanced_criteria', {}).get(status)
if not eligibility_filters:
return default_results

query_eligible = CustomFilterWizardStorage.build_custom_filters_queryset(
Query.module_name,
Query.object_type,
eligibility_filters,
query,
"group",
"group__groupindividual__individual",
)
eligible_group_beneficiaries = gql_optimizer.query(query_eligible, info)
eligible_group_uuids = set(eligible_group_beneficiaries.values_list('uuid', flat=True))
return eligible_group_uuids, True # Eligibility check was performed

def _annotate_is_eligible(query, eligible_group_uuids, eligibility_check_performed):
return query.annotate(
is_eligible=Case(
When(uuid__in=eligible_group_uuids, then=Value(True)),
When(~Q(uuid__in=eligible_group_uuids) & Value(eligibility_check_performed), then=Value(False)),
default=Value(None),
output_field=BooleanField()
)
)

filters = _build_filters(info, **kwargs)
query = _apply_custom_filters(GroupBeneficiary.objects.filter(*filters), **kwargs)

eligible_group_uuids, eligibility_check_performed = _get_eligible_group_uuids(query, info, **kwargs)
query = _annotate_is_eligible(query, eligible_group_uuids, eligibility_check_performed)

return gql_optimizer.query(query, info)


def resolve_awaiting_beneficiary(self, info, **kwargs):
filters = append_validity_filter(**kwargs)

Expand Down
Loading

0 comments on commit fa02b08

Please sign in to comment.