Skip to content

Commit

Permalink
[MDS-6285] ESUP amendments detonator magazine missing (#3348)
Browse files Browse the repository at this point in the history
* fix bug where esup magazine data not showing in preview certificate

* addressing pr comment

* remove unwanted import
  • Loading branch information
asinn134 authored Dec 18, 2024
1 parent d81622d commit a962931
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from app.api.utils.models_mixins import AuditMixin, Base
from app.extensions import db
from app.api.mines.exceptions.mine_exceptions import MineException
from app.api.mines.explosives_permit_amendment.models.explosives_permit_amendment_magazine import ExplosivesPermitAmendmentMagazine

PERMIT_SIGNATURE_IMAGE_HEIGHT_INCHES = 0.8
LETTER_SIGNATURE_IMAGE_HEIGHT_INCHES = 0.8
Expand Down Expand Up @@ -40,7 +41,7 @@ def get_with_context(cls, document_type_code, context_guid):

return document_type

def transform_template_data(self, template_data, explosives_permit):
def transform_template_data(self, template_data, explosives_permit, explosives_permit_amendment_id=None):
def validate_issuing_inspector(explosives_permit):
if not explosives_permit.issuing_inspector:
raise MineException("No Issuing Inspector has been assigned",
Expand Down Expand Up @@ -172,9 +173,16 @@ def is_explosive_type(mag):
}
transformed_magazines.append(transformed_magazine)
return transformed_magazines

explosive_magazines = transform_magazines(explosives_permit.explosive_magazines or [])
detonator_magazines = transform_magazines(explosives_permit.detonator_magazines or [])
untransformed_explosive_magazines = explosives_permit.explosive_magazines
untransformed_detonator_magazines = explosives_permit.detonator_magazines

explosives_permit_amendment_magazines = ExplosivesPermitAmendmentMagazine.find_by_explosives_permit_amendment_id(explosives_permit_amendment_id)
if explosives_permit_amendment_id and len(explosives_permit_amendment_magazines) > 0:
untransformed_explosive_magazines = list(filter(lambda mag: mag.explosives_permit_amendment_magazine_type_code == "EXP", explosives_permit_amendment_magazines))
untransformed_detonator_magazines = list(filter(lambda mag: mag.explosives_permit_amendment_magazine_type_code == "DET", explosives_permit_amendment_magazines))

explosive_magazines = transform_magazines(untransformed_explosive_magazines or [])
detonator_magazines = transform_magazines(untransformed_detonator_magazines or [])
magazines = explosive_magazines + detonator_magazines
template_data['magazines'] = magazines

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from app.extensions import api, cache
from app.api.mines.explosives_permit.models.explosives_permit import ExplosivesPermit
from app.api.mines.explosives_permit_amendment.models.explosives_permit_amendment import ExplosivesPermitAmendment
from app.api.mines.explosives_permit.models.explosives_permit_document_type import ExplosivesPermitDocumentType
from app.api.utils.resources_mixins import UserMixin
from app.api.utils.include.user_info import User
Expand Down Expand Up @@ -47,6 +48,7 @@ def get(self, document_type_code):
class ExplosivesPermitDocumentGenerateResource(Resource, UserMixin):
parser = CustomReqparser()
parser.add_argument('explosives_permit_guid', type=str, location='json', required=True)
parser.add_argument('explosives_permit_amendment_id', type=str, location='json', required=False)
parser.add_argument('template_data', type=dict, location='json', required=True)

@api.doc(
Expand All @@ -70,12 +72,24 @@ def post(self, document_type_code):

explosives_permit_guid = data['explosives_permit_guid']
explosives_permit = ExplosivesPermit.find_by_explosives_permit_guid(explosives_permit_guid)
explosives_permit_id = explosives_permit.explosives_permit_id
explosives_permit_amendment_id = data.get('explosives_permit_amendment_id', None)
explosives_permit_amendments = ExplosivesPermitAmendment.find_by_explosives_permit_id(explosives_permit_id)
if explosives_permit_amendment_id:
is_amendment_matched_with_permit = any(
amendments.explosives_permit_amendment_id == int(explosives_permit_amendment_id)
for amendments in explosives_permit_amendments
)

if is_amendment_matched_with_permit == False:
raise MineException("Provided Explosive Permit Amendment Id does not exist under this Explosive Permit", status_code = 400)

if not explosives_permit:
raise MineException("Explosives Permit not found",
status_code = 404)

template_data = data['template_data']
template_data = document_type.transform_template_data(template_data, explosives_permit)
template_data = document_type.transform_template_data(template_data, explosives_permit, explosives_permit_amendment_id)

form_spec_with_context = document_template._form_spec_with_context(explosives_permit_guid)
enforced_data = [x for x in form_spec_with_context if x.get('read-only') == True]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def delete(self, commit=True):
def find_by_explosives_permit_amendment_guid(cls, explosives_permit_amendment_guid):
return cls.query.filter_by(
explosives_permit_amendment_guid=explosives_permit_amendment_guid, deleted_ind=False).one_or_none()

@classmethod
def find_by_explosives_permit_id(cls, explosives_permit_id):
return cls.query.filter_by(
explosives_permit_id=explosives_permit_id, deleted_ind=False).all()

def update(self,
amendment_no,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ def create(cls,
def find_by_explosives_permit_amendment_magazine_id(cls, explosives_permit_amendment_magazine_id):
return cls.query.filter_by(
explosives_permit_amendment_magazine_id=explosives_permit_amendment_magazine_id,
deleted_ind=False).one_or_none()
deleted_ind=False).one_or_none()

@classmethod
def find_by_explosives_permit_amendment_id(cls, explosives_permit_amendment_id):
return cls.query.filter_by(
explosives_permit_amendment_id=explosives_permit_amendment_id,
deleted_ind=False).order_by(cls.explosives_permit_amendment_magazine_id.asc()).all()
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ def test_explosives_permit_amendment_find_by_explosives_permit_amendment_guid(db
explosives_permit_amendment_guid = explosives_permit_amendment.explosives_permit_amendment_guid
explosives_permit_amendment = ExplosivesPermitAmendment.find_by_explosives_permit_amendment_guid(str(explosives_permit_amendment_guid))
assert explosives_permit_amendment.explosives_permit_amendment_guid == explosives_permit_amendment_guid

def test_explosives_permit_amendment_find_by_explosives_permit_id(db_session):
explosives_permit_amendment = ExplosivesPermitAmendmentFactory()
explosives_permit_id = explosives_permit_amendment.explosives_permit_id
explosives_permit_amendments = ExplosivesPermitAmendment.find_by_explosives_permit_id(explosives_permit_id)
assert any(amendment.explosives_permit_id == explosives_permit_id for amendment in explosives_permit_amendments)
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const ExplosivesPermit: FC<ExplosivesPermitProps> = ({
if (record.isAmendment) values.amendment_no = record.amendment_no;
const payload = {
explosives_permit_guid: record.explosives_permit_guid,
...(record.explosives_permit_amendment_id && { explosives_permit_amendment_id: record.explosives_permit_amendment_id }),
template_data: values,
};
return props.generateExplosivesPermitDocument(
Expand Down

0 comments on commit a962931

Please sign in to comment.