From 7376728d9ee69a52b9b6e62aa4ac4416fe22f5eb Mon Sep 17 00:00:00 2001 From: R Ranathunga Date: Fri, 20 Dec 2024 13:34:07 -0800 Subject: [PATCH] fix: prevent extra row in history when saving dependencies/assessment --- .../Analyst/Assessments/AssessmentsForm.tsx | 71 +++++++++++++++++-- .../Analyst/History/HistoryContent.tsx | 2 +- app/package.json | 2 + .../mutations/assessment/createAssessment.ts | 30 ++++---- app/schema/schema.graphql | 29 ++------ .../assessments/financial-risk.test.tsx | 1 + .../[applicationId]/assessments/gis.test.tsx | 1 + .../assessments/permitting.test.tsx | 1 + .../assessments/project-management.test.tsx | 1 + .../assessments/screening.test.tsx | 3 + .../assessments/technical.test.tsx | 3 +- app/yarn.lock | 7 ++ .../mutations/create_assessment_form.sql | 49 ++++++------- .../create_assessment_form@1.217.0.sql | 70 ++++++++++++++++++ db/deploy/types/assessment_form_result.sql | 9 +++ .../mutations/create_assessment_form.sql | 35 ++++++++- .../create_assessment_form@1.217.0.sql | 37 ++++++++++ db/revert/types/assessment_form_result.sql | 6 ++ db/sqitch.plan | 2 + 19 files changed, 287 insertions(+), 72 deletions(-) create mode 100644 db/deploy/mutations/create_assessment_form@1.217.0.sql create mode 100644 db/deploy/types/assessment_form_result.sql create mode 100644 db/revert/mutations/create_assessment_form@1.217.0.sql create mode 100644 db/revert/types/assessment_form_result.sql diff --git a/app/components/Analyst/Assessments/AssessmentsForm.tsx b/app/components/Analyst/Assessments/AssessmentsForm.tsx index 953fbb4228..f657c980b5 100644 --- a/app/components/Analyst/Assessments/AssessmentsForm.tsx +++ b/app/components/Analyst/Assessments/AssessmentsForm.tsx @@ -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; @@ -70,14 +71,52 @@ const AssessmentsForm: React.FC = ({ >('idle'); const formRef = useRef(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) => { + 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: [], }, @@ -90,11 +129,29 @@ const AssessmentsForm: React.FC = ({ }, 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 } + ); + } }, }); } diff --git a/app/components/Analyst/History/HistoryContent.tsx b/app/components/Analyst/History/HistoryContent.tsx index aeb7b853c7..fdf68775a9 100644 --- a/app/components/Analyst/History/HistoryContent.tsx +++ b/app/components/Analyst/History/HistoryContent.tsx @@ -399,7 +399,7 @@ const HistoryContent = ({ <> - {user} updated the Technical Assessment : on{' '} + {user} updated the Technical Assessment on{' '} {createdAtFormatted} diff --git a/app/package.json b/app/package.json index 252868aec1..6a81cfdcdb 100644 --- a/app/package.json +++ b/app/package.json @@ -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", @@ -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", diff --git a/app/schema/mutations/assessment/createAssessment.ts b/app/schema/mutations/assessment/createAssessment.ts index c3feec483c..7a00b3b533 100644 --- a/app/schema/mutations/assessment/createAssessment.ts +++ b/app/schema/mutations/assessment/createAssessment.ts @@ -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 + } } } } diff --git a/app/schema/schema.graphql b/app/schema/schema.graphql index e6568809e8..0923af0941 100644 --- a/app/schema/schema.graphql +++ b/app/schema/schema.graphql @@ -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.""" @@ -97329,6 +97311,7 @@ input CreateAssessmentFormInput { _assessmentType: String! _jsonData: JSON! _applicationId: Int! + _dependenciesData: JSON } """The output of our `createCbcPendingChangeRequest` mutation.""" diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/financial-risk.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/financial-risk.test.tsx index bf00c15f65..08782598ec 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/financial-risk.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/financial-risk.test.tsx @@ -102,6 +102,7 @@ describe('The index page', () => { nextStep: 'Not started', decision: 'Low risk', }, + _dependenciesData: null, _assessmentType: 'financialRisk', }, connections: [], diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/gis.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/gis.test.tsx index e216fe8f68..3cfbeb538c 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/gis.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/gis.test.tsx @@ -156,6 +156,7 @@ describe('The index page', () => { commentsOnOverbuild: 'test 3', commentsOnOverlap: 'test 4', }, + _dependenciesData: null, _assessmentType: 'gis', }, connections: [], diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/permitting.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/permitting.test.tsx index f5f2af9b1f..2505e97eb2 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/permitting.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/permitting.test.tsx @@ -112,6 +112,7 @@ describe('The index page', () => { 'Normal permitting requirements and timelines anticipated.', ], }, + _dependenciesData: null, _assessmentType: 'permitting', }, connections: [], diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/project-management.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/project-management.test.tsx index 51fac2e0df..c70af200d5 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/project-management.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/project-management.test.tsx @@ -101,6 +101,7 @@ describe('The index page', () => { _jsonData: { nextStep: 'Needs 2nd review', }, + _dependenciesData: null, _assessmentType: 'projectManagement', }, connections: [], diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/screening.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/screening.test.tsx index 8d774a24b7..cb78d2fef6 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/screening.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/screening.test.tsx @@ -168,6 +168,7 @@ describe('The index page', () => { decision: 'Eligible', contestingMap: [], }, + _dependenciesData: null, _assessmentType: 'screening', }, connections: [], @@ -199,6 +200,7 @@ describe('The index page', () => { decision: 'No decision', contestingMap: [], }, + _dependenciesData: null, _assessmentType: 'screening', }, connections: [], @@ -256,6 +258,7 @@ describe('The index page', () => { decision: 'No decision', contestingMap: [], }, + _dependenciesData: null, _assessmentType: 'screening', }, connections: [], diff --git a/app/tests/pages/analyst/application/[applicationId]/assessments/technical.test.tsx b/app/tests/pages/analyst/application/[applicationId]/assessments/technical.test.tsx index c422145306..b291587300 100644 --- a/app/tests/pages/analyst/application/[applicationId]/assessments/technical.test.tsx +++ b/app/tests/pages/analyst/application/[applicationId]/assessments/technical.test.tsx @@ -113,9 +113,8 @@ describe('The index page', () => { _jsonData: { nextStep: 'Not started', decision: 'Pass', - connectedCoastNetworkDependent: 'TBD', - crtcProjectDependent: 'TBD', }, + _dependenciesData: null, _assessmentType: 'technical', }, connections: [], diff --git a/app/yarn.lock b/app/yarn.lock index 99a880a181..d3b9dae5b7 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -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/lodash.template@4.5.0": version "4.5.0" resolved "https://registry.yarnpkg.com/@types/lodash.template/-/lodash.template-4.5.0.tgz#277654af717ed37ce2687c69f8f221c550276b7a" diff --git a/db/deploy/mutations/create_assessment_form.sql b/db/deploy/mutations/create_assessment_form.sql index 4de914fbc3..e6e705df83 100644 --- a/db/deploy/mutations/create_assessment_form.sql +++ b/db/deploy/mutations/create_assessment_form.sql @@ -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 @@ -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; @@ -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; diff --git a/db/deploy/mutations/create_assessment_form@1.217.0.sql b/db/deploy/mutations/create_assessment_form@1.217.0.sql new file mode 100644 index 0000000000..4de914fbc3 --- /dev/null +++ b/db/deploy/mutations/create_assessment_form@1.217.0.sql @@ -0,0 +1,70 @@ +-- deploy ccbc:mutations/create_assessment_form to pg + +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 $$ +declare +new_assessment_data_id int; +old_assessment_data_id int; +crtc_project_dependent jsonb; +connected_coast_network_dependent jsonb; +existing_json_data jsonb; +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'; + + -- Check if the dependency data already exists + select json_data + into existing_json_data + from ccbc_public.application_dependencies + where application_id = _application_id; + + 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, + updated_at = now(), + reason_for_change = '' + where application_id = _application_id; + else + -- Insert new json_data with the extracted fields + insert into ccbc_public.application_dependencies ( + application_id, json_data, created_at + ) values ( + _application_id, crtc_project_dependent || connected_coast_network_dependent, 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 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; + + return (select row(ccbc_public.assessment_data.*) from ccbc_public.assessment_data where id = new_assessment_data_id); +end; +$$ language plpgsql volatile; + +grant execute on function ccbc_public.create_assessment_form to ccbc_analyst; + +grant execute on function ccbc_public.create_assessment_form to ccbc_admin; + +commit; diff --git a/db/deploy/types/assessment_form_result.sql b/db/deploy/types/assessment_form_result.sql new file mode 100644 index 0000000000..ebf4e6aafa --- /dev/null +++ b/db/deploy/types/assessment_form_result.sql @@ -0,0 +1,9 @@ +-- Deploy ccbc:types/assessment_form_result to pg +BEGIN; + +CREATE TYPE ccbc_public.assessment_form_result AS ( + assessment_data ccbc_public.assessment_data, + application_dependencies ccbc_public.application_dependencies +); + +COMMIT; diff --git a/db/revert/mutations/create_assessment_form.sql b/db/revert/mutations/create_assessment_form.sql index 1baa7f1c2c..c1d859f94e 100644 --- a/db/revert/mutations/create_assessment_form.sql +++ b/db/revert/mutations/create_assessment_form.sql @@ -2,12 +2,15 @@ begin; -drop function ccbc_public.create_assessment_form(varchar , jsonb , integer); +drop function ccbc_public.create_assessment_form(varchar , jsonb , integer, jsonb); create or replace function ccbc_public.create_assessment_form(_assessment_type varchar, _json_data jsonb, _application_id int) returns ccbc_public.assessment_data 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; 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 @@ -19,6 +22,36 @@ begin 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'; + + -- Check if the dependency data already exists + select json_data + into existing_json_data + from ccbc_public.application_dependencies + where application_id = _application_id; + + 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, + updated_at = now(), + reason_for_change = '' + where application_id = _application_id; + else + -- Insert new json_data with the extracted fields + insert into ccbc_public.application_dependencies ( + application_id, json_data, created_at + ) values ( + _application_id, crtc_project_dependent || connected_coast_network_dependent, 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; diff --git a/db/revert/mutations/create_assessment_form@1.217.0.sql b/db/revert/mutations/create_assessment_form@1.217.0.sql new file mode 100644 index 0000000000..1baa7f1c2c --- /dev/null +++ b/db/revert/mutations/create_assessment_form@1.217.0.sql @@ -0,0 +1,37 @@ +-- deploy ccbc:mutations/create_assessment_form to pg + +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 $$ +declare +new_assessment_data_id int; +old_assessment_data_id int; +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; + + 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; + end if; + + return (select row(ccbc_public.assessment_data.*) from ccbc_public.assessment_data where id = new_assessment_data_id); +end; +$$ language plpgsql volatile; + +grant execute on function ccbc_public.create_assessment_form to ccbc_analyst; + +grant execute on function ccbc_public.create_assessment_form to ccbc_admin; + +commit; diff --git a/db/revert/types/assessment_form_result.sql b/db/revert/types/assessment_form_result.sql new file mode 100644 index 0000000000..bdafc5b4fe --- /dev/null +++ b/db/revert/types/assessment_form_result.sql @@ -0,0 +1,6 @@ +-- Revert ccbc:types/assessment_form_result from pg +BEGIN; + +DROP TYPE IF EXISTS ccbc_public.assessment_form_result; + +COMMIT; diff --git a/db/sqitch.plan b/db/sqitch.plan index 01e711857d..b93fdb858f 100644 --- a/db/sqitch.plan +++ b/db/sqitch.plan @@ -768,3 +768,5 @@ computed_columns/application_history [computed_columns/application_history@1.213 @1.218.1 2024-12-18T17:53:16Z CCBC Service Account # release v1.218.1 @1.218.2 2024-12-19T16:29:04Z CCBC Service Account # release v1.218.2 @1.218.3 2024-12-24T20:06:08Z CCBC Service Account # release v1.218.3 +types/assessment_form_result 2024-12-20T14:51:59Z ,,, # add assessment_form_result type to get assessment and dependencies together +mutations/create_assessment_form [mutations/create_assessment_form@1.217.0] 2024-12-20T15:09:15Z ,,, # update xx