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-5769] CRR - Edit Mode #2944

Merged
merged 10 commits into from
Feb 13, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
-- add the columns from mine report that are not on mine report submission
ALTER TABLE mine_report_submission
ADD COLUMN IF NOT EXISTS mine_report_definition_id integer,
ADD COLUMN IF NOT EXISTS mine_guid uuid,
ADD COLUMN IF NOT EXISTS permit_id integer,
ADD COLUMN IF NOT EXISTS due_date timestamp,
ADD COLUMN IF NOT EXISTS received_date timestamp default now(),
ADD COLUMN IF NOT EXISTS submission_year smallint,
ADD COLUMN IF NOT EXISTS deleted_ind boolean default False NOT NULL,
-- ADD COLUMN IF NOT EXISTS created_by_idir varchar, // specifically excluding this one
ADD COLUMN IF NOT EXISTS permit_condition_category_code varchar(3),
ADD COLUMN IF NOT EXISTS description_comment varchar,
ADD COLUMN IF NOT EXISTS submitter_name varchar(255),
ADD COLUMN IF NOT EXISTS submitter_email varchar(255);

-- bring over data from parent mine report into mine report submission
UPDATE mine_report_submission
SET
mine_report_definition_id = mine_report.mine_report_definition_id,
mine_guid = mine_report.mine_guid,
permit_id = mine_report.permit_id,
due_date = mine_report.due_date,
received_date = mine_report.received_date,
deleted_ind = mine_report.deleted_ind,
permit_condition_category_code = mine_report.permit_condition_category_code,
description_comment = mine_report.description_comment,
submission_year = mine_report.submission_year,
submitter_name = mine_report.submitter_name,
submitter_email = mine_report.submitter_email
FROM mine_report
WHERE mine_report_submission.mine_report_id = mine_report.mine_report_id;

-- add column constraints
ALTER TABLE mine_report_submission
-- not null constraints
ALTER COLUMN mine_guid SET NOT NULL,

-- foreign key constraints
ADD CONSTRAINT mine_report_submission_mine_report_definition_fkey
FOREIGN KEY (mine_report_definition_id) REFERENCES mine_report_definition(mine_report_definition_id),

ADD CONSTRAINT mine_report_submission_mine_fkey
FOREIGN KEY (mine_guid) REFERENCES mine(mine_guid),

ADD CONSTRAINT mine_report_submission_permit_fkey
FOREIGN KEY (permit_id) REFERENCES permit(permit_id),

ADD CONSTRAINT mine_report_submission_permit_condition_category_fkey
FOREIGN KEY (permit_condition_category_code) REFERENCES permit_condition_category(condition_category_code),

-- existing check on 2 columns
ADD CONSTRAINT submission_condition_category_or_report_def_should_be_specified
CHECK
(
( CASE WHEN permit_condition_category_code IS NULL THEN 0 ELSE 1 END
+ CASE WHEN mine_report_definition_id IS NULL THEN 0 ELSE 1 END
) = 1
);

-- track contacts by submission
ALTER TABLE mine_report_contact
ADD COLUMN IF NOT EXISTS mine_report_submission_id integer;

-- insert an entry for each contact for each submission
INSERT INTO mine_report_contact (name, email, mine_report_id, deleted_ind, mine_report_submission_id)
SELECT
mine_report_contact.name,
mine_report_contact.email,
mine_report_contact.mine_report_id,
mine_report_contact.deleted_ind,
mine_report_submission.mine_report_submission_id
FROM mine_report_submission
LEFT JOIN mine_report_contact
ON mine_report_submission.mine_report_id = mine_report_contact.mine_report_id
WHERE mine_report_contact_id IS NOT NULL;

-- delete old records
DELETE FROM mine_report_contact WHERE mine_report_submission_id IS NULL;

-- add constraints to new column
ALTER TABLE mine_report_contact
ALTER COLUMN mine_report_submission_id SET NOT NULL,
ADD CONSTRAINT mine_report_contact_submission_fkey
FOREIGN KEY (mine_report_submission_id) REFERENCES mine_report_submission(mine_report_submission_id);
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { ReduxWrapper } from "@mds/common/tests/utils/ReduxWrapper";
import ReportDetailsForm from "./ReportDetailsForm";
import { Button } from "antd";
import * as MOCK from "@mds/common/tests/mocks/dataMocks";
import { REPORTS, AUTHENTICATION } from "@mds/common/constants/reducerTypes";
import { AUTHENTICATION } from "@mds/common/constants/reducerTypes";
import { SystemFlagEnum } from "../..";

const mineReport = MOCK.MINE_REPORTS[0];
const mineReportSubmission = MOCK.MINE_REPORT_SUBMISSIONS[0];

