Skip to content

Commit

Permalink
Merge pull request #2095 from bcgov/2083-deleted-intake-bug
Browse files Browse the repository at this point in the history
fix: return only non archived intakes from next_intake and open_intake function
  • Loading branch information
marcellmueller authored Aug 14, 2023
2 parents ebd2ccd + 143dfeb commit eaddabb
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 33 deletions.
1 change: 1 addition & 0 deletions db/deploy/functions/next_intake.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ as $function$
select *
from ccbc_public.intake
where now() < open_timestamp
and archived_at is null
order by ccbc_intake_number
limit 1;
$function$ language sql stable;
Expand Down
19 changes: 19 additions & 0 deletions db/deploy/functions/next_intake@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Deploy ccbc:functions/next_intake to pg

begin;

create or replace function ccbc_public.next_intake()
returns ccbc_public.intake
as $function$
select *
from ccbc_public.intake
where now() < open_timestamp
order by ccbc_intake_number
limit 1;
$function$ language sql stable;

grant execute on function ccbc_public.next_intake to ccbc_auth_user, ccbc_guest, ccbc_admin, ccbc_analyst;

comment on function ccbc_public.next_intake is 'Returns the next intake if any';

commit;
4 changes: 3 additions & 1 deletion db/deploy/functions/open_intake.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ BEGIN;

create or replace function ccbc_public.open_intake() returns ccbc_public.intake as
$function$
select * from ccbc_public.intake where now() >= open_timestamp and now() <= close_timestamp;
select * from ccbc_public.intake
where now() >= open_timestamp and now() <= close_timestamp
and archived_at is null;
$function$ language sql stable;

grant execute on function ccbc_public.open_intake to ccbc_guest, ccbc_auth_user, ccbc_admin, ccbc_analyst;
Expand Down
14 changes: 14 additions & 0 deletions db/deploy/functions/open_intake@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Deploy ccbc:functions/open_intake to pg

BEGIN;

create or replace function ccbc_public.open_intake() returns ccbc_public.intake as
$function$
select * from ccbc_public.intake where now() >= open_timestamp and now() <= close_timestamp;
$function$ language sql stable;

grant execute on function ccbc_public.open_intake to ccbc_guest, ccbc_auth_user, ccbc_admin, ccbc_analyst;

comment on function ccbc_public.open_intake is 'Returns the current open intake';

COMMIT;
4 changes: 2 additions & 2 deletions db/deploy/functions/receive_applications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ $function$
begin
select count(*) into cnt from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);
select id from ccbc_public.intake where now() >= close_timestamp and archived_at is null);

open applications for select id, intake_id
from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);
select id from ccbc_public.intake where now() >= close_timestamp and archived_at is null);

loop
fetch applications into current_app;
Expand Down
46 changes: 46 additions & 0 deletions db/deploy/functions/receive_applications@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Deploy ccbc:functions/receive_applications to pg

BEGIN;

create or replace function ccbc_public.receive_applications() returns table(result_id int) as
$function$
declare
current_app ccbc_public.application%ROWTYPE;
last_status text;
cnt int;
applications refcursor;

begin
select count(*) into cnt from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);

open applications for select id, intake_id
from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);

loop
fetch applications into current_app;
exit when not found;

select ccbc_public.application_status(current_app) into last_status;

if last_status = 'submitted' then
insert into ccbc_public.application_status (application_id, status)
values (current_app.id,'received');
end if;
end loop;

close applications;

return query select 0 as result_id;
end;
$function$ language plpgsql volatile;

grant execute on function ccbc_public.receive_applications to ccbc_job_executor;
revoke execute on function ccbc_public.receive_applications from ccbc_auth_user;

comment on function ccbc_public.receive_applications is 'Detects closed intake and marks all submitted applications as Received';

COMMIT;
2 changes: 1 addition & 1 deletion db/revert/functions/next_intake.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ as $function$
limit 1;
$function$ language sql stable;

grant execute on function ccbc_public.next_intake to ccbc_auth_user, ccbc_guest;
grant execute on function ccbc_public.next_intake to ccbc_auth_user, ccbc_guest, ccbc_admin, ccbc_analyst;

comment on function ccbc_public.next_intake is 'Returns the next intake if any';

Expand Down
19 changes: 19 additions & 0 deletions db/revert/functions/next_intake@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Deploy ccbc:functions/next_intake to pg

begin;

