Skip to content

Commit

Permalink
Finish release/2022-04
Browse files Browse the repository at this point in the history
Release/2022 04
  • Loading branch information
dragos-dobre authored Oct 4, 2022
2 parents eb9a2bb + bdd1c31 commit b798293
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 88 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/openmis-module-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
python-version: 3.8
- name: install linux packages
run: |
wget https://raw.githubusercontent.com/openimis/database_ms_sqlserver/main/Empty%20databases/openIMIS_ONLINE.sql -O openIMIS_ONLINE.sql
wget https://raw.githubusercontent.com/openimis/database_ms_sqlserver/main/Demo%20database/openIMIS_demo_ONLINE.sql -O openIMIS_demo_ONLINE.sql
git clone --depth 1 --branch develop https://github.com/openimis/database_ms_sqlserver.git ./sql
cd sql/ && bash concatenate_files.sh && cd ..
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt-get update
Expand Down Expand Up @@ -67,8 +67,7 @@ jobs:
run: |
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P $SA_PASSWORD -Q 'DROP DATABASE IF EXISTS imis'
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P $SA_PASSWORD -Q 'CREATE DATABASE imis'
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P $SA_PASSWORD -d imis -i openIMIS_ONLINE.sql | grep . | uniq -c
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P $SA_PASSWORD -d imis -i openIMIS_demo_ONLINE.sql | grep . | uniq -c
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P $SA_PASSWORD -d imis -i sql/output/fullDemoDatabase.sql | grep . | uniq -c
env:
SA_PASSWORD: GitHub999
ACCEPT_EULA: Y
Expand Down Expand Up @@ -97,4 +96,3 @@ jobs:
DB_PASSWORD: GitHub999
#DEV_SERVER: true
SITE_ROOT: api
REMOTE_USER_AUTHENTICATION: True
66 changes: 4 additions & 62 deletions policy/gql_mutations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import graphene
from policy.services import update_insuree_policies
from policy.services import update_insuree_policies, PolicyService

from .apps import PolicyConfig
from core.schema import OpenIMISMutation
Expand All @@ -24,35 +24,6 @@ class PolicyInputType(OpenIMISMutation.Input):
officer_id = graphene.Int(required=True)


def reset_policy_before_update(policy):
policy.enroll_date = None
policy.start_date = None
policy.expiry_date = None
policy.value = None
policy.product_id = None
policy.family_id = None
policy.officer_id = None


def update_or_create_policy(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!
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]
else:
policy = Policy.objects.create(**data)
policy.save()
update_insuree_policies(policy, user.id_for_audit)


class CreateRenewOrUpdatePolicyMutation(OpenIMISMutation):
@classmethod
def do_mutate(cls, perms, user, **data):
Expand All @@ -67,7 +38,7 @@ def do_mutate(cls, perms, user, **data):
data['audit_user_id'] = user.id_for_audit
from core.utils import TimeUtils
data['validity_from'] = TimeUtils.now()
update_or_create_policy(data, user)
PolicyService(user).update_or_create(data, user)
return None


Expand Down Expand Up @@ -129,22 +100,6 @@ def async_mutate(cls, user, **data):
'detail': str(exc)}]


def set_policy_suspended(user, policy):
try:
policy.save_history()
policy.status = Policy.STATUS_SUSPENDED
policy.audit_user_id = user.id_for_audit
policy.save()
return []
except Exception as exc:
return {
'title': policy.uuid,
'list': [{
'message': _("policy.mutation.failed_to_suspend_policy") % {'uuid': policy.uuid},
'detail': policy.uuid}]
}


class SuspendPoliciesMutation(OpenIMISMutation):
_mutation_module = "policy"
_mutation_class = "SuspendPolicyMutation"
Expand All @@ -170,7 +125,7 @@ def async_mutate(cls, user, **data):
"policy.mutation.id_does_not_exist") % {'id': policy_uuid}}]
}
continue
errors += set_policy_suspended(user, policy)
errors += PolicyService(user).set_suspended(user, policy)
if len(errors) == 1:
errors = errors[0]['list']
return errors
Expand All @@ -180,19 +135,6 @@ def async_mutate(cls, user, **data):
'detail': str(exc)}]


