Skip to content

Commit

Permalink
feat: create dependencies on submission
Browse files Browse the repository at this point in the history
  • Loading branch information
rafasdc committed Dec 10, 2024
1 parent 6511f25 commit 87f0eb0
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 3 deletions.
7 changes: 7 additions & 0 deletions db/deploy/mutations/submit_application.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ begin
insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'submitted');

insert into ccbc_public.application_dependencies
(application_id, json_data, reason_for_change)
values (application_row_id, jsonb_build_object(
'connectedCoastNetworkDependent', 'TBD',
'crtcProjectDependent', 'TBD'
), '');

if is_rolling_intake then
insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'received');
Expand Down
108 changes: 108 additions & 0 deletions db/deploy/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
-- Deploy ccbc:mutations/submit_application to pg

begin;
create or replace function ccbc_public.submit_application(application_row_id int, _form_schema_id int)
returns ccbc_public.application as $$
declare
current_intake_id int;
associated_intake_id int;
current_intake_number int;
reference_number bigint;
_counter_id int;
application_status varchar;
num_acknowledgements integer;
_form_data_schema_id integer;
_form_data jsonb;
form_data_id int;
is_rolling_intake boolean;
begin

select ccbc_public.application_status(
(select row(ccbc_public.application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id)
) into application_status;

if application_status = 'submitted' then
return (select row(application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id);
end if;

if application_status != 'draft' then
raise 'The application cannot be submitted as it has the following status: %', application_status;
end if;

select json_data, id, form_schema_id from
ccbc_public.application_form_data((select row(ccbc_public.application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id))
into _form_data, form_data_id, _form_data_schema_id;

select jsonb_array_length(json_schema -> 'properties' -> 'acknowledgements' -> 'properties' -> 'acknowledgementsList' -> 'items' -> 'enum')
from ccbc_public.form where id = _form_data_schema_id into num_acknowledgements;

if coalesce(_form_data -> 'submission' ->> 'submissionCompletedFor', '') = '' then
raise 'The application cannot be submitted as the submission field submission_completed_for is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionCompletedBy', '') = '' then
raise 'The application cannot be submitted as the submission field submission_completed_by is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionTitle', '') = '' then
raise 'The application cannot be submitted as the submission field submission_title is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionDate', '') = '' then
raise 'The application cannot be submitted as the submission field submission_date is null or empty';
end if;

if coalesce(jsonb_array_length(_form_data -> 'acknowledgements' -> 'acknowledgementsList'),0) <> num_acknowledgements then
raise 'The application cannot be submitted as there are unchecked acknowledgements';
end if;

select id, ccbc_intake_number, counter_id, rolling_intake from ccbc_public.open_intake()
into current_intake_id, current_intake_number, _counter_id, is_rolling_intake;
select intake_id from ccbc_public.application where id = application_row_id into associated_intake_id;
-- Don't have to worry about the application being re-submitted as it will exit earlier with the status check
if current_intake_id is null or current_intake_id != associated_intake_id then
select id, ccbc_intake_number, counter_id from ccbc_public.intake where id = associated_intake_id into current_intake_id, current_intake_number, _counter_id;
end if;

if current_intake_id is null then
raise 'There is no open intake, the application cannot be submitted';
end if;

insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'submitted');

if is_rolling_intake then
insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'received');
end if;

if current_intake_number = 99 then
update ccbc_public.application set
ccbc_number = format(
'CCBC-025%s',
lpad((ccbc_public.increment_counter(_counter_id::int))::text, 3, '0')
),
intake_id = current_intake_id where id = application_row_id;
else
update ccbc_public.application set
ccbc_number=format(
'CCBC-%s%s', lpad(current_intake_number::text , 2, '0'),
lpad((ccbc_public.increment_counter(_counter_id::int))::text, 4, '0')
),
intake_id = current_intake_id where id = application_row_id;
end if;

update ccbc_public.form_data set
form_data_status_type_id = 'committed',
form_schema_id = _form_schema_id
where id = form_data_id;

return (select row(application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id);
end;

$$ language plpgsql;

grant execute on function ccbc_public.submit_application to ccbc_auth_user;


commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Deploy ccbc:tables/application_dependencies_001_add_applicant_insert to pg

begin;

do
$$
begin

perform ccbc_private.grant_permissions('insert', 'application_dependencies', 'ccbc_auth_user');

end
$$;

commit;
11 changes: 8 additions & 3 deletions db/revert/mutations/submit_application.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-- Deploy ccbc:mutations/submit_application to pg

begin;
drop function if exists ccbc_public.submit_application(application_row_id int);
create or replace function ccbc_public.submit_application(application_row_id int, _form_schema_id int)
returns ccbc_public.application as $$
declare
Expand All @@ -15,6 +14,7 @@ declare
_form_data_schema_id integer;
_form_data jsonb;
form_data_id int;
is_rolling_intake boolean;
begin

select ccbc_public.application_status(
Expand Down Expand Up @@ -56,8 +56,8 @@ begin
raise 'The application cannot be submitted as there are unchecked acknowledgements';
end if;

select id, ccbc_intake_number, counter_id from ccbc_public.open_intake()
into current_intake_id, current_intake_number, _counter_id;
select id, ccbc_intake_number, counter_id, rolling_intake from ccbc_public.open_intake()
into current_intake_id, current_intake_number, _counter_id, is_rolling_intake;
select intake_id from ccbc_public.application where id = application_row_id into associated_intake_id;
-- Don't have to worry about the application being re-submitted as it will exit earlier with the status check
if current_intake_id is null or current_intake_id != associated_intake_id then
Expand All @@ -71,6 +71,11 @@ begin
insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'submitted');

if is_rolling_intake then
insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'received');
end if;

