diff --git a/policy/services.py b/policy/services.py index 519b780..da9e7f5 100644 --- a/policy/services.py +++ b/policy/services.py @@ -6,7 +6,12 @@ import core from claim.models import Claim, ClaimItem from django import dispatch -from django.core.exceptions import PermissionDenied, ValidationError +from django.core.exceptions import ( + PermissionDenied, + ValidationError, + ObjectDoesNotExist, + MultipleObjectsReturned, +) from django.db import connection from django.db.models import Q, Count, Min, Max, Sum, F from django.db.models.functions import Coalesce @@ -773,11 +778,19 @@ def get_eligibility(self, insuree, item_or_service, model, req, now): if item_or_service == "item": item_or_service_code = req.item_code - # TODO validity is checked but should be optional in get_queryset - item_or_service_obj = model.get_queryset(None, self.user).get( - code__iexact=item_or_service_code, *core.filter_validity() - ) - + # try to get the service/item with the exact code + try: + item_or_service_obj = model.get_queryset(None, self.user).get( + code=item_or_service_code + ) + except model.DoesNotExist: + item_or_service_obj = model.get_queryset(None, self.user).filter( + code__iexact=item_or_service_code + ).first() + if item_or_service_obj is None: + raise model.DoesNotExist(f"{model.__name__} has no match for code {item_or_service_code}") + except MultipleObjectsReturned: + raise MultipleObjectsReturned(f"{model.__name__} has multiple match for code {item_or_service_code}") # Beware that MonthAdd() is in Gregorian calendar, not Nepalese or anything else queryset_item_or_service = ( InsureePolicy.objects.filter( diff --git a/policy/tests/tests_gql.py b/policy/tests/tests_gql.py index 1aeaa22..4304515 100644 --- a/policy/tests/tests_gql.py +++ b/policy/tests/tests_gql.py @@ -55,8 +55,11 @@ def setUpClass(cls): cls.service = create_test_service( "A", custom_props={"name": "test_simple_batch"} ) + cls.service_2 = create_test_service( + "a", custom_props={"name": "test_simple_batch"} + ) cls.item = create_test_item("A", custom_props={"name": "test_simple_batch"}) - + cls.item_2 = create_test_item("a", custom_props={"name": "test_simple_batch"}) cls.product = create_test_product( "BCUL0001", custom_props={ @@ -299,7 +302,7 @@ def test_family_query_with_variables(self): UUID(self.policy.uuid), ) - def test_insuree_policy_query(self): + def test_insuree_policy_service_query(self): response = self.query( f""" @@ -317,6 +320,7 @@ def test_insuree_policy_query(self): # This validates the status code and if you get errors self.assertResponseNoErrors(response) + def test_insuree_policy_item_query(self): # Add some more asserts if you like response = self.query( @@ -336,8 +340,23 @@ def test_insuree_policy_query(self): # This validates the status code and if you get errors self.assertResponseNoErrors(response) + def test_insuree_policy_wrong_service_query(self): + response = self.query( + f""" +{{ + policyItemEligibilityByInsuree(chfId:"{self.insuree.chf_id}",itemCode:"IDONOTEXIST") + {{ + minDateItem,itemLeft,isItemOk + }} +}} + """, + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + ) + + # This validates the status code and if you get errors + self.assertResponseHasErrors(response) + # Add some more asserts if you like - ... def test_mutation_simple(self): muuid = "203327cd-501e-41e1-a026-ed742e360081"