diff --git a/db/deploy/mutations/update_application_form.sql b/db/deploy/mutations/update_application_form.sql index 765a97ea2..d9b2298e3 100644 --- a/db/deploy/mutations/update_application_form.sql +++ b/db/deploy/mutations/update_application_form.sql @@ -13,9 +13,9 @@ declare begin select updated_at into current_updated_at from ccbc_public.form_data where id = form_data_row_id; - -- Adding a buffer, can be used to update if someone happens to have a version of the form that was opened <1 second from the last save + -- Adding a buffer, can be used to update if someone happens to have a version of the form that was opened <3 seconds from the last save -- Risk is that there can still be overwritten data. - if client_updated_at < current_updated_at - interval '1 second' then + if client_updated_at < current_updated_at - interval '3 second' then raise exception 'Data is Out of Sync'; end if; diff --git a/db/deploy/mutations/update_application_form@1.94.0.sql b/db/deploy/mutations/update_application_form@1.94.0.sql new file mode 100644 index 000000000..765a97ea2 --- /dev/null +++ b/db/deploy/mutations/update_application_form@1.94.0.sql @@ -0,0 +1,48 @@ +-- Deploy ccbc:mutations/update_application_form to pg + +begin; +-- Since we're updating the function definition, need to manually drop +drop function ccbc_public.update_application_form; + +create or replace function ccbc_public.update_application_form(form_data_row_id int, json_data jsonb, last_edited_page varchar, client_updated_at timestamp with time zone) +returns ccbc_public.form_data as +$func$ +declare + current_updated_at timestamp with time zone; + updated_form_data ccbc_public.form_data; +begin + + select updated_at into current_updated_at from ccbc_public.form_data where id = form_data_row_id; + -- Adding a buffer, can be used to update if someone happens to have a version of the form that was opened <1 second from the last save + -- Risk is that there can still be overwritten data. + if client_updated_at < current_updated_at - interval '1 second' then + raise exception 'Data is Out of Sync'; + end if; + + update ccbc_public.form_data + set + -- use json concatenation operator to merge the provided json_data with the dynamic submission values + json_data = $2 || jsonb_build_object( + 'submission', coalesce($2->'submission', jsonb_build_object()) || jsonb_build_object( + 'submissionCompletedFor', $2->'organizationProfile'->'organizationName', + 'submissionDate', (date_trunc('day', now(), 'America/Vancouver')::date) + ) + ), + last_edited_page = $3 + where id = form_data_row_id + returning * into updated_form_data; + + return updated_form_data; +end; +$func$ language plpgsql; + +grant execute on function ccbc_public.update_application_form to ccbc_auth_user; + +comment on function ccbc_public.update_application_form is +$$ +Mutation to update the "application" form. +This mutation should only be used by applicants as it sets the submission page data +$$; + + +commit; diff --git a/db/revert/mutations/update_application_form.sql b/db/revert/mutations/update_application_form.sql index 04ec7235f..765a97ea2 100644 --- a/db/revert/mutations/update_application_form.sql +++ b/db/revert/mutations/update_application_form.sql @@ -1,27 +1,40 @@ -- Deploy ccbc:mutations/update_application_form to pg begin; - +-- Since we're updating the function definition, need to manually drop drop function ccbc_public.update_application_form; -create or replace function ccbc_public.update_application_form(form_data_row_id int, json_data jsonb, last_edited_page varchar) +create or replace function ccbc_public.update_application_form(form_data_row_id int, json_data jsonb, last_edited_page varchar, client_updated_at timestamp with time zone) returns ccbc_public.form_data as -$$ - -update ccbc_public.form_data -set --- use json concatenation operator to merge the provided json_data with the dynamic submission values - json_data = $2 || jsonb_build_object( - 'submission', coalesce($2->'submission', jsonb_build_object()) || jsonb_build_object( - 'submissionCompletedFor', $2->'organizationProfile'->'organizationName', - 'submissionDate', (date_trunc('day', now(), 'America/Vancouver')::date) - ) - ), - last_edited_page = $3 -where id = form_data_row_id -returning *; - -$$ language sql; +$func$ +declare + current_updated_at timestamp with time zone; + updated_form_data ccbc_public.form_data; +begin + + select updated_at into current_updated_at from ccbc_public.form_data where id = form_data_row_id; + -- Adding a buffer, can be used to update if someone happens to have a version of the form that was opened <1 second from the last save + -- Risk is that there can still be overwritten data. + if client_updated_at < current_updated_at - interval '1 second' then + raise exception 'Data is Out of Sync'; + end if; + + update ccbc_public.form_data + set + -- use json concatenation operator to merge the provided json_data with the dynamic submission values + json_data = $2 || jsonb_build_object( + 'submission', coalesce($2->'submission', jsonb_build_object()) || jsonb_build_object( + 'submissionCompletedFor', $2->'organizationProfile'->'organizationName', + 'submissionDate', (date_trunc('day', now(), 'America/Vancouver')::date) + ) + ), + last_edited_page = $3 + where id = form_data_row_id + returning * into updated_form_data; + + return updated_form_data; +end; +$func$ language plpgsql; grant execute on function ccbc_public.update_application_form to ccbc_auth_user; diff --git a/db/revert/mutations/update_application_form@1.94.0.sql b/db/revert/mutations/update_application_form@1.94.0.sql new file mode 100644 index 000000000..04ec7235f --- /dev/null +++ b/db/revert/mutations/update_application_form@1.94.0.sql @@ -0,0 +1,35 @@ +-- Deploy ccbc:mutations/update_application_form to pg + +begin; + +drop function ccbc_public.update_application_form; + +create or replace function ccbc_public.update_application_form(form_data_row_id int, json_data jsonb, last_edited_page varchar) +returns ccbc_public.form_data as +$$ + +update ccbc_public.form_data +set +-- use json concatenation operator to merge the provided json_data with the dynamic submission values + json_data = $2 || jsonb_build_object( + 'submission', coalesce($2->'submission', jsonb_build_object()) || jsonb_build_object( + 'submissionCompletedFor', $2->'organizationProfile'->'organizationName', + 'submissionDate', (date_trunc('day', now(), 'America/Vancouver')::date) + ) + ), + last_edited_page = $3 +where id = form_data_row_id +returning *; + +$$ language sql; + +grant execute on function ccbc_public.update_application_form to ccbc_auth_user; + +comment on function ccbc_public.update_application_form is +$$ +Mutation to update the "application" form. +This mutation should only be used by applicants as it sets the submission page data +$$; + + +commit; diff --git a/db/sqitch.plan b/db/sqitch.plan index ea2414ada..c2cef41ae 100644 --- a/db/sqitch.plan +++ b/db/sqitch.plan @@ -397,3 +397,4 @@ mutations/archive_application_community_progress_report 2023-08-10T17:18:29Z Mar functions/next_intake [functions/next_intake@1.94.0] 2023-08-14T20:10:54Z Marcel Mueller # return only intakes where archived_at is null functions/open_intake [functions/open_intake@1.94.0] 2023-08-14T20:21:04Z Marcel Mueller # return only intakes where archived_at is null functions/receive_applications [functions/receive_applications@1.94.0] 2023-08-14T21:26:26Z Marcel Mueller # select intake where archived at is null +mutations/update_application_form [mutations/update_application_form@1.94.0] 2023-08-14T20:22:39Z Tony Bushara # Increase the buffer time to 3 seconds