Skip to content

Commit

Permalink
Merge pull request #102 from openimis/develop
Browse files Browse the repository at this point in the history
MERGING develop into release/24.04
  • Loading branch information
delcroip authored Mar 28, 2024
2 parents a8add75 + 248b41d commit c2d6109
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 12 deletions.
4 changes: 3 additions & 1 deletion policy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 9 additions & 4 deletions policy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)) \
Expand Down Expand Up @@ -361,11 +365,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__
Expand All @@ -388,7 +393,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 = {}
Expand Down
146 changes: 139 additions & 7 deletions policy/tests_gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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. """
Expand All @@ -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={
Expand All @@ -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(
Expand Down Expand Up @@ -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(
'''
Expand All @@ -130,13 +229,46 @@ 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(
'''
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':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))



Expand Down

0 comments on commit c2d6109

Please sign in to comment.