Skip to content

Commit

Permalink
[MDS-5530] Automatically close an ESUP if a new amendment is issued (#…
Browse files Browse the repository at this point in the history
…2737)

update status of the previous permit or permit amendment
  • Loading branch information
isuru-aot authored Oct 24, 2023
1 parent 5cde9a8 commit 22736ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 22736ce

Please sign in to comment.