Skip to content

Commit

Permalink
fix: prevent extra row in history when saving dependencies/assessment
Browse files Browse the repository at this point in the history
  • Loading branch information
RRanath committed Dec 30, 2024
1 parent 394acf4 commit 7376728
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 72 deletions.
71 changes: 64 additions & 7 deletions app/components/Analyst/Assessments/AssessmentsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RJSFSchema } from '@rjsf/utils';
import * as Sentry from '@sentry/nextjs';
import { useToast } from 'components/AppProvider';
import { FormBaseRef } from 'components/Form/FormBase';
import isEqual from 'lodash.isequal';

interface Props {
addedContext?: any;
Expand Down Expand Up @@ -70,14 +71,52 @@ const AssessmentsForm: React.FC<Props> = ({
>('idle');
const formRef = useRef<FormBaseRef>(null);

const getDependenciesData = (data: any) => {
const newDependencies = {
connectedCoastNetworkDependent: data?.connectedCoastNetworkDependent,
crtcProjectDependent: data?.crtcProjectDependent,
};
const oldDependencies = {
connectedCoastNetworkDependent: formData?.connectedCoastNetworkDependent,
crtcProjectDependent: formData?.crtcProjectDependent,
};

if (!isEqual(newDependencies, oldDependencies)) {
return newDependencies;
}
return null;
};

const handleSubmit = async (e: IChangeEvent<any>) => {
let hasTechnicalAssessmentChanged = false;
const newAssessmentData = { ...e.formData };
if (!isFormSaved) {
// Remove the fields that are not part of the assessment data
// for dependencies handling and saving only what is needed
if (slug === 'technical') {
delete newAssessmentData.connectedCoastNetworkDependent;
delete newAssessmentData.crtcProjectDependent;

hasTechnicalAssessmentChanged = !isEqual(
newAssessmentData,
(({
connectedCoastNetworkDependent,
crtcProjectDependent,
...assessment
}) => assessment)(formData)
);
}
const dependenciesData = getDependenciesData(e.formData);
createAssessment({
variables: {
input: {
_applicationId: queryFragment.rowId,
_jsonData: e.formData,
_assessmentType: slug,
_jsonData: newAssessmentData,
_dependenciesData: dependenciesData,
_assessmentType:
slug === 'technical' && !hasTechnicalAssessmentChanged
? 'dependencies'
: slug,
},
connections: [],
},
Expand All @@ -90,11 +129,29 @@ const AssessmentsForm: React.FC<Props> = ({
},
updater: (store, data) => {
const application = store.get(queryFragment.id);
application.setLinkedRecord(
store.get(data.createAssessmentForm.assessmentData.id),
'assessmentForm',
{ _assessmentDataType: slug }
);
if (data.createAssessmentForm.assessmentFormResult?.assessmentData) {
application.setLinkedRecord(
store.get(
data.createAssessmentForm.assessmentFormResult?.assessmentData
.id
),
'assessmentForm',
{ _assessmentDataType: slug }
);
}
if (
data.createAssessmentForm.assessmentFormResult
?.applicationDependencies
) {
application.setLinkedRecord(
store.get(
data.createAssessmentForm.assessmentFormResult
.applicationDependencies.id
),
'applicationDependenciesByApplicationId',
{ _applicationId: queryFragment.rowId }
);
}
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/components/Analyst/History/HistoryContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ const HistoryContent = ({
<>
<StyledContent data-testid="history-content-dependencies">
<span>
{user} updated the <b>Technical Assessment :</b> on{' '}
{user} updated the <b>Technical Assessment</b> on{' '}
{createdAtFormatted}
</span>
</StyledContent>
Expand Down
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"jsonlint": "^1.6.3",
"jsonwebtoken": "^9.0.2",
"lightship": "7.2.0",
"lodash.isequal": "^4.5.0",
"luxon": "^3.5.0",
"material-react-table": "2.13",
"morgan": "^1.10.0",
Expand Down Expand Up @@ -131,6 +132,7 @@
"@types/express": "^4.17.21",
"@types/jest": "^28.1.6",
"@types/json-diff": "^1.0.3",
"@types/lodash.isequal": "^4.5.8",
"@types/luxon": "^3.4.2",
"@types/node": "20.14.14",
"@types/react": "18.3.12",
Expand Down
30 changes: 18 additions & 12 deletions app/schema/mutations/assessment/createAssessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ const mutation = graphql`
$connections: [ID!]!
) {
createAssessmentForm(input: $input) {
assessmentData
@prependNode(
connections: $connections
edgeTypeName: "AssessmentDataEdge"
) {
id
rowId
jsonData
createdAt
updatedAt
updatedBy
assessmentDataType
assessmentFormResult {
assessmentData
@prependNode(
connections: $connections
edgeTypeName: "AssessmentDataEdge"
) {
id
rowId
jsonData
createdAt
updatedAt
updatedBy
assessmentDataType
}
applicationDependencies {
id
jsonData
}
}
}
}
Expand Down
29 changes: 6 additions & 23 deletions app/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -97288,35 +97288,17 @@ type CreateAssessmentFormPayload {
unchanged and unused. May be used by a client to track mutations.
"""
clientMutationId: String
assessmentData: AssessmentData
assessmentFormResult: AssessmentFormResult

"""
Our root query field type. Allows us to run any query from our mutation payload.
"""
query: Query
}

"""Reads a single `Application` that is related to this `AssessmentData`."""
applicationByApplicationId: Application

"""
Reads a single `AssessmentType` that is related to this `AssessmentData`.
"""
assessmentTypeByAssessmentDataType: AssessmentType

"""Reads a single `CcbcUser` that is related to this `AssessmentData`."""
ccbcUserByCreatedBy: CcbcUser

"""Reads a single `CcbcUser` that is related to this `AssessmentData`."""
ccbcUserByUpdatedBy: CcbcUser

"""Reads a single `CcbcUser` that is related to this `AssessmentData`."""
ccbcUserByArchivedBy: CcbcUser

"""An edge for our `AssessmentData`. May be used by Relay 1."""
assessmentDataEdge(
"""The method to use when ordering `AssessmentData`."""
orderBy: [AssessmentDataOrderBy!] = [PRIMARY_KEY_ASC]
): AssessmentDataEdge
type AssessmentFormResult {
assessmentData: AssessmentData
applicationDependencies: ApplicationDependency
}

"""All input for the `createAssessmentForm` mutation."""
Expand All @@ -97329,6 +97311,7 @@ input CreateAssessmentFormInput {
_assessmentType: String!
_jsonData: JSON!
_applicationId: Int!
_dependenciesData: JSON
}

"""The output of our `createCbcPendingChangeRequest` mutation."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('The index page', () => {
nextStep: 'Not started',
decision: 'Low risk',
},
_dependenciesData: null,
_assessmentType: 'financialRisk',
},
connections: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('The index page', () => {
commentsOnOverbuild: 'test 3',
commentsOnOverlap: 'test 4',
},
_dependenciesData: null,
_assessmentType: 'gis',
},
connections: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('The index page', () => {
'Normal permitting requirements and timelines anticipated.',
],
},
_dependenciesData: null,
_assessmentType: 'permitting',
},
connections: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('The index page', () => {
_jsonData: {
nextStep: 'Needs 2nd review',
},
_dependenciesData: null,
_assessmentType: 'projectManagement',
},
connections: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('The index page', () => {
decision: 'Eligible',
contestingMap: [],
},
_dependenciesData: null,
_assessmentType: 'screening',
},
connections: [],
Expand Down Expand Up @@ -199,6 +200,7 @@ describe('The index page', () => {
decision: 'No decision',
contestingMap: [],
},
_dependenciesData: null,
_assessmentType: 'screening',
},
connections: [],
Expand Down Expand Up @@ -256,6 +258,7 @@ describe('The index page', () => {
decision: 'No decision',
contestingMap: [],
},
_dependenciesData: null,
_assessmentType: 'screening',
},
connections: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ describe('The index page', () => {
_jsonData: {
nextStep: 'Not started',
decision: 'Pass',
connectedCoastNetworkDependent: 'TBD',
crtcProjectDependent: 'TBD',
},
_dependenciesData: null,
_assessmentType: 'technical',
},
connections: [],
Expand Down
7 changes: 7 additions & 0 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4033,6 +4033,13 @@
dependencies:
"@types/node" "*"

"@types/lodash.isequal@^4.5.8":
version "4.5.8"
resolved "https://registry.yarnpkg.com/@types/lodash.isequal/-/lodash.isequal-4.5.8.tgz#b30bb6ff6a5f6c19b3daf389d649ac7f7a250499"
integrity sha512-uput6pg4E/tj2LGxCZo9+y27JNyB2OZuuI/T5F+ylVDYuqICLG2/ktjxx0v6GvVntAf8TvEzeQLcV0ffRirXuA==
dependencies:
"@types/lodash" "*"

"@types/[email protected]":
version "4.5.0"
resolved "https://registry.yarnpkg.com/@types/lodash.template/-/lodash.template-4.5.0.tgz#277654af717ed37ce2687c69f8f221c550276b7a"
Expand Down
49 changes: 23 additions & 26 deletions db/deploy/mutations/create_assessment_form.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,19 @@ begin;

drop function ccbc_public.create_assessment_form(varchar , jsonb , integer);

create or replace function ccbc_public.create_assessment_form(_assessment_type varchar, _json_data jsonb, _application_id int) returns ccbc_public.assessment_data as $$
create or replace function ccbc_public.create_assessment_form(_assessment_type varchar, _json_data jsonb, _application_id int, _dependencies_data jsonb default null) returns ccbc_public.assessment_form_result as $$
declare
new_assessment_data_id int;
old_assessment_data_id int;
crtc_project_dependent jsonb;
connected_coast_network_dependent jsonb;
existing_json_data jsonb;
new_assessment_data_id int;
old_assessment_data_id int;
existing_json_data jsonb;
assessment_data_row ccbc_public.assessment_data;
application_dependencies_row ccbc_public.application_dependencies;
begin

select ad.id into old_assessment_data_id from ccbc_public.assessment_data as ad where ad.assessment_data_type = _assessment_type and ad.application_id = _application_id
order by ad.id desc limit 1;



if (select name from ccbc_public.assessment_type where name = _assessment_type) is null then
raise exception 'There is no assessment with slug %', schema_slug;
end if;

-- If the assessment type is 'technical', remove specific fields and handle dependencies
if _assessment_type = 'technical' then
-- Extract the values of the fields and remove them from _json_data
crtc_project_dependent := jsonb_build_object('crtcProjectDependent', _json_data -> 'crtcProjectDependent');
connected_coast_network_dependent := jsonb_build_object('connectedCoastNetworkDependent', _json_data -> 'connectedCoastNetworkDependent');
_json_data := _json_data - 'crtcProjectDependent' - 'connectedCoastNetworkDependent';

if _dependencies_data is not null then
-- Check if the dependency data already exists
select json_data
into existing_json_data
Expand All @@ -38,7 +26,7 @@ begin
if existing_json_data is not null then
-- Update the existing json_data with new fields
update ccbc_public.application_dependencies
set json_data = existing_json_data || crtc_project_dependent || connected_coast_network_dependent,
set json_data = _dependencies_data,
updated_at = now(),
reason_for_change = ''
where application_id = _application_id;
Expand All @@ -47,19 +35,28 @@ begin
insert into ccbc_public.application_dependencies (
application_id, json_data, created_at
) values (
_application_id, crtc_project_dependent || connected_coast_network_dependent, now()
_application_id, _dependencies_data, now()
);
end if;
end if;

insert into ccbc_public.assessment_data (json_data, assessment_data_type, application_id)
values ( _json_data, _assessment_type, _application_id) returning id into new_assessment_data_id;
if _assessment_type != 'dependencies' then
if (select name from ccbc_public.assessment_type where name = _assessment_type) is null then
raise exception 'There is no assessment with slug %', schema_slug;
end if;

insert into ccbc_public.assessment_data (json_data, assessment_data_type, application_id)
values ( _json_data, _assessment_type, _application_id) returning id into new_assessment_data_id;

if exists (select * from ccbc_public.assessment_data where id = old_assessment_data_id)
then update ccbc_public.assessment_data set archived_at = now() where id = old_assessment_data_id;
if exists (select * from ccbc_public.assessment_data where id = old_assessment_data_id)
then update ccbc_public.assessment_data set archived_at = now() where id = old_assessment_data_id;
end if;
end if;

return (select row(ccbc_public.assessment_data.*) from ccbc_public.assessment_data where id = new_assessment_data_id);
select * into assessment_data_row from ccbc_public.assessment_data where id = COALESCE(new_assessment_data_id, old_assessment_data_id);
select * into application_dependencies_row from ccbc_public.application_dependencies where application_id = _application_id;

return (assessment_data_row, application_dependencies_row);
end;
$$ language plpgsql volatile;

Expand Down
Loading

0 comments on commit 7376728

Please sign in to comment.