Skip to content

Commit

Permalink
refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
henryoforeh-dev committed Mar 6, 2024
1 parent 9c8ec9c commit b9664cc
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 139 deletions.
36 changes: 2 additions & 34 deletions services/common/src/components/reports/reportSubmissionSlice.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 => {
Expand All @@ -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
);
Expand Down
11 changes: 2 additions & 9 deletions services/common/src/constants/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions services/core-api/app/api/mines/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,7 +119,6 @@
api.add_resource(MineReportListResource, '/<string:mine_guid>/reports')
api.add_resource(MineReportResource, '/<string:mine_guid>/reports/<string:mine_report_guid>')
api.add_resource(ReportSubmissionResource, '/reports/submissions')
api.add_resource(ReportSubmissionUpdateResource, '/reports/submissions/<string:mine_report_submission_guid>')
api.add_resource(MineReportDefinitionListResource, '/reports/definitions')
api.add_resource(MineReportCommentListResource,
'/<string:mine_guid>/reports/<string:mine_report_guid>/comments')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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)

Expand All @@ -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'])
Expand All @@ -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)
Expand All @@ -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 protected]",
'submitter_name': "Submitter Name",
'submitter_name': "Submitter Name",
}

# post from Minespace
Expand All @@ -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)

Expand All @@ -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 protected]",
'submitter_name': "Submitter Name",
'submitter_name': "Submitter Name",
}

# post from Minespace
Expand All @@ -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]
Expand Down Expand Up @@ -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(
Expand All @@ -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

13 changes: 7 additions & 6 deletions services/core-web/src/components/mine/Reports/ReportPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -22,7 +21,6 @@ import {
fetchLatestReportSubmission,
getLatestReportSubmission,
createReportSubmission,
updateReportSubmission,
} from "@mds/common/components/reports/reportSubmissionSlice";

import ReportDetailsForm from "@mds/common/components/reports/ReportDetailsForm";
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b9664cc

Please sign in to comment.