Skip to content

Commit

Permalink
Merge pull request #116 from openimis/merge-d-24.10
Browse files Browse the repository at this point in the history
Get dev changes
  • Loading branch information
delcroip authored Oct 18, 2024
2 parents 1158752 + 4f18ebf commit 73bc8e6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
19 changes: 17 additions & 2 deletions policy/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import graphene
from django.db import transaction

from policy.services import PolicyService
from policy.services import PolicyService, PolicyRenewalService

from .apps import PolicyConfig
from core.schema import OpenIMISMutation
from .models import Policy, PolicyMutation
from .models import Policy, PolicyMutation, PolicyRenewal
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError, PermissionDenied
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -50,9 +50,24 @@ def do_mutate(cls, perms, user, **data):

data["validity_from"] = TimeUtils.now()
policy = PolicyService(user).update_or_create(data, user)
logger.info(f"After policy create_or_update: {policy.uuid}")
if data["stage"] == Policy.STAGE_RENEWED:
logger.info("Deleting the optional PolicyRenewals after renewing")
previous_policy = (Policy.objects.filter(validity_to__isnull=True,
family_id=data["family_id"],
product_id=data["product_id"],
status__in=[Policy.STATUS_EXPIRED, Policy.STATUS_ACTIVE])
.order_by("-id")
.first())
if not previous_policy:
logger.error("Can't find the policy that was renewed - not deleting the PolicyRenewals")
policy_renewals = PolicyRenewal.objects.filter(policy=previous_policy, validity_to__isnull=True)
logger.info(f"Total PolicyRenewals found: {policy_renewals.count()}")
[PolicyRenewalService(user).delete(policy_renewal) for policy_renewal in policy_renewals]
PolicyMutation.object_mutated(
user, client_mutation_id=client_mutation_id, policy=policy
)

return None


Expand Down
9 changes: 8 additions & 1 deletion policy/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from graphene_django import DjangoObjectType
from django.utils.translation import gettext as _
from .apps import PolicyConfig
from .models import Policy
from .models import Policy, PolicyRenewal
from core import (
prefix_filterset,
ExtendedConnection,
Expand Down Expand Up @@ -48,6 +48,13 @@ class Meta:
}
connection_class = ExtendedConnection

class PolicyRenewalGQLType(DjangoObjectType):
class Meta:
model = PolicyRenewal
interfaces = (graphene.relay.Node,)
filter_fields = {
}
connection_class = ExtendedConnection

class PolicyAndWarningsGQLType(graphene.ObjectType):
policy = graphene.Field(PolicyGQLType)
Expand Down
30 changes: 30 additions & 0 deletions policy/migrations/0008_policyrenewalmutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.19 on 2023-08-30 16:33

import core.models
from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('core', '0023_alter_jsonext_column_in_tblOfficer'),
('policy', '0007_fix_policy_mutation_name'),
]

operations = [
migrations.CreateModel(
name='PolicyRenewalMutation',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('mutation', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='policy_renewals', to='core.mutationlog')),
('policy_renewal', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='mutations', to='policy.policyrenewal')),
],
options={
'db_table': 'policy_renewal_PolicyMutation',
'managed': True,
},
bases=(models.Model, core.models.ObjectMutation),
),
]
14 changes: 14 additions & 0 deletions policy/migrations/0010_merge_20240912_1248.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 3.2.25 on 2024-09-12 12:48

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('policy', '0008_policyrenewalmutation'),
('policy', '0009_auto_20240716_1744'),
]

operations = [
]
9 changes: 9 additions & 0 deletions policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ class Meta:
managed = True
db_table = "policy_PolicyMutation"

class PolicyRenewalMutation(core_models.UUIDModel, core_models.ObjectMutation):
policy_renewal = models.ForeignKey(PolicyRenewal, models.DO_NOTHING,
related_name='mutations')
mutation = models.ForeignKey(
core_models.MutationLog, models.DO_NOTHING, related_name='policy_renewals')

class Meta:
managed = True
db_table = "policy_renewal_PolicyMutation"

if "claim" in sys.modules:
from claim.models import Claim
Expand Down
16 changes: 15 additions & 1 deletion policy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import graphene_django_optimizer as gql_optimizer
from graphene_django.filter import DjangoFilterConnectionField
from core.models import Officer
from .models import PolicyMutation, Policy
from .models import PolicyMutation, Policy, PolicyRenewal
from product.models import Product
from insuree.models import Family, Insuree, InsureePolicy
from django.db.models import OuterRef, Subquery, F, Count
Expand All @@ -29,6 +29,7 @@
PolicyAndWarningsGQLType,
PolicyGQLType,
OfficerGQLType,
PolicyRenewalGQLType,
PolicyByFamilyOrInsureeConnection,
) # lgtm [py/polluting-import]
from .gql_mutations import (
Expand Down Expand Up @@ -64,6 +65,7 @@ class Query(graphene.ObjectType):
confirmationType=graphene.String(),
orderBy=graphene.List(of_type=graphene.String),
)

# Note:
# A Policy is bound to a Family...
# but an insuree of the family is only covered by the family policy
Expand Down Expand Up @@ -108,6 +110,18 @@ class Query(graphene.ObjectType):
region=graphene.String(),
)

policy_renewals = DjangoFilterConnectionField(PolicyRenewalGQLType)

def resolve_policy_renewals(self, info, **kwargs):
if not info.context.user.has_perms(PolicyConfig.gql_mutation_renew_policies_perms):
raise PermissionDenied(_("unauthorized"))
user = info.context.user
filters = Q(validity_to__isnull=True)
if hasattr(user, "is_imis_admin") and not user.is_imis_admin:
enrollment_officer = user.officer
filters &= Q(new_officer=enrollment_officer)
return PolicyRenewal.objects.filter(filters)

def resolve_policy_values(self, info, **kwargs):
if not info.context.user.has_perms(PolicyConfig.gql_query_policies_perms):
raise PermissionDenied(_("unauthorized"))
Expand Down
21 changes: 21 additions & 0 deletions policy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ def reset_policy_before_update(policy):
policy.officer_id = None


class PolicyRenewalService:
def __init__(self, user):
self.user = user

def delete(self, policy_renewal):
try:
policy_renewal.delete_history()
logger.info(f"Deleting the related policy renewal details, if any")
for detail in policy_renewal.details.all():
detail.delete_history()
return []
except Exception as exc:
logger.error(f'ERROR {exc}')
return {
'title': policy_renewal.uuid,
'list': [{
'message': _("policy_renewal.mutation.failed_to_delete_policy_renewal") % {'policy_renewal': str(policy_renewal)},
'detail': policy_renewal.uuid}]
}


class PolicyService:
def __init__(self, user):
self.user = user
Expand Down

0 comments on commit 73bc8e6

Please sign in to comment.