From 88e3f9492f58b44388c868584d983611015199a1 Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 8 Apr 2024 20:38:42 +0200 Subject: [PATCH] CM-856: add default responses (#6) * CM-854: allow to fetch grievance config * CM-856: add default responses --------- Co-authored-by: Jan --- grievance_social_protection/apps.py | 60 +++++++++++++++++----- grievance_social_protection/gql_queries.py | 20 +++++++- grievance_social_protection/schema.py | 9 ++++ 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/grievance_social_protection/apps.py b/grievance_social_protection/apps.py index f8de525..d7e3348 100644 --- a/grievance_social_protection/apps.py +++ b/grievance_social_protection/apps.py @@ -1,7 +1,13 @@ +import logging + from django.apps import AppConfig +logger = logging.getLogger(__name__) + MODULE_NAME = "grievance_social_protection" +DEFAULT_STRING = 'Default' + DEFAULT_CFG = { "default_validations_disabled": False, "gql_query_tickets_perms": ["123000"], @@ -14,6 +20,10 @@ "gql_mutation_delete_categorys_perms": ["123007"], "tickets_attachments_root_path": None, + "grievance_types": [DEFAULT_STRING], + "grievance_flags": [DEFAULT_STRING], + "grievance_channels": [DEFAULT_STRING], + "default_responses": {'eloo': DEFAULT_STRING} } @@ -30,19 +40,45 @@ class TicketConfig(AppConfig): gql_mutation_delete_categorys_perms = [] tickets_attachments_root_path = None - def _configure_perms(self, cfg): - TicketConfig.default_validations_disabled = cfg["default_validations_disabled"] - TicketConfig.gql_query_tickets_perms = cfg["gql_query_tickets_perms"] - TicketConfig.gql_mutation_create_tickets_perms = cfg["gql_mutation_create_tickets_perms"] - TicketConfig.gql_mutation_update_tickets_perms = cfg["gql_mutation_update_tickets_perms"] - TicketConfig.gql_mutation_delete_tickets_perms = cfg["gql_mutation_delete_tickets_perms"] - TicketConfig.gql_query_categorys_perms = cfg["gql_query_categorys_perms"] - TicketConfig.gql_mutation_create_categorys_perms = cfg["gql_mutation_create_categorys_perms"] - TicketConfig.gql_mutation_update_categorys_perms = cfg["gql_mutation_update_categorys_perms"] - TicketConfig.gql_mutation_delete_categorys_perms = cfg["gql_mutation_delete_categorys_perms"], - TicketConfig.tickets_attachments_root_path = cfg["tickets_attachments_root_path"] + grievance_types = [] + grievance_flags = [] + grievance_channels = [] + default_responses = {} def ready(self): from core.models import ModuleConfiguration cfg = ModuleConfiguration.get_or_default(MODULE_NAME, DEFAULT_CFG) - self._configure_perms(cfg) + self.__validate_grievance_responses(cfg) + self.__load_config(cfg) + + @classmethod + def __validate_grievance_responses(cls, cfg): + def get_grievance_type_options_msg(types): + types_string = ", ".join(types) + return logger.info(f'Available grievance types: %s', types_string) + + default_responses = cfg.get('default_responses', {}) + if not default_responses: + return + + grievance_types = cfg.get('grievance_types', []) + if not grievance_types: + logger.warning('Please specify grievance_types if you want to setup default responses.') + + if not isinstance(default_responses, dict): + get_grievance_type_options_msg(grievance_types) + return + + for grievance_type_key in default_responses.keys(): + if grievance_type_key not in grievance_types: + logger.warning(f'%s not in grievance_types', grievance_type_key) + get_grievance_type_options_msg(grievance_types) + + @classmethod + def __load_config(cls, cfg): + """ + Load all config fields that match current AppConfig class fields, all custom fields have to be loaded separately + """ + for field in cfg: + if hasattr(TicketConfig, field): + setattr(TicketConfig, field, cfg[field]) diff --git a/grievance_social_protection/gql_queries.py b/grievance_social_protection/gql_queries.py index 7ac03d6..337399d 100644 --- a/grievance_social_protection/gql_queries.py +++ b/grievance_social_protection/gql_queries.py @@ -1,5 +1,8 @@ import graphene +from graphene import ObjectType from graphene_django import DjangoObjectType + +from .apps import TicketConfig from .models import Ticket, Category, TicketAttachment from insuree.schema import InsureeGQLType from core import prefix_filterset, ExtendedConnection, filter_validity, ExtendedRelayConnection @@ -16,7 +19,6 @@ class Meta: filter_fields = { "id": ["exact"], "uuid": ['exact'], - "uuid": ["exact"], "category_title": ["exact", "istartswith", "icontains", "iexact"], "slug": ["exact", "istartswith", "icontains", "iexact"] } @@ -81,3 +83,19 @@ class Meta: def get_queryset(cls, queryset, info): queryset = queryset.filter(*filter_validity()) return queryset + + +class GrievanceTypeConfigurationGQLType(ObjectType): + grievance_types = graphene.List(graphene.String) + grievance_flags = graphene.List(graphene.String) + grievance_channels = graphene.List(graphene.String) + + + def resolve_grievance_types(self, info): + return TicketConfig.grievance_types + + def resolve_grievance_flags(self, info): + return TicketConfig.grievance_flags + + def resolve_grievance_channels(self, info): + return TicketConfig.grievance_channels diff --git a/grievance_social_protection/schema.py b/grievance_social_protection/schema.py index 09d2bfa..b0fec88 100644 --- a/grievance_social_protection/schema.py +++ b/grievance_social_protection/schema.py @@ -39,6 +39,8 @@ class Query(graphene.ObjectType): orderBy=graphene.List(of_type=graphene.String), ) + grievance_config = graphene.Field(GrievanceTypeConfigurationGQLType) + def resolve_ticket_details(self, info, **kwargs): # if not info.context.user.has_perms(ServiceProviderConfig.gql_query_service_provider_perms): # raise PermissionDenied(_("unauthorized")) @@ -99,6 +101,13 @@ def resolve_claim_attachments(self, info, **kwargs): raise PermissionDenied(_("unauthorized")) + def resolve_grievance_config(self, info, **kwargs): + user = info.context.user + if not user.is_imis_admin: + raise PermissionDenied(_("unauthorized")) + return GrievanceTypeConfigurationGQLType() + + class Mutation(graphene.ObjectType): create_Ticket = CreateTicketMutation.Field() update_Ticket = UpdateTicketMutation.Field()