Skip to content

Commit

Permalink
Merge pull request #34 from openimis/develop
Browse files Browse the repository at this point in the history
MERGING RELEASE branches
  • Loading branch information
delcroip authored Nov 10, 2022
2 parents b798293 + 24d1364 commit f1d7428
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
47 changes: 31 additions & 16 deletions policy/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from dataclasses import dataclass
from datetime import datetime as py_datetime
from datetime import datetime as py_datetime, date as py_date

import core
from claim.models import ClaimService, Claim, ClaimItem
Expand Down Expand Up @@ -41,24 +41,39 @@ def __init__(self, user):

@register_service_signal('policy_service.create_or_update')
def update_or_create(self, data, user):
if "client_mutation_id" in data:
data.pop('client_mutation_id')
if "client_mutation_label" in data:
data.pop('client_mutation_label')
policy_uuid = data.pop('policy_uuid') if 'policy_uuid' in data else None
# update_or_create(uuid=policy_uuid, ...)
# doesn't work because of explicit attempt to set null to uuid!
policy_uuid = data.get('uuid', None)
if policy_uuid:
policy = Policy.objects.get(uuid=policy_uuid)
policy.save_history()
reset_policy_before_update(policy)
[setattr(policy, key, data[key]) for key in data]
return self.update_policy(data, user)
else:
policy = Policy.objects.create(**data)
return self.create_policy(data, user)

@register_service_signal('policy_service.update')
def update_policy(self, data, user):
data = self._clean_mutation_info(data)
policy_uuid = data.pop('uuid') if 'uuid' in data else None
policy = Policy.objects.get(uuid=policy_uuid)
policy.save_history()
reset_policy_before_update(policy)
[setattr(policy, key, data[key]) for key in data]
policy.save()
update_insuree_policies(policy, user.id_for_audit)
return policy

@register_service_signal('policy_service.create')
def create_policy(self, data, user):
data = self._clean_mutation_info(data)
policy = Policy.objects.create(**data)
policy.save()
update_insuree_policies(policy, user.id_for_audit)
return policy

def _clean_mutation_info(self, data):
if "client_mutation_id" in data:
data.pop('client_mutation_id')
if "client_mutation_label" in data:
data.pop('client_mutation_label')
return data

def set_suspended(self, user, policy):
try:
policy.save_history()
Expand Down Expand Up @@ -206,8 +221,8 @@ def _to_item(row):
enroll_date=row.enroll_date,
effective_date=row.effective_date,
expiry_date=row.expiry_date,
officer_code=row.officer.code,
officer_name=row.officer.name(),
officer_code=row.officer.code if row.officer else None,
officer_name=row.officer.name() if row.officer else None,
status=row.status,
ded=row.total_ded_g,
ded_in_patient=row.total_ded_ip,
Expand Down Expand Up @@ -241,7 +256,7 @@ def build_query(self, req):
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_datetime.max)) \
res = res.annotate(not_null_expiry_date=Coalesce('expiry_date', py_date.max)) \
.annotate(not_null_validity_to=Coalesce('validity_to', py_datetime.max)) \
.order_by('product__code', 'status', '-not_null_expiry_date', '-not_null_validity_to', '-validity_from')
return res
Expand Down
25 changes: 1 addition & 24 deletions policy/signals.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
from calculation.services import run_calculation_rules
from core.models import User
from core.service_signals import ServiceSignalBindType
from core.signals import Signal, bind_service_signal
from policy.models import Policy
from core.signals import Signal

_check_formal_sector_for_policy_signal_params = ["user", "policy_id"]
signal_check_formal_sector_for_policy = Signal(providing_args=_check_formal_sector_for_policy_signal_params)


def bind_service_signals():
bind_service_signal(
'policy_service.create_or_update',
on_policy_create_or_update,
bind_type=ServiceSignalBindType.AFTER
)


def on_policy_create_or_update(**kwargs):
policy = kwargs.get('result', None)
if policy:
if policy.status in [Policy.STATUS_IDLE, Policy.STATUS_ACTIVE]:
user = User.objects.filter(i_user__id=policy.audit_user_id).first()
# run calcrule for Invoice if there is valid rule
run_calculation_rules(policy, "PolicyCreatedInvoice", user)
# run calcrule for creating Bill if there is valid rule
run_calculation_rules(policy, "PolicyCreated", user)
20 changes: 10 additions & 10 deletions policy/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

class EligibilityServiceTestCase(TestCase):
def setUp(self) -> None:
super(EligibilityServiceTestCase, self).setUp()
self.user = mock.Mock(is_anonymous=False)
self.user.has_perms = mock.MagicMock(return_value=True)

def test_eligibility_request_permission_denied(self):
with mock.patch("django.db.backends.utils.CursorWrapper") as mock_cursor:
mock_cursor.return_value.__enter__.return_value.description = None
mock_cursor.return_value.description = None
mock_user = mock.Mock(is_anonymous=False)
mock_user.has_perms = mock.MagicMock(return_value=False)
req = EligibilityRequest(chf_id="a")
Expand All @@ -43,11 +44,11 @@ def test_eligibility_request_all_good(self):
True,
True,
],
][::-1]

mock_cursor.return_value.__enter__.return_value.fetchone = (
lambda: return_values.pop()
)
]
# required for all modules tests
mock_cursor.return_value.fetchone.side_effect = return_values
# required for policy module tests
mock_cursor.return_value.__enter__.return_value.fetchone.side_effect = return_values
mock_user = mock.Mock(is_anonymous=False)
mock_user.has_perm = mock.MagicMock(return_value=True)
req = EligibilityRequest(chf_id="a")
Expand Down Expand Up @@ -303,16 +304,14 @@ class RenewalsTestCase(TestCase):
item_1 = None

def setUp(self) -> None:
super(RenewalsTestCase, self).setUp()
self.i_user = InteractiveUser(
login_name="test_batch_run", audit_user_id=978911, id=97891
)
self.user = User(i_user=self.i_user)

self.item_1 = create_test_item("D")

def tearDown(self) -> None:
self.item_1.delete()

def test_insert_renewals(self):
# Given
from core import datetime, datetimedelta
Expand Down Expand Up @@ -466,7 +465,8 @@ def test_insert_renewal_details(self):

insuree_newpic = create_test_insuree(
custom_props={"photo_date": datetime.datetime.now() - datetimedelta(days=30)})
insuree_oldpic = create_test_insuree(custom_props={"photo_date": "2010-01-01", "chf_id": "CHFMARK"}) # 5 years by default
insuree_oldpic = create_test_insuree(
custom_props={"photo_date": "2010-01-01", "chf_id": "CHFMARK"}) # 5 years by default
product = create_test_product("VISIT")
officer = create_test_officer(custom_props={"phone": "+32444444444", "phone_communication": True})
photo_newpic = create_test_photo(insuree_newpic.id, officer.id)
Expand Down

0 comments on commit f1d7428

Please sign in to comment.