Skip to content
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

CM-359: allow to fetch schema fields #46

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions social_protection/gql_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import graphene
from django.contrib.auth.models import AnonymousUser
from graphene import ObjectType
from graphene_django import DjangoObjectType

from core import prefix_filterset, ExtendedConnection
Expand Down Expand Up @@ -112,3 +113,17 @@ class Meta:
**prefix_filterset("benefit_plan__", BenefitPlanGQLType._meta.filter_fields),
}
connection_class = ExtendedConnection


class BenefitPlanSchemaFieldsGQLType(ObjectType):
schema_fields = graphene.List(graphene.String)

def resolve_schema_fields(self, info, **kwargs):
schemas = self.values_list("json_ext__properties", flat=True)
field_list = set(
f'json_ext__{field}'
for schema in schemas # Iterate over each schema
if schema # Ensure the schema is not None or empty
for field in schema # Iterate over fields in the schema
)
return field_list
27 changes: 26 additions & 1 deletion social_protection/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from social_protection.gql_queries import (
BenefitPlanGQLType,
BeneficiaryGQLType, GroupBeneficiaryGQLType, BenefitPlanDataUploadQGLType
BeneficiaryGQLType, GroupBeneficiaryGQLType, BenefitPlanDataUploadQGLType, BenefitPlanSchemaFieldsGQLType
)
from social_protection.models import (
BenefitPlan,
Expand All @@ -44,6 +44,11 @@ def patch_details(beneficiary_df: pd.DataFrame):
return df_final


class BfTypeEnum(graphene.Enum):
INDIVIDUAL = BenefitPlan.BenefitPlanType.INDIVIDUAL_TYPE
GROUP = BenefitPlan.BenefitPlanType.GROUP_TYPE


class Query(ExportableQueryMixin, graphene.ObjectType):
export_patches = {
'beneficiary': [
Expand Down Expand Up @@ -112,6 +117,11 @@ class Query(ExportableQueryMixin, graphene.ObjectType):
bf_schema=graphene.String(required=True),
description="Checks that the specified Benefit Plan schema is valid"
)
benefit_plan_schema_field = graphene.Field(
BenefitPlanSchemaFieldsGQLType,
bf_type=graphene.Argument(BfTypeEnum),
description="Endpoint responsible for getting all fields from all BF schemas"
)

def resolve_bf_code_validity(self, info, **kwargs):
if not info.context.user.has_perms(SocialProtectionConfig.gql_benefit_plan_search_perms):
Expand Down Expand Up @@ -230,6 +240,21 @@ def resolve_beneficiary_data_upload_history(self, info, **kwargs):
query = BenefitPlanDataUploadRecords.objects.filter(*filters)
return gql_optimizer.query(query, info)

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

Query._check_permissions(
info.context.user,
SocialProtectionConfig.gql_schema_search_perms
)

bf_type = kwargs.get("bf_type", None)
if bf_type:
filters.append(Q(type=bf_type))

query = BenefitPlan.objects.filter(*filters)
return gql_optimizer.query(query, info)

@staticmethod
def _check_permissions(user, permission):
if type(user) is AnonymousUser or not user.id or not user.has_perms(permission):
Expand Down
Loading