From b9664cc5fd3d6a490262efe5ea525c4ab6f86390 Mon Sep 17 00:00:00 2001 From: Henry Oforeh Date: Wed, 6 Mar 2024 10:10:29 -0700 Subject: [PATCH] refactor code. --- .../reports/reportSubmissionSlice.ts | 36 +-------------- services/common/src/constants/API.ts | 11 +---- .../reports/mineReportSubmission.interface.ts | 5 --- services/core-api/app/api/mines/namespace.py | 2 - .../reports/models/mine_report_submission.py | 13 ------ .../mine_report_submission_update_resource.py | 44 ------------------ .../test_mine_report_submissions_resource.py | 45 ++++++++----------- .../components/mine/Reports/ReportPage.tsx | 13 +++--- 8 files changed, 30 insertions(+), 139 deletions(-) delete mode 100644 services/core-api/app/api/mines/reports/resources/mine_report_submission_update_resource.py diff --git a/services/common/src/components/reports/reportSubmissionSlice.ts b/services/common/src/components/reports/reportSubmissionSlice.ts index 4b680c0797..aa05813430 100644 --- a/services/common/src/components/reports/reportSubmissionSlice.ts +++ b/services/common/src/components/reports/reportSubmissionSlice.ts @@ -1,5 +1,5 @@ import { hideLoading, showLoading } from "react-redux-loading-bar"; -import { ENVIRONMENT, IMineReportSubmission, IUpdateMineReportSubmissionStatus } from "../.."; +import { ENVIRONMENT, IMineReportSubmission } from "../.."; import { createAppSlice } from "@mds/common/redux/createAppSlice"; import CustomAxios from "@mds/common/redux/customAxios"; import * as API from "@mds/common/constants/API"; @@ -79,34 +79,6 @@ const submissionSlice = createAppSlice({ }, } ), - updateReportSubmission: create.asyncThunk( - async (payload: IUpdateMineReportSubmissionStatus, thunkApi) => { - const headers = createRequestHeader(); - thunkApi.dispatch(showLoading()); - const messages = { - errorToastMessage: "default", - successToastMessage: "Successfully updated report submission", - }; - const params = { - mine_report_submission_guid: payload.mine_report_submission_guid, - }; - const resp = await CustomAxios(messages).patch( - `${ENVIRONMENT.apiUrl}${API.MINE_REPORT_SUBMISSIONS(params)}`, - { ...payload }, - headers - ); - thunkApi.dispatch(hideLoading()); - return resp.data; - }, - { - fulfilled: (state, action) => { - state.reportSubmission = action.payload; - }, - rejected: (_state, action) => { - rejectHandler(action); - }, - } - ), }), selectors: { getLatestReportSubmission: (state, mineReportGuid): IMineReportSubmission => { @@ -118,11 +90,7 @@ const submissionSlice = createAppSlice({ }, }); -export const { - fetchLatestReportSubmission, - createReportSubmission, - updateReportSubmission, -} = submissionSlice.actions; +export const { fetchLatestReportSubmission, createReportSubmission } = submissionSlice.actions; export const { getLatestReportSubmission } = submissionSlice.getSelectors( (rootState: RootState) => rootState.reportSubmission ); diff --git a/services/common/src/constants/API.ts b/services/common/src/constants/API.ts index bd44d2f847..32631aa024 100644 --- a/services/common/src/constants/API.ts +++ b/services/common/src/constants/API.ts @@ -246,19 +246,12 @@ export const MINE_REPORTS = (mineGuid, reportsType?) => export const MINE_REPORT = (mineGuid, mineReportGuid) => `/mines/${mineGuid}/reports/${mineReportGuid}`; export const MINE_REPORT_SUBMISSIONS = ( - params: { mine_report_guid?: string; mine_report_submission_guid?: string }, + params: { mine_report_guid?: string }, latest_submission = true ) => { const queryParams = latest_submission ? { latest_submission, ...params } : params; - const baseURI = "/mines/reports/submissions"; - - if (params?.mine_report_submission_guid) { - return `${baseURI}/${params.mine_report_submission_guid}`; - } - - return `${baseURI}?${queryString.stringify(queryParams)}`; + return `/mines/reports/submissions?${queryString.stringify(queryParams)}`; }; - export const MINE_REPORT_DOCUMENT = (mineGuid) => `/mines/${mineGuid}/reports/documents`; export const MINE_REPORT_COMMENTS = (mineGuid, reportGuid) => `/mines/${mineGuid}/reports/${reportGuid}/comments`; diff --git a/services/common/src/interfaces/reports/mineReportSubmission.interface.ts b/services/common/src/interfaces/reports/mineReportSubmission.interface.ts index 2ef224d677..bf8f42e2be 100644 --- a/services/common/src/interfaces/reports/mineReportSubmission.interface.ts +++ b/services/common/src/interfaces/reports/mineReportSubmission.interface.ts @@ -32,8 +32,3 @@ export interface IMineReportSubmission { update_timestamp: string; update_user: string; } - -export interface IUpdateMineReportSubmissionStatus { - mine_report_submission_guid: string; - mine_report_submission_status_code: MINE_REPORT_SUBMISSION_CODES; -} diff --git a/services/core-api/app/api/mines/namespace.py b/services/core-api/app/api/mines/namespace.py index db748680a5..54529d0596 100644 --- a/services/core-api/app/api/mines/namespace.py +++ b/services/core-api/app/api/mines/namespace.py @@ -29,7 +29,6 @@ from app.api.mines.explosives_permit.resources.explosives_permit_document_type import ExplosivesPermitDocumentTypeResource, ExplosivesPermitDocumentTypeListResource, ExplosivesPermitDocumentGenerateResource from app.api.mines.region.resources.region import MineRegionResource from app.api.mines.reports.resources.mine_report_document import MineReportDocumentListResource -from app.api.mines.reports.resources.mine_report_submission_update_resource import ReportSubmissionUpdateResource from app.api.mines.reports.resources.reports_resource import ReportsResource from app.api.mines.reports.resources.mine_reports import MineReportListResource, MineReportResource from app.api.mines.reports.resources.mine_report_submission_resource import ReportSubmissionResource @@ -120,7 +119,6 @@ api.add_resource(MineReportListResource, '//reports') api.add_resource(MineReportResource, '//reports/') api.add_resource(ReportSubmissionResource, '/reports/submissions') -api.add_resource(ReportSubmissionUpdateResource, '/reports/submissions/') api.add_resource(MineReportDefinitionListResource, '/reports/definitions') api.add_resource(MineReportCommentListResource, '//reports//comments') diff --git a/services/core-api/app/api/mines/reports/models/mine_report_submission.py b/services/core-api/app/api/mines/reports/models/mine_report_submission.py index 6f8039319b..3439e067d9 100644 --- a/services/core-api/app/api/mines/reports/models/mine_report_submission.py +++ b/services/core-api/app/api/mines/reports/models/mine_report_submission.py @@ -108,12 +108,6 @@ def find_by_guid(cls, _id): except ValueError: return None - @classmethod - def find_by_mine_report_submission_guid(cls, mine_report_submission_guid): - return cls.query.filter_by( - mine_report_submission_guid=mine_report_submission_guid).one_or_none() - - def json(self): return { 'comments': [comment.json() for comment in self.comments], @@ -139,10 +133,3 @@ def json(self): 'update_timestamp': str(self.update_timestamp), 'update_user': str(self.update_user) } - - def patch(self, mine_report_submission_status_code, add_to_session=True): - self.mine_report_submission_status_code = mine_report_submission_status_code - - if add_to_session: - self.save(commit=False) - return self diff --git a/services/core-api/app/api/mines/reports/resources/mine_report_submission_update_resource.py b/services/core-api/app/api/mines/reports/resources/mine_report_submission_update_resource.py deleted file mode 100644 index 592ed69fec..0000000000 --- a/services/core-api/app/api/mines/reports/resources/mine_report_submission_update_resource.py +++ /dev/null @@ -1,44 +0,0 @@ -from flask_restx import Resource -from werkzeug.exceptions import NotFound - -from app.api.mines.reports.models.mine_report_submission import MineReportSubmission -from app.api.mines.response_models import MINE_REPORT_SUBMISSION_MODEL -from app.extensions import api -from app.api.utils.resources_mixins import UserMixin -from app.api.utils.custom_reqparser import CustomReqparser -from app.api.utils.access_decorators import requires_any_of, EDIT_REPORT, MINE_ADMIN - - -class ReportSubmissionUpdateResource(Resource, UserMixin): - parser = CustomReqparser() - - parser.add_argument( - 'mine_report_submission_status_code', - type=str, - store_missing=False, - required=True, - ) - - @api.doc( - description='Update a Mine Report Submission.', - params={ - 'mine_report_submission_guid': 'The GUID of the Mine Report Submission to update.' - }) - @api.marshal_with(MINE_REPORT_SUBMISSION_MODEL, code=201) - @requires_any_of([EDIT_REPORT, MINE_ADMIN]) - def patch(self, mine_report_submission_guid): - - mine_report_submission = MineReportSubmission.find_by_mine_report_submission_guid(mine_report_submission_guid) - - data = self.parser.parse_args() - - if mine_report_submission is None: - raise NotFound('Mine Report Submission not found') - - mine_report_submission.patch(data.get('mine_report_submission_status_code')) - - mine_report_submission.save() - - return mine_report_submission - - diff --git a/services/core-api/tests/reports/resource/test_mine_report_submissions_resource.py b/services/core-api/tests/reports/resource/test_mine_report_submissions_resource.py index 4a546d3ba5..3c28e6a6b0 100644 --- a/services/core-api/tests/reports/resource/test_mine_report_submissions_resource.py +++ b/services/core-api/tests/reports/resource/test_mine_report_submissions_resource.py @@ -6,13 +6,13 @@ from app.api.mines.reports.models.mine_report_submission_status_code import MineReportSubmissionStatusCode from app.api.constants import MINE_REPORT_TYPE -from tests.factories import MineFactory, MineDocumentFactory, MineReportFactory, MineReportSubmissionFactory +from tests.factories import MineFactory, MineDocumentFactory, MineReportFactory # GET def test_get_all_mine_report_submissions_for_report(test_client, db_session, auth_headers): mine_report = MineReportFactory() - + params = f'?mine_report_guid={mine_report.mine_report_guid}' get_resp = test_client.get( f'mines/reports/submissions{params}', headers=auth_headers['full_auth_header']) @@ -22,6 +22,7 @@ def test_get_all_mine_report_submissions_for_report(test_client, db_session, aut assert str(mine_report.mine_report_guid) == get_data[0]['mine_report_guid'] assert get_resp.status_code == 200 + def test_get_mine_report_with_no_submissions(test_client, db_session, auth_headers): mine_report = MineReportFactory(mine_report_submissions=0) @@ -33,12 +34,13 @@ def test_get_mine_report_with_no_submissions(test_client, db_session, auth_heade assert get_resp.status_code == 200 assert get_data['mine_report_guid'] == str(mine_report.mine_report_guid) assert get_data['mine_report_submission_status_code'] == "NON" - assert get_data['mine_report_submission_guid'] == None - assert get_data['documents'] == None + assert get_data['mine_report_submission_guid'] == None + assert get_data['documents'] == None + def test_get_latest_mine_report_submission_for_report(test_client, db_session, auth_headers): mine_report = MineReportFactory() - + params = f'?latest_submission=true&mine_report_guid={mine_report.mine_report_guid}' get_resp = test_client.get( f'mines/reports/submissions{params}', headers=auth_headers['full_auth_header']) @@ -49,14 +51,15 @@ def test_get_latest_mine_report_submission_for_report(test_client, db_session, a def test_get_latest_report_submission_without_mine_report_guid(test_client, db_session, auth_headers): - # TODO: in the end this is not desired behaviour, - # this is just a reminder to write a real test when implementing + # TODO: in the end this is not desired behaviour, + # this is just a reminder to write a real test when implementing params = '?latest_submission=true' get_resp = test_client.get( f'mines/reports/submissions{params}', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 400 + # # POST def test_post_initial_crr_mine_report_submission(test_client, db_session, auth_headers): mine = MineFactory(minimal=True) @@ -77,7 +80,7 @@ def test_post_initial_crr_mine_report_submission(test_client, db_session, auth_h 'received_date': "2024-02-09", 'submission_year': "2024", 'submitter_email': "email@email.com", - 'submitter_name': "Submitter Name", + 'submitter_name': "Submitter Name", } # post from Minespace @@ -93,6 +96,7 @@ def test_post_initial_crr_mine_report_submission(test_client, db_session, auth_h assert num_submissions == 1 assert latest_submission['mine_report_submission_status_code'] == "INI" + def test_post_requested_report(test_client, db_session, auth_headers): mine_report = MineReportFactory(mine_report_submissions=0) @@ -112,7 +116,7 @@ def test_post_requested_report(test_client, db_session, auth_headers): 'received_date': "2024-02-09", 'submission_year': "2024", 'submitter_email': "email@email.com", - 'submitter_name': "Submitter Name", + 'submitter_name': "Submitter Name", } # post from Minespace @@ -125,6 +129,7 @@ def test_post_requested_report(test_client, db_session, auth_headers): assert post_data['mine_report_submission_guid'] is not None assert post_data['mine_report_definition_guid'] == str(mine_report_definition.mine_report_definition_guid) + def test_post_additional_mine_report_submission(test_client, db_session, auth_headers): mine_report = MineReportFactory(mine_report_submissions=1) first_submission = mine_report.mine_report_submissions[0] @@ -160,7 +165,7 @@ def test_post_additional_mine_report_submission(test_client, db_session, auth_he if x.mine_report_submission_status_code != first_submission.mine_report_submission_status_code ][0] submission_data['mine_report_submission_status_code'] = new_status_code.mine_report_submission_status_code - + submission_data['description_comment'] = "New description comment" post_resp = test_client.post( @@ -178,22 +183,10 @@ def test_post_additional_mine_report_submission(test_client, db_session, auth_he # fields that should be changed assert previous_submission['update_timestamp'] != latest_submission['update_timestamp'] assert previous_submission['submission_date'] != latest_submission['submission_date'] - assert previous_submission['mine_report_submission_status_code'] != latest_submission['mine_report_submission_status_code'] + assert previous_submission['mine_report_submission_status_code'] != latest_submission[ + 'mine_report_submission_status_code'] assert previous_submission['description_comment'] != latest_submission['description_comment'] - + # fields that should not change - assert previous_submission['received_date'] == latest_submission['received_date'] + assert previous_submission['received_date'] == latest_submission['received_date'] assert previous_submission['create_timestamp'] + '+00:00' == latest_submission['create_timestamp'] - -def test_patch_mine_report_submission_status(test_client, db_session, auth_headers): - mine_report_submission = MineReportSubmissionFactory() - - submission_data = { - 'mine_report_submission_status_code': 'ACC', - } - - post_resp = test_client.patch( - f'mines/reports/submissions/{mine_report_submission.mine_report_submission_guid}', headers=auth_headers['full_auth_header'], json=submission_data) - - assert post_resp.status_code == 200 - \ No newline at end of file diff --git a/services/core-web/src/components/mine/Reports/ReportPage.tsx b/services/core-web/src/components/mine/Reports/ReportPage.tsx index c851b16f21..a551e390bc 100644 --- a/services/core-web/src/components/mine/Reports/ReportPage.tsx +++ b/services/core-web/src/components/mine/Reports/ReportPage.tsx @@ -11,7 +11,6 @@ import * as routes from "@/constants/routes"; import { FORM, IMineReportSubmission, - IUpdateMineReportSubmissionStatus, MINE_REPORT_STATUS_HASH, MINE_REPORT_SUBMISSION_CODES, } from "@mds/common"; @@ -22,7 +21,6 @@ import { fetchLatestReportSubmission, getLatestReportSubmission, createReportSubmission, - updateReportSubmission, } from "@mds/common/components/reports/reportSubmissionSlice"; import ReportDetailsForm from "@mds/common/components/reports/ReportDetailsForm"; @@ -32,7 +30,7 @@ import ScrollSidePageWrapper from "@mds/common/components/common/ScrollSidePageW import modalConfig from "@/components/modalContent/config"; import { closeModal, openModal } from "@mds/common/redux/actions/modalActions"; -import { formatDate, getMineReportStatusDescription } from "@mds/common/redux/utils/helpers"; +import { getMineReportStatusDescription } from "@mds/common/redux/utils/helpers"; const ReportPage: FC = () => { const dispatch = useDispatch(); @@ -92,10 +90,13 @@ const ReportPage: FC = () => { dispatch(closeModal()); }; - const handleUpdateMineSubmissionStatus = (values: IUpdateMineReportSubmissionStatus) => { - values.mine_report_submission_guid = latestSubmission.mine_report_submission_guid; + const handleUpdateMineSubmissionStatus = (values) => { + const payload = { + ...latestSubmission, + mine_report_submission_status_code: values.mine_report_submission_status_code, + }; handleCloseModal(); - dispatch(updateReportSubmission(values)).then((response) => { + dispatch(createReportSubmission(payload)).then((response) => { if (response.payload) { dispatch(fetchLatestReportSubmission({ mine_report_guid: reportGuid })); dispatch(fetchMineRecordById(mineGuid));