diff --git a/schema/deploy/functions/handle_project_summary_report_form_change_commit.sql b/schema/deploy/functions/handle_project_summary_report_form_change_commit.sql index c1a2e35e49..43629aec50 100644 --- a/schema/deploy/functions/handle_project_summary_report_form_change_commit.sql +++ b/schema/deploy/functions/handle_project_summary_report_form_change_commit.sql @@ -48,8 +48,13 @@ begin (fc.new_form_data->>'dateSentToCsnr')::timestamptz ); +-- brianna is it this then? it sets it wrong? because it looks like it goes in right so it must be changed later. this is on create so that doesn't make sense + raise notice 'reporting_requirement_record_id: %', reporting_requirement_record_id; + 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 @@ -67,6 +72,10 @@ begin date_sent_to_csnr = (fc.new_form_data->>'dateSentToCsnr')::timestamptz where py.reporting_requirement_id = fc.form_data_record_id; + +-- brianna the first update doesn't get a form_data record id, I think it should, all the project ones have the same one + + elsif fc.operation = 'archive' then update cif.reporting_requirement set archived_at = now() where id = fc.form_data_record_id; diff --git a/schema/deploy/functions/raise_exception.sql b/schema/deploy/functions/raise_exception.sql index a721410851..59e9d3e7fc 100644 --- a/schema/deploy/functions/raise_exception.sql +++ b/schema/deploy/functions/raise_exception.sql @@ -2,7 +2,7 @@ begin; -create or replace function cif_private.raise_exception(text) returns text as +create or replace function cif_private.raise_exception(numeric) returns text as $$ begin raise exception '%',$1; diff --git a/schema/deploy/mutations/create_form_change.sql b/schema/deploy/mutations/create_form_change.sql index 8b2830f68d..5b98b6660e 100644 --- a/schema/deploy/mutations/create_form_change.sql +++ b/schema/deploy/mutations/create_form_change.sql @@ -2,7 +2,14 @@ begin; -create function cif.create_form_change( +create or replace function raise_notice (s numeric) language plpgsql as +$$ +begin + raise notice '%', s; +end; +$$; + +create or replace function cif.create_form_change( operation cif.form_change_operation, json_schema_name varchar(1000), form_data_schema_name varchar(1000), @@ -13,6 +20,10 @@ create function cif.create_form_change( change_status varchar(1000) default 'pending', validation_errors jsonb default '[]' ) returns cif.form_change as $create_form_change$ + + +cif_private.raise_exception(form_data_record_id); + insert into cif.form_change( new_form_data, operation, @@ -24,6 +35,7 @@ create function cif.create_form_change( json_schema_name, validation_errors ) values ( + -- brianna here it must be grabbing an old form change instead of the latest committed one coalesce(new_form_data, ( select new_form_data from cif.form_change @@ -37,6 +49,7 @@ create function cif.create_form_change( operation, form_data_schema_name, form_data_table_name, + -- or here? form_data_record_id, project_revision_id, change_status, diff --git a/schema/deploy/mutations/create_project_revision.sql b/schema/deploy/mutations/create_project_revision.sql index 66b230e149..7bcdce84c7 100644 --- a/schema/deploy/mutations/create_project_revision.sql +++ b/schema/deploy/mutations/create_project_revision.sql @@ -9,6 +9,7 @@ declare revision_row cif.project_revision; form_change_record record; _amendment_type varchar(1000); + brianna cif.form_change; begin insert into cif.project_revision(project_id, change_status, revision_type, revision_status) -- conditionally set revision_status to `In Discussion` if revision_type is Amendment otherwise set it to `Draft` @@ -160,12 +161,18 @@ begin operation => form_change_record.operation, form_data_schema_name => 'cif', form_data_table_name => form_change_record.form_data_table_name, + -- first update doesn't get this, I think it should cause other forms do form_data_record_id => form_change_record.id, project_revision_id => revision_row.id, json_schema_name => form_change_record.json_schema_name - ); + ) ; + raise notice '!!!!!!!!!!!!!form_change_record.operation:%',form_change_record.operation; + raise notice '!!!!!!!!!!!!!form_change_record.form_data_table_name:%',form_change_record.form_data_table_name; + raise notice '!!!!!!!!!!!!!revision_row.id:%',revision_row.id; + + raise notice '!!!!!!!!!!!!!form_change_record.json_schema_name:%',form_change_record.json_schema_name; + raise notice '!!!!!!!!!!!!!form_change_record.id:%',form_change_record.id; end loop; - return revision_row; end; $function$ language plpgsql strict; diff --git a/schema/deploy/trigger_functions/set_previous_form_change_id.sql b/schema/deploy/trigger_functions/set_previous_form_change_id.sql index 5e8f1d7050..4552088782 100644 --- a/schema/deploy/trigger_functions/set_previous_form_change_id.sql +++ b/schema/deploy/trigger_functions/set_previous_form_change_id.sql @@ -1,10 +1,15 @@ -- Deploy cif:trigger_functions/set_previous_form_change_id to pg -- requires: tables/form_change + +-- brianna it sets the project summary report previus form change id off the first one begin; create or replace function cif_private.set_previous_form_change_id() returns trigger as $$ begin + +raise notice 'new: %',new; + new.previous_form_change_id = ( select id from cif.form_change where form_data_schema_name = new.form_data_schema_name diff --git a/schema/test/unit/mutations/create_project_revision_test.sql b/schema/test/unit/mutations/create_project_revision_test.sql index 1cca324617..1ea96245d3 100644 --- a/schema/test/unit/mutations/create_project_revision_test.sql +++ b/schema/test/unit/mutations/create_project_revision_test.sql @@ -202,18 +202,18 @@ select results_eq( select results_eq( $$ - select new_form_data from cif.form_change + select form_data_record_id, previous_form_change_id, new_form_data from cif.form_change where form_data_table_name = 'project' and project_revision_id = 2 $$, $$ - select '{ + values(1,1, '{ "projectName": "name", "summary": "lorem ipsum", "fundingStreamRfpId": 1, "projectStatusId": 1, "proposalReference": "1235", "operatorId": 1 - }'::jsonb + }'::jsonb) $$, 'creating a new project revision should create a form_change record for the project' ); @@ -241,15 +241,18 @@ select is_empty( 'creating a new project revision should not create a form_change record for the project_contact' ); + + + select results_eq( $$ - select new_form_data, json_schema_name from cif.form_change + select form_data_record_id, previous_form_change_id, new_form_data, json_schema_name from cif.form_change where form_data_table_name = 'reporting_requirement' and project_revision_id = 2 order by json_schema_name $$, $$ values - ('{ + (3,5,'{ "projectId":1, "adjustedEmissionsIntensityPerformance": 98, "baselineEmissionIntensity": 324.25364, @@ -267,12 +270,13 @@ select results_eq( "targetEmissionIntensity": 23.2357, "totalLifetimeEmissionReduction": 44.4224 }'::jsonb, 'emission_intensity'::varchar), - ('{ + (1,3,'{ "projectId":1, "reportType": "General Milestone", "reportingRequirementIndex": 1 }'::jsonb, 'milestone'::varchar), - ('{ + -- brianna change this later to 4 I think + (null,6,'{ "projectId":1, "reportType": "Project Summary Report", "reportDueDate": "2021-08-31 14:24:46.318423-07", @@ -283,13 +287,13 @@ select results_eq( "paymentNotes": "payment notes", "dateSentToCsnr": "2021-08-29 14:24:46.318423-07" }'::jsonb, 'project_summary_report'::varchar), - ('{ + (2,4,'{ "projectId":1, "reportType": "Annual", "reportingRequirementIndex": 1 }'::jsonb, 'reporting_requirement'::varchar) $$, - 'creating a new project revision should create a form_change record for the reporting_requirement, for the milestone schema, generic reporting_requirement schema, emission_intensity and project_summary_report schemas' + 'creating a new project revision should create a form_change record for the reporting_requirement, for the milestone schema, generic reporting_requirement schema, emission_intensity and project_summary_report schemas' ); select results_eq( @@ -350,5 +354,23 @@ select lives_ok( 'allows creating a pending General Revision on a project with a pending Amendment' ); +select '-----------'; +select form_data_record_id, previous_form_change_id, new_form_data, json_schema_name from cif.form_change + where form_data_table_name = 'reporting_requirement' and project_revision_id = 2 + order by form_data_record_id; +select '-----------'; + +select '-----------'; +select form_data_record_id, previous_form_change_id, new_form_data + -- there is no form_data_record_id 4 for some reason + where form_data_record_id = 4 + and form_data_schema_name = 'cif' + and form_data_table_name = 'reporting_requirement' + and json_schema_name='project_summary_report' + and change_status = 'committed' + order by updated_at desc, id desc; + -- limit 1; +select '-----------'; + select finish(); rollback;