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

Fix:1802 project summary bug #1815

Merged
merged 12 commits into from
Aug 10, 2023
59 changes: 32 additions & 27 deletions app/components/Form/ProjectSummaryReportFormSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ const ProjectSummaryReportFormSummary: React.FC<Props> = ({
viewOnly,
isOnAmendmentsAndOtherRevisionsPage,
setHasDiff,
query,
}) => {
const { projectSummaryReportFormBySlug } = useFragment(
graphql`
fragment ProjectSummaryReportFormSummary_query on Query {
projectSummaryReportFormBySlug: formBySlug(
slug: "project_summary_report"
) {
jsonSchema
}
}
`,
query
);

const {
summaryProjectSummaryFormChanges,
isFirstRevision,
Expand All @@ -40,15 +54,11 @@ const ProjectSummaryReportFormSummary: React.FC<Props> = ({
) @connection(key: "connection_summaryProjectSummaryFormChanges") {
edges {
node {
isPristine
newFormData
operation
formChangeByPreviousFormChangeId {
newFormData
}
formByJsonSchemaName {
jsonSchema
}
}
}
}
Expand Down Expand Up @@ -79,21 +89,29 @@ const ProjectSummaryReportFormSummary: React.FC<Props> = ({
// Set custom rjsf fields to display diffs
const customFields = { ...fields, ...CUSTOM_DIFF_FIELDS };

const allFormChangesPristine = useMemo(() => {
if (
projectSummaryReport?.isPristine === false ||
projectSummaryReport?.isPristine === null
)
return false;
return true;
}, [projectSummaryReport?.isPristine]);
const filteredSchema = getFilteredSchema(
projectSummaryReportFormBySlug.jsonSchema.schema as JSONSchema7,
projectSummaryReport || {}
);

const allFormChangesPristine = useMemo(
() => Object.keys(filteredSchema.formData).length === 0,
[filteredSchema.formData]
);

useEffect(
() => setHasDiff && setHasDiff(!allFormChangesPristine),
[allFormChangesPristine, setHasDiff]
);

if (allFormChangesPristine || !projectSummaryReport)
// Set the formSchema and formData based on showing the diff or not
const projectSummaryFormDiffObject = !renderDiff
? {
formSchema: projectSummaryReportFormBySlug.jsonSchema.schema,
formData: projectSummaryReport?.newFormData,
}
: filteredSchema;
if ((allFormChangesPristine && !viewOnly) || !projectSummaryReport)
return (
<>
{!isOnAmendmentsAndOtherRevisionsPage && (
Expand All @@ -106,24 +124,11 @@ const ProjectSummaryReportFormSummary: React.FC<Props> = ({
</>
);

// Set the formSchema and formData based on showing the diff or not
const projectSummaryFormDiffObject = !renderDiff
? {
formSchema: projectSummaryReport.formByJsonSchemaName.jsonSchema.schema,
formData: projectSummaryReport.newFormData,
}
: getFilteredSchema(
projectSummaryReport.formByJsonSchemaName.jsonSchema
.schema as JSONSchema7,
projectSummaryReport || {}
);

// todo: not displaying when reviewing changes

return (
<>
{!isOnAmendmentsAndOtherRevisionsPage && <h3>Project Summary Report</h3>}
{allFormChangesPristine &&
!viewOnly &&
projectSummaryReport?.operation !== "ARCHIVE" && (
<FormNotAddedOrUpdated
isFirstRevision={isFirstRevision}
Expand Down
2 changes: 2 additions & 0 deletions app/pages/cif/project-revision/[projectRevision]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const EditProjectRevisionQuery = graphql`
...ProjectFundingAgreementFormSummary_query
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...ProjectEmissionIntensityReportFormSummary_query
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...ProjectSummaryReportFormSummary_query
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const pageQuery = graphql`
}
...ProjectFundingAgreementFormSummary_query
...ProjectEmissionIntensityReportFormSummary_query
...ProjectSummaryReportFormSummary_query

projectRevision(id: $projectRevision) {
id
changeStatus
Expand Down
2 changes: 2 additions & 0 deletions app/pages/cif/project-revision/[projectRevision]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const pageQuery = graphql`
}
...ProjectFundingAgreementFormSummary_query
...ProjectEmissionIntensityReportFormSummary_query
...ProjectSummaryReportFormSummary_query
}
}
`;
Expand Down Expand Up @@ -253,6 +254,7 @@ export function ProjectRevision({
{fundingStream == "IA" && (
<ProjectSummaryReportFormSummary
projectRevision={query.projectRevision}
query={query}
/>
)}
<ProjectAttachmentsFormSummary
Expand Down
2 changes: 2 additions & 0 deletions app/pages/cif/project-revision/[projectRevision]/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const ViewProjectRevisionQuery = graphql`
...ProjectFundingAgreementFormSummary_query
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...ProjectEmissionIntensityReportFormSummary_query
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...ProjectSummaryReportFormSummary_query
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import ProjectSummaryReportFormSummary from "components/Form/ProjectSummaryRepor
import compiledFormIndexPageQuery, {
FormIndexPageQuery,
} from "__generated__/FormIndexPageQuery.graphql";
import projectSummaryProdSchema from "../../../../../schema/data/prod/json_schema/project_summary_report.json";
import projectSummaryProdSchema from "/schema/data/prod/json_schema/emission_intensity.json";

const testQuery = graphql`
query ProjectSummaryReportFormSummaryQuery @relay_test_operation {
query {
projectRevision(id: "Test Project Revision ID") {
...ProjectSummaryReportFormSummary_projectRevision
}
...ProjectSummaryReportFormSummary_query
}
}
`;

const defaultMockResolver = {
Query() {
return {
projectSummaryReportFormBySlug: {
jsonSchema: projectSummaryProdSchema,
},
};
},
ProjectRevision() {
return {
id: "mock-id-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ begin;
create or replace function cif_private.handle_project_summary_report_form_change_commit(fc cif.form_change)
returns int as $$
declare
reporting_requirement_record_id int;
reporting_requirement_record_id int;
begin
-- If there is no change in the form data, return the form_change record and do not touch the associated table.
if (fc.new_form_data = '{}') then
return fc.form_data_record_id;
end if;

if (fc.change_status = 'committed') then
raise exception 'Cannot commit form_change. It has already been committed.';
end if;
-- If there is no change in the form data, return the form_change record and do not touch the associated table.
if (fc.new_form_data = '{}') then
return fc.form_data_record_id; -- can be null if creating with empty form data...problem?
end if;

if (fc.change_status = 'committed') then
raise exception 'Cannot commit form_change. It has already been committed.';
end if;

reporting_requirement_record_id := fc.form_data_record_id;

Comment on lines +21 to 22
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This line was missing and that's what created the bug. This function returns reporting_requirement_record_id, so if we don't assign here, then the update and archive cases return null.

if fc.operation = 'create' then
if fc.operation = 'create' then
insert into cif.reporting_requirement(
project_id,
report_type,
Expand Down
89 changes: 89 additions & 0 deletions schema/deploy/functions/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
-- Deploy cif:functions/handle_project_summary_report_form_change_commit to pg
-- requires: tables/form_change

begin;

create or replace function cif_private.handle_project_summary_report_form_change_commit(fc cif.form_change)
returns int as $$
declare
reporting_requirement_record_id int;
begin
-- If there is no change in the form data, return the form_change record and do not touch the associated table.
if (fc.new_form_data = '{}') then
return fc.form_data_record_id;
end if;

if (fc.change_status = 'committed') then
raise exception 'Cannot commit form_change. It has already been committed.';
end if;

if fc.operation = 'create' then
insert into cif.reporting_requirement(
project_id,
report_type,
report_due_date,
submitted_date,
comments,
reporting_requirement_index,
description
) values (
(select form_data_record_id from cif.form_change pfc where form_data_table_name = 'project' and pfc.project_revision_id = fc.project_revision_id),
(fc.new_form_data->>'reportType'),
(fc.new_form_data->>'reportDueDate')::timestamptz,
(fc.new_form_data->>'submittedDate')::timestamptz,
(fc.new_form_data->>'comments'),
(fc.new_form_data->>'reportingRequirementIndex')::int,
(fc.new_form_data->>'description')
) returning id into reporting_requirement_record_id;

insert into cif.payment(
reporting_requirement_id,
gross_amount,
net_amount,
date_sent_to_csnr
) values (
reporting_requirement_record_id,
(fc.new_form_data->>'projectSummaryReportPayment')::numeric,
(fc.new_form_data->>'projectSummaryReportPayment')::numeric,
(fc.new_form_data->>'dateSentToCsnr')::timestamptz
);

update cif.form_change set form_data_record_id = reporting_requirement_record_id where id = fc.id;

elsif fc.operation = 'update' then

update cif.reporting_requirement rr set
report_type = (fc.new_form_data->>'reportType'),
report_due_date = (fc.new_form_data->>'reportDueDate')::timestamptz,
submitted_date = (fc.new_form_data->>'submittedDate')::timestamptz,
comments = (fc.new_form_data->>'comments'),
reporting_requirement_index = (fc.new_form_data->>'reportingRequirementIndex')::int,
description = (fc.new_form_data->>'description')
where rr.id = fc.form_data_record_id;

update cif.payment py set
gross_amount = (fc.new_form_data->>'projectSummaryReportPayment')::numeric,
net_amount = (fc.new_form_data->>'projectSummaryReportPayment')::numeric,
date_sent_to_csnr = (fc.new_form_data->>'dateSentToCsnr')::timestamptz
where py.reporting_requirement_id = fc.form_data_record_id;

elsif fc.operation = 'archive' then

update cif.reporting_requirement set archived_at = now() where id = fc.form_data_record_id;
update cif.payment set archived_at = now() where reporting_requirement_id = fc.form_data_record_id;

end if;

return reporting_requirement_record_id;
end;
$$ language plpgsql volatile;

grant execute on function cif_private.handle_project_summary_report_form_change_commit to cif_internal, cif_external, cif_admin;

comment on function cif_private.handle_project_summary_report_form_change_commit
is $$
The custom function used to parse project summary form_change data into table data.
The data within the single form_change record is parsed into the reporting_requirement, and payment tables.
$$;

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Deploy cif:migration_rebuild_project_summary_report_history to pg

begin;

create or replace function cif_private.migration_rebuild_project_summary_report_history()
returns void as
$migration$

with records as (
select fc1.id as id1,fc1.previous_form_change_id as prfid, fc2.form_data_record_id as fdrid, fc2.id as id2
from cif.form_change as fc1
join cif.form_change as fc2
on fc1.previous_form_change_id=fc2.id
and fc1.json_schema_name='project_summary_report'
)

update cif.form_change fc set form_data_record_id=
(
select fdrid from records where fc.id=id1
)
where form_data_record_id is null
and change_status='committed'
and json_schema_name='project_summary_report';

$migration$ language sql volatile;

commit;
11 changes: 11 additions & 0 deletions schema/deploy/migrations/009_rebuild_project_summary_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Deploy cif:migrations/009_rebuild_project_summary_history to pg

begin;

alter table cif.form_change disable trigger _100_committed_changes_are_immutable, disable trigger _set_previous_form_change_id, disable trigger _100_timestamps;

select cif_private.migration_rebuild_project_summary_report_history();

alter table cif.form_change enable trigger _100_committed_changes_are_immutable, enable trigger _set_previous_form_change_id, enable trigger _100_timestamps;

commit;
Loading
Loading