const initialState = {
[REPORTS]: {
reports: MOCK.MINE_REPORTS,
mineReportGuid: mineReport.mine_report_guid,
mineReports: [mineReport],
reportSubmission: {
reportSubmission: mineReportSubmission,
mineReportGuid: mineReportSubmission.mine_report_guid,
},
[AUTHENTICATION]: {
systemFlag: SystemFlagEnum.ms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { render } from "@testing-library/react";
import { ReduxWrapper } from "@mds/common/tests/utils/ReduxWrapper";
import ReportDetailsForm from "./ReportDetailsForm";
import { Button } from "antd";
import { AUTHENTICATION, REPORTS, STATIC_CONTENT } from "@mds/common/constants/reducerTypes";
import { AUTHENTICATION, STATIC_CONTENT } from "@mds/common/constants/reducerTypes";
import * as MOCK from "@mds/common/tests/mocks/dataMocks";
import { IMineReport, SystemFlagEnum } from "../..";
import { IMineReportSubmission, SystemFlagEnum } from "../..";

const mineReport = MOCK.MINE_REPORTS[0];
const mineReportSubmission = MOCK.MINE_REPORT_SUBMISSIONS[0];
const initialState = {
[REPORTS]: {
reports: MOCK.MINE_REPORTS,
mineReportGuid: mineReport.mine_report_guid,
mineReports: [mineReport],
reportSubmission: {
reportSubmission: mineReportSubmission,
mineReportGuid: mineReportSubmission.mine_report_guid,
},
[STATIC_CONTENT]: {
mineReportDefinitionOptions: MOCK.BULK_STATIC_CONTENT_RESPONSE.mineReportDefinitionOptions,
Expand All @@ -27,7 +26,7 @@ describe("ReportDetailsForm", () => {
const { container } = render(
<ReduxWrapper initialState={initialState}>
<ReportDetailsForm
initialValues={(mineReport as any) as IMineReport}
initialValues={(mineReportSubmission as any) as IMineReportSubmission}
isEditMode={false}
mineGuid={"123"}
formButtons={
Expand Down
139 changes: 78 additions & 61 deletions services/common/src/components/reports/ReportDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTrash } from "@fortawesome/pro-light-svg-icons";

import { getMineReportDefinitionOptions } from "@mds/common/redux/selectors/staticContentSelectors";
import { ReportSubmissions } from "@mds/common/components/reports/ReportSubmissions";
import ReportFileUpload from "@mds/common/components/reports/ReportFileUpload";

import { FORM } from "@mds/common/constants/forms";
import { email, maxLength, required, yearNotInFuture } from "@mds/common/redux/utils/Validate";
import ReportFilesTable from "./ReportFilesTable";
import { formatComplianceCodeReportName } from "@mds/common/redux/utils/helpers";
import RenderDate from "../forms/RenderDate";
import RenderSelect from "../forms/RenderSelect";
import FormWrapper, { FormConsumer } from "../forms/FormWrapper";
import FormWrapper from "../forms/FormWrapper";
import RenderField from "../forms/RenderField";
import {
IMineReport,
IMineDocument,
IMineReportDefinition,
IMineReportSubmission,
IParty,
Expand All @@ -35,56 +35,56 @@ import { getParties, getPartyRelationships } from "@mds/common/redux/selectors/p
import { uniqBy } from "lodash";
import { getSystemFlag } from "@mds/common/redux/selectors/authenticationSelectors";

const RenderContacts: FC<any> = ({ fields }) => (
<FormConsumer>
{({ isEditMode }) => {
return (
<div>
{fields.map((contact, index) => (
<Row key={contact.id} gutter={[16, 8]}>
<Col span={24}>
<Row>
<Typography.Title level={5}>Report Contact #{index + 1}</Typography.Title>
{isEditMode && (
<Button
icon={<FontAwesomeIcon icon={faTrash} />}
type="text"
onClick={() => fields.remove(index)}
/>
)}
</Row>
</Col>
<Col span={12}>
<Field
name={`${contact}.name`}
component={RenderField}
label={`Contact Name`}
placeholder="Enter name"
required
validate={[required]}
/>
</Col>
<Col span={12}>
<Field
name={`${contact}.email`}
component={RenderField}
label={`Contact Email`}
required
placeholder="Enter email"
validate={[email, required]}
const RenderContacts: FC<any> = ({ fields, isEditMode, mineSpaceEdit }) => {
const canEdit = isEditMode && !mineSpaceEdit;
return (
<div>
{fields.map((contact, index) => (
<Row key={contact.id} gutter={[16, 8]}>
<Col span={24}>
<Row>
<Typography.Title level={5}>Report Contact #{index + 1}</Typography.Title>
{canEdit && (
<Button
style={{ marginTop: 0 }}
icon={<FontAwesomeIcon icon={faTrash} />}
type="text"
onClick={() => fields.remove(index)}
/>
</Col>
)}
</Row>
))}
</div>
);
}}
</FormConsumer>
);
</Col>
<Col span={12}>
<Field
name={`${contact}.name`}
component={RenderField}
label={`Contact Name`}
placeholder="Enter name"
disabled={!canEdit}
required
validate={[required]}
/>
</Col>
<Col span={12}>
<Field
name={`${contact}.email`}
component={RenderField}
label={`Contact Email`}
disabled={!canEdit}
required
placeholder="Enter email"
validate={[email, required]}
/>
</Col>
</Row>
))}
</div>
);
};

interface ReportDetailsFormProps {
isEditMode?: boolean;
initialValues?: IMineReport;
initialValues?: Partial<IMineReportSubmission>;
mineGuid: string;
formButtons: ReactNode;
handleSubmit: (values) => void;
Expand All @@ -100,22 +100,29 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
currentReportDefinition,
}) => {
const dispatch = useDispatch();
const formValues: IMineReport =
const formValues: IMineReportSubmission =
useSelector((state) => getFormValues(FORM.VIEW_EDIT_REPORT)(state)) ?? {};
const [mineManager, setMineManager] = useState<IParty>();
const [mineManagerGuid, setMineManagerGuid] = useState<string>("");
const [selectedReportName, setSelectedReportName] = useState("");
const { mine_report_category = "", mine_report_definition_guid = "" } = formValues;
const {
mine_report_category = "",
mine_report_definition_guid = "",
documents = [],
} = formValues;
const [selectedReportCode, setSelectedReportCode] = useState("");
const [formattedMineReportDefinitionOptions, setFormatMineReportDefinitionOptions] = useState([]);
const [mineReportSubmissions, setMineReportSubmissions] = useState([]);

const partyRelationships: IPartyAppt[] = useSelector((state) => getPartyRelationships(state));
const parties = useSelector((state) => getParties(state));
const mineReportDefinitionOptions = useSelector(getMineReportDefinitionOptions);

const system = useSelector(getSystemFlag);

// minespace users are only allowed to add documents
const mineSpaceEdit =
system === SystemFlagEnum.ms && initialValues?.mine_report_guid && isEditMode;

useEffect(() => {
if (!partyRelationships.length) {
// fetch all party relationships for the mine
Expand Down Expand Up @@ -190,14 +197,13 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
}
}, [mine_report_definition_guid]);

const updateMineReportSubmissions = (updatedSubmissions: IMineReportSubmission[]) => {
dispatch(change(FORM.VIEW_EDIT_REPORT, "mine_report_submissions", updatedSubmissions));
setMineReportSubmissions(updatedSubmissions);
const updateDocuments = (docs: IMineDocument[]) => {
dispatch(change(FORM.VIEW_EDIT_REPORT, "documents", docs));
};

return (
<div>
{(isEditMode || !initialValues) && (
{(isEditMode || !initialValues) && system !== SystemFlagEnum.core && (
<Alert
message=""
description={
Expand Down Expand Up @@ -228,6 +234,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
id="mine_report_definition_guid"
name="mine_report_definition_guid"
label="Report Name"
disabled={mineSpaceEdit}
props={{
data: formattedMineReportDefinitionOptions,
}}
Expand Down Expand Up @@ -273,6 +280,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
id="description_comment"
name="description_comment"
label="Report Title and Additional Comment"
disabled={mineSpaceEdit}
required
props={{ maximumCharacters: 500, rows: 3 }}
component={RenderAutoSizeField}
Expand All @@ -296,6 +304,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
id="submission_year"
name="submission_year"
label="Report Compliance Year/Period"
disabled={mineSpaceEdit}
required
placeholder="Select year"
component={RenderDate}
Expand All @@ -312,6 +321,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
name="due_date"
label="Due Date"
placeholder="Select date"
disabled={mineSpaceEdit}
required
component={RenderDate}
validate={[required]}
Expand All @@ -324,6 +334,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
name="submitter_name"
label="Submitter Name"
placeholder="Enter name"
disabled={mineSpaceEdit}
required
component={RenderField}
validate={[required]}
Expand All @@ -336,6 +347,7 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
name="submitter_email"
label="Submitter Email"
placeholder="Enter email"
disabled={mineSpaceEdit}
component={RenderField}
validate={[email]}
help="By providing your email, you agree to receive notification of the report"
Expand All @@ -362,10 +374,14 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
<Typography.Paragraph>{mineManager?.email ?? "-"}</Typography.Paragraph>
</Col>
<Col span={24}>
<FieldArray name="mine_report_contacts" component={RenderContacts} />
<FieldArray
name="mine_report_contacts"
component={RenderContacts}
props={{ isEditMode, mineSpaceEdit }}
/>
</Col>
<Col span={24}>
{isEditMode && (
{isEditMode && !mineSpaceEdit && (
<Button
type="link"
onClick={() =>
Expand All @@ -391,13 +407,14 @@ const ReportDetailsForm: FC<ReportDetailsFormProps> = ({
/>
)}
{isEditMode && (
<ReportSubmissions
<ReportFileUpload
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's showing as a completely new file, but I changed ReportSubmissions --> ReportFileUpload, since we are only dealing with one submission and don't really need to parse its documents (much simpler here at least)

mineGuid={mineGuid}
mineReportSubmissions={mineReportSubmissions}
updateMineReportSubmissions={updateMineReportSubmissions}
isProponent={system === SystemFlagEnum.ms}
documents={documents}
updateDocuments={updateDocuments}
/>
)}
<ReportFilesTable report={formValues} />
<ReportFilesTable documents={documents} />
</Col>
</Row>
{formButtons}
Expand Down
Loading
Loading