diff --git a/policy/services.py b/policy/services.py index 1395985..fbaf9e8 100644 --- a/policy/services.py +++ b/policy/services.py @@ -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 @@ -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() @@ -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, @@ -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 diff --git a/policy/signals.py b/policy/signals.py index 3b281ec..89955ac 100644 --- a/policy/signals.py +++ b/policy/signals.py @@ -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) diff --git a/policy/test_services.py b/policy/test_services.py index 16b0946..f3e86d4 100644 --- a/policy/test_services.py +++ b/policy/test_services.py @@ -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") @@ -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") @@ -303,6 +304,7 @@ 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 ) @@ -310,9 +312,6 @@ def setUp(self) -> None: 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 @@ -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)