Skip to content

Commit

Permalink
fix: only fire trigger on change from draft to submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Jun 2, 2021
1 parent e73bfae commit 3a4f140
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ create or replace function ggircs_portal_private.ensure_window_open_submit_appli
returns trigger as $$
begin
if (select reporting_year from ggircs_portal.opened_reporting_year()) is null and new.version_number = 1 then
if (new.application_revision_status = 'submitted') then
if (new.application_revision_status = 'submitted')
and (select application_revision_status from ggircs_portal.application_revision_status where application_id = new.application_id order by id desc limit 1) = 'draft'
then
raise exception 'You cannot submit an application when the application window is closed';
end if;
if (new.application_revision_status = 'draft') then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Deploy ggircs-portal:trigger_functions/ensure_window_open_submit_application_status to pg

begin;

create or replace function ggircs_portal_private.ensure_window_open_submit_application_status()
returns trigger as $$
begin
if (select reporting_year from ggircs_portal.opened_reporting_year()) is null and new.version_number = 1 then
if (new.application_revision_status = 'submitted') then
raise exception 'You cannot submit an application when the application window is closed';
end if;
if (new.application_revision_status = 'draft') then
raise exception 'You cannot start a draft when the application window is closed';
end if;
end if;
return new;
end;
$$ language plpgsql;

grant execute on function ggircs_portal_private.ensure_window_open_submit_application_status to ciip_administrator, ciip_analyst, ciip_industry_user;

comment on function ggircs_portal_private.ensure_window_open_submit_application_status is 'a trigger function that throws an exception if the application window is not opened, the application version number is <= 1, and the new status is either "draft" or "submitted"';

commit;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ begin;
create or replace function ggircs_portal_private.ensure_window_open_submit_application_status()
returns trigger as $$
begin
if (select reporting_year from ggircs_portal.opened_reporting_year()) is null and new.version_number <= 1 then
if (select reporting_year from ggircs_portal.opened_reporting_year()) is null and new.version_number = 1 then
if (new.application_revision_status = 'submitted') then
raise exception 'You cannot submit an application when the application window is closed';
end if;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Deploy ggircs-portal:trigger_functions/ensure_window_open_submit_application_status to pg

begin;

create or replace function ggircs_portal_private.ensure_window_open_submit_application_status()
returns trigger as $$
begin
if (select reporting_year from ggircs_portal.opened_reporting_year()) is null and new.version_number <= 1 then
if (new.application_revision_status = 'submitted') then
raise exception 'You cannot submit an application when the application window is closed';
end if;
if (new.application_revision_status = 'draft') then
raise exception 'You cannot start a draft when the application window is closed';
end if;
end if;
return new;
end;
$$ language plpgsql;

grant execute on function ggircs_portal_private.ensure_window_open_submit_application_status to ciip_administrator, ciip_analyst, ciip_industry_user;

comment on function ggircs_portal_private.ensure_window_open_submit_application_status is 'a trigger function that throws an exception if the application window is not opened, the application version number is <= 1, and the new status is either "draft" or "submitted"';

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,4 @@ application_validation_functions/carbon_taxed_fuels_match_rev_zero [application_
trigger_functions/create_or_refresh_review_step [trigger_functions/create_or_refresh_review_step@v2.6.0] 2021-05-27T23:02:19Z Dylan Leard <dylan@button.is> # Migration: ignore version 0 when creating review steps for an application
tables/application_review_step_001 [tables/application_review_step] 2021-05-27T23:14:00Z Dylan Leard <dylan@button.is> # Migration: remove erroneously created application_review_steps for 2018 applications
queries/facility_application_by_reporting_year [queries/facility_application_by_reporting_year@v2.6.0] 2021-05-27T18:02:27Z Matthieu Foucault <matthieu@button.is> # fix order by clause
trigger_functions/ensure_window_open_submit_application_status [trigger_functions/ensure_window_open_submit_application_status@v2.6.0] 2021-05-28T17:01:41Z Dylan Leard <dylan@button.is> # Migration: only fire trigger on change from draft to submitted
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:trigger_functions/ensure_window_open_submit_application_status on pg

begin;

select pg_get_functiondef('ggircs_portal_private.ensure_window_open_submit_application_status()'::regprocedure);

rollback;

0 comments on commit 3a4f140

Please sign in to comment.