def set_policy_deleted(policy):
try:
policy.delete_history()
return []
except Exception as exc:
return {
'title': policy.uuid,
'list': [{
'message': _("policy.mutation.failed_to_change_status_of_policy") % {'policy': str(policy)},
'detail': policy.uuid}]
}


class DeletePoliciesMutation(OpenIMISMutation):
_mutation_module = "policy"
_mutation_class = "DeletePoliciesMutation"
Expand All @@ -216,7 +158,7 @@ def async_mutate(cls, user, **data):
"policy.validation.id_does_not_exist") % {'id': policy_uuid}}]
}
continue
errors += set_policy_deleted(policy)
errors += PolicyService(user).set_deleted(policy)
if len(errors) == 1:
errors = errors[0]['list']
return errors
12 changes: 11 additions & 1 deletion policy/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@


class PolicyGQLType(DjangoObjectType):
sum_premiums = graphene.Float(source='sum_premiums')
sum_premiums = graphene.Float(source="sum_premiums")

def resolve_family(self, info):
if "family_loader" in info.context.dataloaders and self.family_id:
return info.context.dataloaders["family_loader"].load(self.family_id)
return self.family

def resolve_product(self, info):
if "product_loader" in info.context.dataloaders and self.product_id:
return info.context.dataloaders["product_loader"].load(self.product_id)
return self.product

class Meta:
model = Policy
Expand Down
2 changes: 1 addition & 1 deletion policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def is_new(self):
return not self.stage or self.stage == Policy.STAGE_NEW

def can_add_insuree(self):
return self.family.members.filter(validity_to__isnull=True).count() < self.product.member_count
return self.family.members.filter(validity_to__isnull=True).count() < self.product.max_members

class Meta:
managed = False
Expand Down
78 changes: 71 additions & 7 deletions policy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from django.db.models import Sum, F
from django.db.models.functions import Coalesce
from django.template import Template, Context
from django.utils.translation import gettext as _
from graphene.utils.str_converters import to_snake_case
from core.signals import register_service_signal
from insuree.models import Insuree, Family, InsureePolicy
from insuree.services import create_insuree_renewal_detail
from medical.models import Service, Item
Expand All @@ -23,6 +25,68 @@
logger = logging.getLogger(__name__)


def reset_policy_before_update(policy):
policy.enroll_date = None
policy.start_date = None
policy.expiry_date = None
policy.value = None
policy.product_id = None
policy.family_id = None
policy.officer_id = None


class PolicyService:
def __init__(self, user):
self.user = 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!
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]
else:
policy = Policy.objects.create(**data)
policy.save()
update_insuree_policies(policy, user.id_for_audit)
return policy

def set_suspended(self, user, policy):
try:
policy.save_history()
policy.status = Policy.STATUS_SUSPENDED
policy.audit_user_id = user.id_for_audit
policy.save()
return []
except Exception as exc:
return {
'title': policy.uuid,
'list': [{
'message': _("policy.mutation.failed_to_suspend_policy") % {'uuid': policy.uuid},
'detail': policy.uuid}]
}

def set_deleted(self, policy):
try:
policy.delete_history()
return []
except Exception as exc:
return {
'title': policy.uuid,
'list': [{
'message': _("policy.mutation.failed_to_change_status_of_policy") % {'policy': str(policy)},
'detail': policy.uuid}]
}


@core.comparable
class ByInsureeRequest(object):

Expand Down Expand Up @@ -464,11 +528,11 @@ def request(self, req, response):

if req.service_code:
if insuree.is_adult():
waiting_period_field = "policy__product__products__waiting_period_adult"
limit_field = "policy__product__products__limit_no_adult"
waiting_period_field = "policy__product__services__waiting_period_adult"
limit_field = "policy__product__services__limit_no_adult"
else:
waiting_period_field = "policy__product__products__waiting_period_child"
limit_field = "policy__product__products__limit_no_child"
waiting_period_field = "policy__product__services__waiting_period_child"
limit_field = "policy__product__services__limit_no_child"

