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 8f65ea5b1b..b33207bec5 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 @@ -352,3 +352,9 @@ def find_by_mine_guid(cls, mine_guid): def find_by_explosives_permit_guid(cls, explosives_permit_guid): return cls.query.filter_by( explosives_permit_guid=explosives_permit_guid, deleted_ind=False).one_or_none() + + @classmethod + def find_permit_number_by_explosives_permit_id(cls, explosives_permit_id): + obj = cls.query.filter_by( + explosives_permit_id=explosives_permit_id, deleted_ind=False).one_or_none() + return obj.permit_number if obj else None 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 04152e67da..95fe3c03b8 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 @@ -6,6 +6,7 @@ from sqlalchemy.schema import Sequence from app.api.mines.documents.models.mine_document import MineDocument +from app.api.mines.explosives_permit.models.explosives_permit import ExplosivesPermit from app.api.mines.explosives_permit.models.explosives_permit_document_type import ExplosivesPermitDocumentType from app.api.mines.explosives_permit_amendment.models.explosives_permit_amendment_document_xref import \ @@ -84,7 +85,6 @@ def create(cls, description, issue_date, expiry_date, - permit_number, issuing_inspector_party_guid, mine_manager_mine_party_appt_id, permittee_mine_party_appt_id, @@ -107,7 +107,6 @@ def create(cls, application_number = ExplosivesPermitAmendment.get_next_application_number() received_timestamp = datetime.utcnow() is_closed = False - permit_number = None issue_date = None expiry_date = None if is_closed: @@ -117,6 +116,8 @@ def create(cls, closed_reason = None closed_timestamp = None + permit_number = ExplosivesPermit.find_permit_number_by_explosives_permit_id(explosives_permit_id) + explosives_permit_amendment = cls( permit_guid=permit_guid, explosives_permit_id=explosives_permit_id, @@ -175,6 +176,7 @@ def find_by_explosives_permit_amendment_guid(cls, explosives_permit_amendment_gu explosives_permit_amendment_guid=explosives_permit_amendment_guid, deleted_ind=False).one_or_none() def update(self, + explosives_permit_id, permit_guid, now_application_guid, issuing_inspector_party_guid, @@ -327,8 +329,8 @@ def create_issued_permit(): ) return ExplosivesPermitAmendmentDocumentResource.generate_explosives_permit_document( token, True, False, False) - if self.application_status == 'REC' and application_status == 'APP': - self.permit_number = ExplosivesPermitAmendment.get_next_permit_number() + + permit_number = ExplosivesPermit.find_permit_number_by_explosives_permit_id(explosives_permit_id) create_permit_enclosed_letter() create_issued_permit() diff --git a/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment.py b/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment.py index e673eef842..2499f2b50b 100644 --- a/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment.py +++ b/services/core-api/app/api/mines/explosives_permit_amendment/resources/explosives_permit_amendment.py @@ -13,6 +13,12 @@ from app.api.mines.explosives_permit_amendment.models.explosives_permit_amendment import ExplosivesPermitAmendment class ExplosivesPermitAmendmentResource(Resource, UserMixin): parser = CustomReqparser() + parser.add_argument( + 'explosives_permit_id', + type=str, + store_missing=False, + required=False, + ) parser.add_argument( 'explosives_permit_guid', type=str, @@ -180,6 +186,7 @@ def put(self,mine_guid, explosives_permit_amendment_guid): data = self.parser.parse_args() explosives_permit_amendment.update( + data.get('explosives_permit_id'), data.get('permit_guid'), data.get('now_application_guid'), data.get('issuing_inspector_party_guid'), data.get('mine_manager_mine_party_appt_id'), data.get('permittee_mine_party_appt_id'), data.get('application_status'), 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 eba42ff36c..1e22680e89 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 @@ -118,12 +118,6 @@ class ExplosivesPermitAmendmentListResource(Resource, UserMixin): store_missing=False, required=False, ) - parser.add_argument( - 'permit_number', - type=str, - store_missing=False, - required=False, - ) parser.add_argument( 'detonator_magazines', type=list, @@ -159,7 +153,6 @@ def post(self, mine_guid): data.get('latitude'), data.get('longitude'), data.get('description'), data.get('issue_date'), data.get('expiry_date'), - data.get('permit_number'), data.get('issuing_inspector_party_guid'), data.get('mine_manager_mine_party_appt_id'), data.get('permittee_mine_party_appt_id'), diff --git a/services/core-api/tests/factories.py b/services/core-api/tests/factories.py index b6744b9a40..242816e968 100644 --- a/services/core-api/tests/factories.py +++ b/services/core-api/tests/factories.py @@ -1080,6 +1080,7 @@ class Params: mine_manager_mine_party_appt_id = factory.SelfAttribute('mine_manager.mine_party_appt_id') permittee_mine_party_appt_id = factory.SelfAttribute('permittee.mine_party_appt_id') + explosives_permit_id = factory.Faker('random_int', min=1000, max=9999) originating_system = 'Core' application_number = factory.Faker('sentence', nb_words=1) received_timestamp = TODAY diff --git a/services/core-api/tests/mines/explosives_permit_amendment/resources/test_explosives_permit_amendment_list_resource.py b/services/core-api/tests/mines/explosives_permit_amendment/resources/test_explosives_permit_amendment_list_resource.py new file mode 100644 index 0000000000..a40c00fdb6 --- /dev/null +++ b/services/core-api/tests/mines/explosives_permit_amendment/resources/test_explosives_permit_amendment_list_resource.py @@ -0,0 +1,77 @@ +from tests.factories import ExplosivesPermitAmendmentFactory, create_mine_and_permit, MinePartyAppointmentFactory, \ + ExplosivesPermitFactory +import json + +def test_post_explosives_permit_amendment_application(test_client, db_session, auth_headers): + mine, permit = create_mine_and_permit() + mine_manager = MinePartyAppointmentFactory(mine=mine) + permittee = MinePartyAppointmentFactory(mine=mine) + explosives_permit = ExplosivesPermitFactory() + data = { + 'explosives_permit_id': explosives_permit.explosives_permit_id, + 'originating_system': + 'Core', + 'permit_guid': + str(permit.permit_guid), + 'mine_manager_mine_party_appt_id': + mine_manager.mine_party_appt_id, + 'permittee_mine_party_appt_id': + permittee.mine_party_appt_id, + 'application_date': + '2021-07-12', + 'description': + 'Other Information.', + 'latitude': + '54', + 'longitude': + '40', + 'explosive_magazines': [{ + 'type_no': '1', + 'tag_no': '1', + 'construction': '1', + 'quantity': '1', + 'latitude': '1', + 'longitude': '1', + 'distance_road': '1', + 'distance_dwelling': '1', + 'length': '1', + 'width': '1', + 'height': '1' + }], + 'detonator_magazines': [{ + 'type_no': '1', + 'tag_no': '1', + 'construction': '1', + 'quantity': '1', + 'longitude': '1', + 'latitude': '1', + 'detonator_type': '1', + 'distance_road': '1', + 'distance_dwelling': '1', + 'width': '1', + 'height': '1', + 'length': '1' + }] + } + + post_resp = test_client.post( + f'/mines/{mine.mine_guid}/explosives-permits-amendment', + json=data, + headers=auth_headers['full_auth_header']) + + assert post_resp.status_code == 201 + assert len(mine.explosives_permits_amendments) == 1 + + post_data = json.loads(post_resp.data.decode()) + assert post_data['explosives_permit_id'] != None + assert post_data['mine_guid'] == str(mine.mine_guid) + assert post_data['permit_guid'] == data['permit_guid'] + assert post_data['application_status'] == 'REC' + assert post_data['application_number'] != None + assert post_data['received_timestamp'] != None + assert post_data['is_closed'] == False + assert post_data['permit_number'] == None + assert post_data['issue_date'] == None + assert post_data['expiry_date'] == None + assert len(post_data['explosive_magazines']) == len(data['explosive_magazines']) + assert len(post_data['detonator_magazines']) == len(data['detonator_magazines']) \ No newline at end of file