Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDS-5530] Automatically close an ESUP if a new amendment is issued #2737

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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