create or replace function ccbc_public.next_intake()
returns ccbc_public.intake
as $function$
select *
from ccbc_public.intake
where now() < open_timestamp
order by ccbc_intake_number
limit 1;
$function$ language sql stable;

grant execute on function ccbc_public.next_intake to ccbc_auth_user, ccbc_guest;

comment on function ccbc_public.next_intake is 'Returns the next intake if any';

commit;
2 changes: 1 addition & 1 deletion db/revert/functions/open_intake.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $function$
select * from ccbc_public.intake where now() >= open_timestamp and now() <= close_timestamp;
$function$ language sql stable;

grant execute on function ccbc_public.open_intake to ccbc_guest, ccbc_auth_user;
grant execute on function ccbc_public.open_intake to ccbc_guest, ccbc_auth_user, ccbc_admin, ccbc_analyst;

comment on function ccbc_public.open_intake is 'Returns the current open intake';

Expand Down
14 changes: 14 additions & 0 deletions db/revert/functions/open_intake@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Deploy ccbc:functions/open_intake to pg

BEGIN;

create or replace function ccbc_public.open_intake() returns ccbc_public.intake as
$function$
select * from ccbc_public.intake where now() >= open_timestamp and now() <= close_timestamp;
$function$ language sql stable;

grant execute on function ccbc_public.open_intake to ccbc_guest, ccbc_auth_user;

comment on function ccbc_public.open_intake is 'Returns the current open intake';

COMMIT;
4 changes: 2 additions & 2 deletions db/revert/functions/receive_applications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ $function$
end;
$function$ language plpgsql volatile;

grant execute on function ccbc_public.receive_applications to ccbc_auth_user;
revoke execute on function ccbc_public.receive_applications from ccbc_job_executor;
grant execute on function ccbc_public.receive_applications to ccbc_job_executor;
revoke execute on function ccbc_public.receive_applications from ccbc_auth_user;

comment on function ccbc_public.receive_applications is 'Detects closed intake and marks all submitted applications as Received';

Expand Down
46 changes: 46 additions & 0 deletions db/revert/functions/receive_applications@1.94.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Deploy ccbc:functions/receive_applications to pg

BEGIN;

create or replace function ccbc_public.receive_applications() returns table(result_id int) as
$function$
declare
current_app ccbc_public.application%ROWTYPE;
last_status text;
cnt int;
applications refcursor;

begin
select count(*) into cnt from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);

open applications for select id, intake_id
from ccbc_public.application
where intake_id in (
select id from ccbc_public.intake where now() >= close_timestamp);

loop
fetch applications into current_app;
exit when not found;

select ccbc_public.application_status(current_app) into last_status;

if last_status = 'submitted' then
insert into ccbc_public.application_status (application_id, status)
values (current_app.id,'received');
end if;
end loop;

close applications;

return query select 0 as result_id;
end;
$function$ language plpgsql volatile;

grant execute on function ccbc_public.receive_applications to ccbc_auth_user;
revoke execute on function ccbc_public.receive_applications from ccbc_job_executor;

comment on function ccbc_public.receive_applications is 'Detects closed intake and marks all submitted applications as Received';

COMMIT;
3 changes: 3 additions & 0 deletions db/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,6 @@ tables/application_community_progress_report_data_002.sql 2023-08-09T22:24:14Z M
mutations/archive_application_community_progress_report 2023-08-10T17:18:29Z Marcel Mueller <marcel@m2> # mutations to archive community progress report form and excel data
@1.94.0 2023-08-11T19:28:01Z CCBC Service Account <ccbc@button.is> # release v1.94.0
@1.94.1 2023-08-11T22:08:15Z CCBC Service Account <ccbc@button.is> # release v1.94.1
functions/next_intake [functions/next_intake@1.94.0] 2023-08-14T20:10:54Z Marcel Mueller <marcel@m2> # return only intakes where archived_at is null
functions/open_intake [functions/open_intake@1.94.0] 2023-08-14T20:21:04Z Marcel Mueller <marcel@m2> # return only intakes where archived_at is null
functions/receive_applications [functions/receive_applications@1.94.0] 2023-08-14T21:26:26Z Marcel Mueller <marcel@m2> # select intake where archived at is null
19 changes: 16 additions & 3 deletions db/test/unit/functions/next_intake_test.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
begin;

select plan(3);
select plan(4);

