diff --git a/policy/gql_mutations.py b/policy/gql_mutations.py index 877bb39..fc2b776 100644 --- a/policy/gql_mutations.py +++ b/policy/gql_mutations.py @@ -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 _ @@ -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 diff --git a/policy/gql_queries.py b/policy/gql_queries.py index e51986e..7f29093 100644 --- a/policy/gql_queries.py +++ b/policy/gql_queries.py @@ -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 @@ -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) diff --git a/policy/migrations/0008_policyrenewalmutation.py b/policy/migrations/0008_policyrenewalmutation.py new file mode 100644 index 0000000..d25448b --- /dev/null +++ b/policy/migrations/0008_policyrenewalmutation.py @@ -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), + ), + ] diff --git a/policy/migrations/0010_merge_20240912_1248.py b/policy/migrations/0010_merge_20240912_1248.py new file mode 100644 index 0000000..8b5da6b --- /dev/null +++ b/policy/migrations/0010_merge_20240912_1248.py @@ -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 = [ + ] diff --git a/policy/models.py b/policy/models.py index 154b8f5..bfdc045 100644 --- a/policy/models.py +++ b/policy/models.py @@ -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" diff --git a/policy/schema.py b/policy/schema.py index 5249c04..82f28a6 100644 --- a/policy/schema.py +++ b/policy/schema.py @@ -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 @@ -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 @@ -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")) diff --git a/policy/services.py b/policy/services.py index d4de50d..32a4439 100644 --- a/policy/services.py +++ b/policy/services.py @@ -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