if current_intake_number = 99 then
update ccbc_public.application set
ccbc_number = format(
Expand Down
103 changes: 103 additions & 0 deletions db/revert/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-- Deploy ccbc:mutations/submit_application to pg

begin;
drop function if exists ccbc_public.submit_application(application_row_id int);
create or replace function ccbc_public.submit_application(application_row_id int, _form_schema_id int)
returns ccbc_public.application as $$
declare
current_intake_id int;
associated_intake_id int;
current_intake_number int;
reference_number bigint;
_counter_id int;
application_status varchar;
num_acknowledgements integer;
_form_data_schema_id integer;
_form_data jsonb;
form_data_id int;
begin

select ccbc_public.application_status(
(select row(ccbc_public.application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id)
) into application_status;

if application_status = 'submitted' then
return (select row(application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id);
end if;

if application_status != 'draft' then
raise 'The application cannot be submitted as it has the following status: %', application_status;
end if;

select json_data, id, form_schema_id from
ccbc_public.application_form_data((select row(ccbc_public.application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id))
into _form_data, form_data_id, _form_data_schema_id;

select jsonb_array_length(json_schema -> 'properties' -> 'acknowledgements' -> 'properties' -> 'acknowledgementsList' -> 'items' -> 'enum')
from ccbc_public.form where id = _form_data_schema_id into num_acknowledgements;

if coalesce(_form_data -> 'submission' ->> 'submissionCompletedFor', '') = '' then
raise 'The application cannot be submitted as the submission field submission_completed_for is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionCompletedBy', '') = '' then
raise 'The application cannot be submitted as the submission field submission_completed_by is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionTitle', '') = '' then
raise 'The application cannot be submitted as the submission field submission_title is null or empty';
end if;

if coalesce(_form_data -> 'submission' ->> 'submissionDate', '') = '' then
raise 'The application cannot be submitted as the submission field submission_date is null or empty';
end if;

if coalesce(jsonb_array_length(_form_data -> 'acknowledgements' -> 'acknowledgementsList'),0) <> num_acknowledgements then
raise 'The application cannot be submitted as there are unchecked acknowledgements';
end if;

select id, ccbc_intake_number, counter_id from ccbc_public.open_intake()
into current_intake_id, current_intake_number, _counter_id;
select intake_id from ccbc_public.application where id = application_row_id into associated_intake_id;
-- Don't have to worry about the application being re-submitted as it will exit earlier with the status check
if current_intake_id is null or current_intake_id != associated_intake_id then
select id, ccbc_intake_number, counter_id from ccbc_public.intake where id = associated_intake_id into current_intake_id, current_intake_number, _counter_id;
end if;

if current_intake_id is null then
raise 'There is no open intake, the application cannot be submitted';
end if;

insert into ccbc_public.application_status
(application_id, status) values (application_row_id, 'submitted');

if current_intake_number = 99 then
update ccbc_public.application set
ccbc_number = format(
'CCBC-025%s',
lpad((ccbc_public.increment_counter(_counter_id::int))::text, 3, '0')
),
intake_id = current_intake_id where id = application_row_id;
else
update ccbc_public.application set
ccbc_number=format(
'CCBC-%s%s', lpad(current_intake_number::text , 2, '0'),
lpad((ccbc_public.increment_counter(_counter_id::int))::text, 4, '0')
),
intake_id = current_intake_id where id = application_row_id;
end if;

update ccbc_public.form_data set
form_data_status_type_id = 'committed',
form_schema_id = _form_schema_id
where id = form_data_id;

return (select row(application.*)::ccbc_public.application from ccbc_public.application where id = application_row_id);
end;

$$ language plpgsql;

grant execute on function ccbc_public.submit_application to ccbc_auth_user;


commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ccbc:tables/application_dependencies_001_add_applicant_insert from pg

BEGIN;

-- XXX Add DDLs here.

COMMIT;
2 changes: 2 additions & 0 deletions db/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,5 @@ mutations/create_assessment_form [mutations/[email protected]] 2024
@1.216.0 2024-12-09T21:27:31Z CCBC Service Account <[email protected]> # release v1.216.0
@1.216.1 2024-12-09T22:11:47Z CCBC Service Account <[email protected]> # release v1.216.1
@1.216.2 2024-12-09T23:35:46Z CCBC Service Account <[email protected]> # release v1.216.2
mutations/submit_application [mutations/[email protected]] 2024-12-09T22:24:03Z Rafael Solorzano <[email protected]> # insert TBD for dependencies on application submission
tables/application_dependencies_001_add_applicant_insert 2024-12-09T22:35:13Z Rafael Solorzano <[email protected]> # grant insert to application dependencies to applicant for submission

0 comments on commit 87f0eb0

Please sign in to comment.