diff --git a/social_protection/gql_queries.py b/social_protection/gql_queries.py index 089e89c..8d9f5f0 100644 --- a/social_protection/gql_queries.py +++ b/social_protection/gql_queries.py @@ -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 @@ -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 diff --git a/social_protection/schema.py b/social_protection/schema.py index 71a2d45..a24cbeb 100644 --- a/social_protection/schema.py +++ b/social_protection/schema.py @@ -25,7 +25,7 @@ ) from social_protection.gql_queries import ( BenefitPlanGQLType, - BeneficiaryGQLType, GroupBeneficiaryGQLType, BenefitPlanDataUploadQGLType + BeneficiaryGQLType, GroupBeneficiaryGQLType, BenefitPlanDataUploadQGLType, BenefitPlanSchemaFieldsGQLType ) from social_protection.models import ( BenefitPlan, @@ -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': [ @@ -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): @@ -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):