select has_function(
'ccbc_public', 'next_intake',
Expand All @@ -11,7 +11,9 @@ delete from ccbc_public.intake;

insert into ccbc_public.intake(open_timestamp, close_timestamp, ccbc_intake_number)
values('2022-03-01 09:00:00-07', '2022-05-01 09:00:00-07', 1),
('2022-05-01 09:00:01-07', '2022-06-01 09:00:00-07', 2);
('2022-05-01 09:00:01-07', '2022-06-01 09:00:00-07', 2),
('2022-06-01 09:00:01-07', '2022-07-01 09:00:00-07', 3);


select mocks.set_mocked_time_in_transaction('2022-04-01 09:00:00-07'::timestamptz);

Expand All @@ -21,14 +23,25 @@ select is(
'The next_intake function returns the next intake when there is an upcoming intake'
);

select mocks.set_mocked_time_in_transaction('2022-05-02 09:00:01-07'::timestamptz);
--test to ensure archived intakes are not returned
update ccbc_public.intake set archived_at = now() where ccbc_intake_number = 2;

select is(
(select ccbc_intake_number from ccbc_public.next_intake()),
(values(3::int)),
'The next_intake function returns the next intake when there is an archived intake before it'
);

select mocks.set_mocked_time_in_transaction('2023-05-02 09:00:01-07'::timestamptz);

select is(
(select ccbc_intake_number from ccbc_public.next_intake()),
(values(null::int)),
'The next_intake function returns null when there is no upcoming intake'
);



select finish();

rollback;
59 changes: 36 additions & 23 deletions db/test/unit/functions/open_intake_test.sql
Original file line number Diff line number Diff line change
@@ -1,56 +1,69 @@
BEGIN;
begin;

SELECT plan(8);
select plan(9);

SELECT has_function('ccbc_public', 'open_intake',
select has_function('ccbc_public', 'open_intake',
'function ccbc_public.open_intake exists');

-- setup intake table, since there is no guarantee that an intake will be available when running the test

DELETE FROM ccbc_public.intake;
delete from ccbc_public.intake;

INSERT INTO ccbc_public.intake
insert into ccbc_public.intake
(open_timestamp, close_timestamp, ccbc_intake_number)
VALUES (now(), now() + interval '10 days', 10);
values (now(), now() + interval '10 days', 10);

-- ccbc_auth_user
SET ROLE ccbc_auth_user;
SELECT concat('current user is: ', (SELECT current_user));
set role ccbc_auth_user;
select concat('current user is: ', (select current_user));

SELECT results_eq(
select results_eq(
$$
SELECT ccbc_intake_number FROM ccbc_public.open_intake();
select ccbc_intake_number from ccbc_public.open_intake();
$$
, ARRAY[10::int],
'Current intake number is displayed for ccbc_auth_user'
);

-- ccbc_guest
SET ROLE ccbc_guest;
SELECT concat('current user is: ', (SELECT current_user));
set role ccbc_guest;
select concat('current user is: ', (select current_user));

SELECT results_eq(
select results_eq(
$$
SELECT ccbc_intake_number FROM ccbc_public.open_intake();
select ccbc_intake_number from ccbc_public.open_intake();
$$
, ARRAY[10::int],
'Current intake number is displayed for guest'
);

set role postgres;
update ccbc_public.intake set archived_at = now() where ccbc_intake_number = 10;

set role ccbc_auth_user;

select results_eq(
$$
select ccbc_intake_number from ccbc_public.open_intake();
$$
, ARRAY[null::int],
'Current intake number is null when current intake is archived'
);

-- no open intakes should return null
-- set role with enough permissions to delete from intake table
SET ROLE postgres;
DELETE FROM ccbc_public.intake;
set role postgres;
delete from ccbc_public.intake;

INSERT INTO ccbc_public.intake
insert into ccbc_public.intake
(open_timestamp, close_timestamp, ccbc_intake_number)
VALUES (now() - interval '11 days', now() - interval '2 days', 10);
values (now() - interval '11 days', now() - interval '2 days', 10);

-- set role back to guest user
SET ROLE ccbc_guest;
set role ccbc_guest;

SELECT is(
(SELECT ccbc_intake_number FROM ccbc_public.open_intake())
select is(
(select ccbc_intake_number from ccbc_public.open_intake())
,
null
,
Expand All @@ -77,5 +90,5 @@ select function_privs_are(
'ccbc_analyst can execute ccbc_public.open_intake()'
);

SELECT finish();
ROLLBACK;
select finish();
rollback;

0 comments on commit eaddabb

Please sign in to comment.