From 22736ce2fb725b7225b8c7a5bc53a130d8660c05 Mon Sep 17 00:00:00 2001 From: Isuru Gunawardana <118843449+isuru-aot@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:59:00 -0700 Subject: [PATCH] [MDS-5530] Automatically close an ESUP if a new amendment is issued (#2737) update status of the previous permit or permit amendment --- .../models/explosives_permit.py | 12 ++++++++ .../models/explosives_permit_amendment.py | 29 ++++++++++++++++++- .../explosives_permit_amendment_list.py | 10 ++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/services/core-api/app/api/mines/explosives_permit/models/explosives_permit.py b/services/core-api/app/api/mines/explosives_permit/models/explosives_permit.py index ed01c949e9..0069615724 100644 --- a/services/core-api/app/api/mines/explosives_permit/models/explosives_permit.py +++ b/services/core-api/app/api/mines/explosives_permit/models/explosives_permit.py @@ -15,6 +15,7 @@ from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.schema import FetchedValue, Sequence from sqlalchemy import and_, func +from sqlalchemy.sql import update from app.api.utils.models_mixins import SoftDeleteMixin, AuditMixin, PermitMixin, Base from app.extensions import db @@ -34,6 +35,8 @@ class ExplosivesPermit(SoftDeleteMixin, AuditMixin, PermitMixin, Base): permit_number = db.Column(db.String, unique=True) + is_closed = db.Column(db.Boolean) + closed_by = db.Column(db.String(60)) explosive_magazines = db.relationship( @@ -237,6 +240,15 @@ def get_next_permit_number(cls): next_value = sequence.next_value() return func.concat(prefix, next_value) + @classmethod + def update_permit_status(self, explosives_permit_id, is_closed_status): + update_stmt = update(self)\ + .where(self.explosives_permit_id == explosives_permit_id)\ + .values(is_closed = is_closed_status) + update_result = db.session.execute(update_stmt) + db.session.commit() + return update_result.rowcount + @classmethod def create(cls, mine, permit_guid, application_date, originating_system, latitude, longitude, description, issue_date, expiry_date, permit_number, issuing_inspector_party_guid, mine_manager_mine_party_appt_id, diff --git a/services/core-api/app/api/mines/explosives_permit_amendment/models/explosives_permit_amendment.py b/services/core-api/app/api/mines/explosives_permit_amendment/models/explosives_permit_amendment.py index fce06e1e93..088f885b22 100644 --- a/services/core-api/app/api/mines/explosives_permit_amendment/models/explosives_permit_amendment.py +++ b/services/core-api/app/api/mines/explosives_permit_amendment/models/explosives_permit_amendment.py @@ -14,7 +14,9 @@ from app.api.mines.explosives_permit_amendment.models.explosives_permit_amendment_magazine import \ ExplosivesPermitAmendmentMagazine from app.api.utils.models_mixins import Base, SoftDeleteMixin, AuditMixin, PermitMixin -from sqlalchemy import func +from sqlalchemy import func, and_ +from sqlalchemy.sql import update +from app.api.utils.include.user_info import User from app.extensions import db @@ -73,6 +75,31 @@ def get_next_application_number(cls): next_value = sequence.next_value() return func.concat(next_value, f'-{year}-{month}') + @classmethod + def update_amendment_status_by_explosives_permit_id(cls, explosives_permit_id, is_closed_status, amendment_guid_to_exclude = None): + + and_clause = None + + if amendment_guid_to_exclude is not None: + and_clause = and_( + cls.explosives_permit_id == explosives_permit_id, + cls.is_closed != is_closed_status, + cls.explosives_permit_amendment_guid != amendment_guid_to_exclude + ) + else: + and_clause = and_( + cls.explosives_permit_id == explosives_permit_id, + cls.is_closed != is_closed_status, + ) + + update_stmt = update(cls)\ + .where(and_clause)\ + .values(is_closed = is_closed_status) + + update_result = db.session.execute(update_stmt) + db.session.commit() + return update_result.rowcount + @classmethod def create(cls, mine, diff --git a/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment_list.py b/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment_list.py index b0868e7b94..1c096afe17 100644 --- a/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment_list.py +++ b/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment_list.py @@ -9,7 +9,7 @@ from app.api.utils.custom_reqparser import CustomReqparser from app.api.utils.access_decorators import requires_role_edit_explosives_permit from app.api.mines.mine.models.mine import Mine - +from app.api.mines.explosives_permit.models.explosives_permit import ExplosivesPermit class ExplosivesPermitAmendmentListResource(Resource, UserMixin): @@ -165,4 +165,12 @@ def post(self, mine_guid): data.get('now_application_guid')) explosives_permit_amendment.save() + # Updating the previous amendments' is_closed status to True. + updated_columns = ExplosivesPermitAmendment.update_amendment_status_by_explosives_permit_id( + data.get('explosives_permit_id'), True, + explosives_permit_amendment.explosives_permit_amendment_guid) + + if updated_columns == 0: #Explosive Permit status need to be updated. + ExplosivesPermit.update_permit_status(explosives_permit_amendment.explosives_permit_id, True) + return explosives_permit_amendment, 201