From 1960e541d504ca5a588d3157376a4d29e6553401 Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Tue, 26 Mar 2024 10:22:42 +0100 Subject: [PATCH 1/4] Update schema.py --- policy/schema.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/policy/schema.py b/policy/schema.py index 0055963..5d915c9 100644 --- a/policy/schema.py +++ b/policy/schema.py @@ -67,6 +67,7 @@ class Query(graphene.ObjectType): active_or_last_expired_only=graphene.Boolean(), show_history=graphene.Boolean(), order_by=graphene.String(), + target_date=graphene.Date(), ) # TODO: refactoring # Eligibility is calculated for a Policy... which is bound to a Family (not an Insuree) @@ -218,7 +219,8 @@ def resolve_policies_by_family(self, info, **kwargs): active_or_last_expired_only=kwargs.get( 'active_or_last_expired_only', False), show_history=kwargs.get('show_history', False), - order_by=kwargs.get('order_by', None) + order_by=kwargs.get('order_by', None), + target_date=kwargs.get('target_date', None) ) res = ByFamilyService(user=info.context.user).request(req) return [Query._to_policy_by_family_or_insuree_item(x) for x in res.items] From f6d18b0014e50c92e1f7281935415800f1130512 Mon Sep 17 00:00:00 2001 From: delcroip Date: Tue, 26 Mar 2024 11:15:16 +0100 Subject: [PATCH 2/4] test passing --- policy/services.py | 5 +++-- policy/tests_gql.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/policy/services.py b/policy/services.py index c9eccf0..fdfcd81 100644 --- a/policy/services.py +++ b/policy/services.py @@ -361,11 +361,12 @@ def request(self, by_insuree_request): @core.comparable class ByFamilyRequest(object): - def __init__(self, family_uuid, active_or_last_expired_only=False, show_history=False, order_by=None): + def __init__(self, family_uuid, active_or_last_expired_only=False, show_history=False, order_by=None, target_date=None): self.family_uuid = family_uuid self.active_or_last_expired_only = active_or_last_expired_only self.show_history = show_history self.order_by = order_by + self.target_date = target_date def __eq__(self, other): return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ @@ -388,7 +389,7 @@ def __init__(self, user): def request(self, by_family_request): res = self.build_query(by_family_request) - res = res.filter(family_uuid=by_family_request.family_uuid) + res = res.filter(family__uuid=by_family_request.family_uuid) # .distinct('product__code') >> DISTINCT ON fields not supported by MS-SQL if by_family_request.active_or_last_expired_only: products = {} diff --git a/policy/tests_gql.py b/policy/tests_gql.py index 36c137a..8adc7fb 100644 --- a/policy/tests_gql.py +++ b/policy/tests_gql.py @@ -137,6 +137,33 @@ def test_query_with_variables(self): # This validates the status code and if you get errors self.assertResponseNoErrors(response) + + def test_family_query_with_variables(self): + response = self.query( + ''' + query policiesByFamily($familyUuid: String!, $targetDate: Date! ) { + policiesByFamily(orderBy: "expiryDate",activeOrLastExpiredOnly: true,familyUuid:$familyUuid ,targetDate: $targetDate,first: 5) + { + totalCount + pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor} + edges + { + node + { + policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient + } + } + } + } + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + variables={'familyUuid': str(self.insuree.family.uuid), 'targetDate':"2019-01-01"} + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) From e279d8dfbc20b33daf103c47600b402716e088f2 Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Wed, 27 Mar 2024 09:05:23 +0100 Subject: [PATCH 3/4] Feature/allow target date (#100) * Update schema.py * test passing --- policy/schema.py | 4 +++- policy/services.py | 5 +++-- policy/tests_gql.py | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/policy/schema.py b/policy/schema.py index 0055963..5d915c9 100644 --- a/policy/schema.py +++ b/policy/schema.py @@ -67,6 +67,7 @@ class Query(graphene.ObjectType): active_or_last_expired_only=graphene.Boolean(), show_history=graphene.Boolean(), order_by=graphene.String(), + target_date=graphene.Date(), ) # TODO: refactoring # Eligibility is calculated for a Policy... which is bound to a Family (not an Insuree) @@ -218,7 +219,8 @@ def resolve_policies_by_family(self, info, **kwargs): active_or_last_expired_only=kwargs.get( 'active_or_last_expired_only', False), show_history=kwargs.get('show_history', False), - order_by=kwargs.get('order_by', None) + order_by=kwargs.get('order_by', None), + target_date=kwargs.get('target_date', None) ) res = ByFamilyService(user=info.context.user).request(req) return [Query._to_policy_by_family_or_insuree_item(x) for x in res.items] diff --git a/policy/services.py b/policy/services.py index c9eccf0..fdfcd81 100644 --- a/policy/services.py +++ b/policy/services.py @@ -361,11 +361,12 @@ def request(self, by_insuree_request): @core.comparable class ByFamilyRequest(object): - def __init__(self, family_uuid, active_or_last_expired_only=False, show_history=False, order_by=None): + def __init__(self, family_uuid, active_or_last_expired_only=False, show_history=False, order_by=None, target_date=None): self.family_uuid = family_uuid self.active_or_last_expired_only = active_or_last_expired_only self.show_history = show_history self.order_by = order_by + self.target_date = target_date def __eq__(self, other): return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ @@ -388,7 +389,7 @@ def __init__(self, user): def request(self, by_family_request): res = self.build_query(by_family_request) - res = res.filter(family_uuid=by_family_request.family_uuid) + res = res.filter(family__uuid=by_family_request.family_uuid) # .distinct('product__code') >> DISTINCT ON fields not supported by MS-SQL if by_family_request.active_or_last_expired_only: products = {} diff --git a/policy/tests_gql.py b/policy/tests_gql.py index 36c137a..8adc7fb 100644 --- a/policy/tests_gql.py +++ b/policy/tests_gql.py @@ -137,6 +137,33 @@ def test_query_with_variables(self): # This validates the status code and if you get errors self.assertResponseNoErrors(response) + + def test_family_query_with_variables(self): + response = self.query( + ''' + query policiesByFamily($familyUuid: String!, $targetDate: Date! ) { + policiesByFamily(orderBy: "expiryDate",activeOrLastExpiredOnly: true,familyUuid:$familyUuid ,targetDate: $targetDate,first: 5) + { + totalCount + pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor} + edges + { + node + { + policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient + } + } + } + } + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + variables={'familyUuid': str(self.insuree.family.uuid), 'targetDate':"2019-01-01"} + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) From 62bc50594cb19ec4d2bb06b1d3d7a50b91e8e776 Mon Sep 17 00:00:00 2001 From: delcroip Date: Wed, 27 Mar 2024 15:25:47 +0100 Subject: [PATCH 4/4] fixing query, filtering on uuid --- policy/services.py | 8 ++- policy/tests_gql.py | 121 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/policy/services.py b/policy/services.py index fdfcd81..fcacc26 100644 --- a/policy/services.py +++ b/policy/services.py @@ -316,10 +316,14 @@ def build_query(self, req): .annotate(total_rem_delivery=Sum('claim_ded_rems__rem_delivery')) \ .annotate(total_rem_hospitalization=Sum('claim_ded_rems__rem_hospitalization')) \ .annotate(total_rem_antenatal=Sum('claim_ded_rems__rem_antenatal')) - res.query.group_by = ['id'] + if hasattr(req, 'chf_id'): + res= res.filter(insuree_policies__insuree__chf_id = req.chf_id) if not req.show_history: - res = res.filter(*core.filter_validity(validity = req.target_date if req.target_date else None)) + if req.target_date: + res = res.filter(*core.filter_validity(), expiry_date__gt = req.target_date, effective_date__lte = req.target_date) + else: + res = res.filter(*core.filter_validity()) if req.active_or_last_expired_only: # sort on status, so that any active policy (status = 2) pops up... res = res.annotate(not_null_expiry_date=Coalesce('expiry_date', py_date.max)) \ diff --git a/policy/tests_gql.py b/policy/tests_gql.py index 8adc7fb..048c14e 100644 --- a/policy/tests_gql.py +++ b/policy/tests_gql.py @@ -14,7 +14,7 @@ create_test_service ) from insuree.test_helpers import create_test_insuree -from policy.test_helpers import create_test_policy +from policy.test_helpers import create_test_policy, dts from contribution.test_helpers import create_test_premium from product.models import ProductItemOrService from product.test_helpers import ( @@ -26,7 +26,7 @@ create_test_health_facility, create_test_village ) - +from uuid import UUID @dataclass class DummyContext: """ Just because we need a context to generate. """ @@ -51,9 +51,10 @@ def setUpClass(cls): cls.test_district = cls.test_village.parent.parent # Given cls.insuree = create_test_insuree(custom_props={'current_village':cls.test_village}) + cls.service = create_test_service("A", custom_props={"name": "test_simple_batch"}) cls.item = create_test_item("A", custom_props={"name": "test_simple_batch"}) - + cls.product = create_test_product( "BCUL0001", custom_props={ @@ -77,9 +78,20 @@ def setUpClass(cls): cls.premium = create_test_premium( policy_id=cls.policy.id, custom_props={} ) - - - + cls.policy_past = create_test_policy(cls.product, cls.insuree, link=True, custom_props={ + "enroll_date": dts("2010-01-01"), + "start_date": dts("2010-01-01"), + "validity_from": dts("2010-01-01"), + "effective_date": dts("2010-01-01"), + "expiry_date": dts("2011-01-01"), + }) + cls.premium_past = create_test_premium( + policy_id=cls.policy_past.id, custom_props={'pay_date':dts('2010-01-01')} + ) + cls.not_insuree = create_test_insuree(with_family= False, custom_props={'family':cls.insuree.family}) + + + def test_insuree_policy_query(self): response = self.query( @@ -109,8 +121,95 @@ def test_insuree_policy_query(self): # Add some more asserts if you like ... + def test_query_not_insured_family_member(self): + response = self.query( + ''' + + query policiesByInsuree($chfid: String!) { + policiesByInsuree(chfId:$chfid) + { + totalCount + pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor} + edges + { + node + { + policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient + } + } + } + } + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + variables={'chfid': self.not_insuree.chf_id, 'targetDate':self.policy.effective_date.strftime("%Y-%m-%d")} + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) + self.assertEqual(content['data']['policiesByInsuree']['totalCount'], 0) def test_query_with_variables(self): + response = self.query( + ''' + + query policiesByInsuree($chfid: String!) { + policiesByInsuree(chfId:$chfid) + { + totalCount + pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor} + edges + { + node + { + policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient + } + } + } + } + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + variables={'chfid': self.insuree.chf_id, 'targetDate':self.policy.effective_date.strftime("%Y-%m-%d")} + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) + self.assertEqual(content['data']['policiesByInsuree']['totalCount'], 2) + + def test_query_with_variables_2(self): + response = self.query( + ''' + + query policiesByInsuree($chfid: String!, $activeOrLastExpiredOnly: Boolean!) { + policiesByInsuree(chfId:$chfid, activeOrLastExpiredOnly:$activeOrLastExpiredOnly) + { + totalCount + pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor} + edges + { + node + { + policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient + } + } + } + } + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + variables={'chfid': self.insuree.chf_id, 'activeOrLastExpiredOnly':True} + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) + self.assertEqual(content['data']['policiesByInsuree']['totalCount'], 1) + self.assertEqual(UUID(content['data']['policiesByInsuree']['edges'][0]['node']['policyUuid']), UUID(self.policy.uuid)) + + def test_query_with_variables_3(self): response = self.query( ''' @@ -130,13 +229,15 @@ def test_query_with_variables(self): } ''', headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, - variables={'chfid': "070707070", 'targetDate':"2019-01-01"} + variables={'chfid': self.insuree.chf_id, 'targetDate':self.policy.effective_date.strftime("%Y-%m-%d")} ) content = json.loads(response.content) # This validates the status code and if you get errors self.assertResponseNoErrors(response) + self.assertEqual(content['data']['policiesByInsuree']['totalCount'], 1) + self.assertEqual(UUID(content['data']['policiesByInsuree']['edges'][0]['node']['policyUuid']), UUID(self.policy.uuid)) def test_family_query_with_variables(self): response = self.query( @@ -157,13 +258,17 @@ def test_family_query_with_variables(self): } ''', headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, - variables={'familyUuid': str(self.insuree.family.uuid), 'targetDate':"2019-01-01"} + variables={'familyUuid': str(self.insuree.family.uuid), 'targetDate':self.policy.effective_date.strftime("%Y-%m-%d")} ) content = json.loads(response.content) + + # This validates the status code and if you get errors self.assertResponseNoErrors(response) + self.assertEqual(content['data']['policiesByFamily']['totalCount'], 1) + self.assertEqual(UUID(content['data']['policiesByFamily']['edges'][0]['node']['policyUuid']), UUID(self.policy.uuid))