Skip to content

Commit

Permalink
Finish feature/policy_renewals_mobile
Browse files Browse the repository at this point in the history
Handling policy renewals from mobile
  • Loading branch information
dragos-dobre authored Oct 7, 2024
2 parents ccaad97 + 8825a07 commit 08ed574
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 4 deletions.
18 changes: 16 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,6 +50,20 @@ def do_mutate(cls, perms, user, **data):
from core.utils import TimeUtils
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, filter_validity, ExtendedConnection, ExtendedRelayConnection
from core.schema import OfficerGQLType
from product.schema import ProductGQLType
Expand Down Expand Up @@ -44,6 +44,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 = [
]
10 changes: 10 additions & 0 deletions policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ class PolicyMutation(core_models.UUIDModel, core_models.ObjectMutation):
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"
14 changes: 13 additions & 1 deletion policy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import graphene_django_optimizer as gql_optimizer
from graphene_django.filter import DjangoFilterConnectionField
from core.models import Officer
from .models import PolicyMutation
from product.models import Product
from contribution.models import Premium
from insuree.models import Family, Insuree, InsureePolicy
Expand Down Expand Up @@ -51,6 +50,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 @@ -96,6 +96,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 @@ -36,6 +36,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 08ed574

Please sign in to comment.