# TODO validity is checked but should be optional in get_queryset
service = Service.get_queryset(None, self.user).get(code__iexact=req.service_code)
Expand All @@ -477,8 +541,8 @@ def request(self, req, response):
queryset_svc = InsureePolicy.objects\
.filter(validity_to__isnull=True)\
.filter(policy__validity_to__isnull=True) \
.filter(policy__product__products__validity_to__isnull=True,
policy__product__products__service_id=service.id) \
.filter(policy__product__services__validity_to__isnull=True,
policy__product__services__service_id=service.id) \
.filter(policy__status=Policy.STATUS_ACTIVE) \
.filter(insuree=insuree) \
.filter(Q(insuree__claim__validity_to__isnull=True)
Expand All @@ -492,7 +556,7 @@ def request(self, req, response):
waiting_period=F(waiting_period_field),
limit_no=F(limit_field))\
.annotate(min_date=MonthsAdd(Coalesce(F(waiting_period_field), 0), "effective_date"))\
.annotate(services_count=Count("policy__product__products__service_id"))\
.annotate(services_count=Count("policy__product__services__service_id"))\
.annotate(services_left=F("limit_no") - F("services_count"))

min_date_qs = queryset_svc.aggregate(
Expand Down
26 changes: 24 additions & 2 deletions policy/signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
from core.signals import Signal

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

_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)
10 changes: 5 additions & 5 deletions policy/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_new_policy_basis(self):
})

product = create_test_product("SIMPLE", custom_props={
"member_count": 5,
"max_members": 5,
"administration_period": 0,
"lump_sum": 0,
"premium_adult": 300,
Expand Down Expand Up @@ -64,11 +64,11 @@ def test_new_policy_lump_sum_and_cycles(self):
})

product = create_test_product("SIMPLE", custom_props={
"member_count": 5,
"max_members": 5,
"administration_period": 0,
"lump_sum": 200,
"threshold": 2,
"grace_period": 1,
"grace_period_enrolment": 1,
"start_cycle_1": "01-01",
"start_cycle_2": "01-06",
"premium_adult": 300,
Expand Down Expand Up @@ -114,11 +114,11 @@ def test_new_policy_admin_period_max_members_insurance_period(self):
})

product = create_test_product("SIMPLE", custom_props={
"member_count": 2,
"max_members": 2,
"administration_period": 1,
"lump_sum": 200,
"threshold": 1,
"grace_period": 1,
"grace_period_enrolment": 1,
"start_cycle_1": "01-01",
"start_cycle_2": "01-06",
"premium_adult": 300,
Expand Down
10 changes: 5 additions & 5 deletions policy/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def set_start_date(policy):
return

grace = 0
if policy.stage == Policy.STAGE_NEW and product.grace_period:
grace = product.grace_period
if policy.stage == Policy.STAGE_NEW and product.grace_period_enrolment:
grace = product.grace_period_enrolment
elif policy.stage == Policy.STAGE_RENEWED and product.grace_period_renewal:
grace = product.grace_period_renewal

Expand Down Expand Up @@ -68,8 +68,8 @@ def family_counts(product, family):
extra_children = 0
total = 0
# sad, but can't get the limit inside the prefetch
# product.member_count is NOT NULL (but can be 0)
for member in family.members.all()[:product.member_count]:
# product.max_members is NOT NULL (but can be 0)
for member in family.members.all()[:product.max_members]:
total += 1
age = member.age()
if age >= CoreConfig.age_of_majority and member.relationship_id != 7:
Expand Down Expand Up @@ -174,7 +174,7 @@ def set_value(policy, family, prev_policy):

def policy_values(policy, family, prev_policy):
members = family.members.filter(validity_to__isnull=True).count()
max_members = policy.product.member_count
max_members = policy.product.max_members
above_max = max(0, members - max_members)
warnings = []
if above_max:
Expand Down

0 comments on commit b798293

Please sign in to comment.