Skip to content

Commit

Permalink
feat: removing certifier jobs from graphile worker
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Mar 23, 2021
1 parent fbc3c37 commit 34369e7
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 110 deletions.
104 changes: 0 additions & 104 deletions schema/deploy/trigger_functions/run_graphile_worker_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,110 +79,6 @@ begin
'versionNumber', new.version_number));
return new;

-- **ON REQUEST FOR CERTIFICATION**
when tg_argv[0] = 'certification_request' then

if (new.send_certification_request = true) then

with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'certification_request',
'certifierUrl', new.certifier_url,
'reporterEmail', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'email', new.certifier_email,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name));

-- timestamp when the email was sent
new.certification_request_sent_at = now();
end if;
return new;

-- **ON SIGNED BY CERTIFIER**
when tg_argv[0] = 'signed_by_certifier' then

with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

with certifier as (select u.first_name, u.last_name from ggircs_portal.ciip_user u where u.id = new.certified_by)
select first_name, last_name from certifier into certifier_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'signed_by_certifier',
'applicationId', new.application_id,
'versionNumber', new.version_number,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'certifierEmail', new.certifier_email,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name,
'certifierFirstName', certifier_details.first_name,
'certifierLastName', certifier_details.last_name));
return new;

-- **ON SIGNED BY CERTIFIER**
when tg_argv[0] = 'recertification' then

with ad as (select a.id, o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select id, operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

with certifier as (select u.first_name, u.last_name from ggircs_portal.ciip_user u where u.id = new.certified_by)
select first_name, last_name from certifier into certifier_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'recertification',
'applicationId', application_details.id,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name));
new.recertification_request_sent=true;
return new;
end case;
end;
$$ language plpgsql volatile;
Expand Down
192 changes: 192 additions & 0 deletions schema/deploy/trigger_functions/run_graphile_worker_job@v1.16.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
-- Deploy ggircs-portal:trigger_functions/run_graphile_worker_job to pg
-- requires: schema_ggircs_portal

begin;

create or replace function ggircs_portal_private.run_graphile_worker_job() returns trigger as $$
declare
application_details record;
certifier_details record;
status_change_type text;
begin
case
-- **ON NEW USER REGISTRATION**
-- Send welcome email after registering for CIIP program
when tg_argv[0]='welcome' then
perform ggircs_portal_private.graphile_worker_job_definer('sendMail', json_build_object('type', tg_argv[0], 'firstName', new.first_name, 'lastName', new.last_name, 'email', new.email_address));
return new;

-- **ON APPLICATION REVISION STATUS CHANGE**
-- Notify reporter when the status of their application has changed
when tg_argv[0]='status_change' then

-- Do not send any mail when a draft revision is created
if (new.application_revision_status = 'draft' or new.version_number = 0) then
return new;
end if;

-- Get application details
with ad as (select o.operator_name, f.organisation_id, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = ar.created_by
)
select operator_name, organisation_id, facility_name, first_name, last_name, email_address from ad into application_details;
-- Choose type of email to sent based on what the status of the application has been changed to
case
when new.application_revision_status = 'submitted' then
status_change_type := 'status_change_submitted';
perform ggircs_portal_private.graphile_worker_job_definer(
'sendMail',
json_build_object(
'type', 'notify_admin_submitted',
'applicationId', new.application_id,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name,
'versionNumber', new.version_number
)
);
when new.application_revision_status = 'approved' then
status_change_type := 'status_change_approved';
when new.application_revision_status = 'rejected' then
status_change_type := 'status_change_rejected';
when new.application_revision_status = 'requested changes' then
status_change_type := 'status_change_requested_changes';
else
status_change_type := 'status_change_other';
end case;

-- Assign worker to the sendMail task
perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', status_change_type,
'applicationId', new.application_id,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'email', application_details.email_address,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name,
'organisationId', application_details.organisation_id,
'status', new.application_revision_status,
'versionNumber', new.version_number));
return new;

-- **ON REQUEST FOR CERTIFICATION**
when tg_argv[0] = 'certification_request' then

if (new.send_certification_request = true) then

with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'certification_request',
'certifierUrl', new.certifier_url,
'reporterEmail', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'email', new.certifier_email,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name));

-- timestamp when the email was sent
new.certification_request_sent_at = now();
end if;
return new;

-- **ON SIGNED BY CERTIFIER**
when tg_argv[0] = 'signed_by_certifier' then

with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

with certifier as (select u.first_name, u.last_name from ggircs_portal.ciip_user u where u.id = new.certified_by)
select first_name, last_name from certifier into certifier_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'signed_by_certifier',
'applicationId', new.application_id,
'versionNumber', new.version_number,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'certifierEmail', new.certifier_email,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name,
'certifierFirstName', certifier_details.first_name,
'certifierLastName', certifier_details.last_name));
return new;

-- **ON SIGNED BY CERTIFIER**
when tg_argv[0] = 'recertification' then

with ad as (select a.id, o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
and new.application_id = ar.application_id
and new.version_number = ar.version_number
join ggircs_portal.facility f
on a.facility_id = f.id
join ggircs_portal.organisation o
on f.organisation_id = o.id
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select id, operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

with certifier as (select u.first_name, u.last_name from ggircs_portal.ciip_user u where u.id = new.certified_by)
select first_name, last_name from certifier into certifier_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'recertification',
'applicationId', application_details.id,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name));
new.recertification_request_sent=true;
return new;
end case;
end;
$$ language plpgsql volatile;

comment on function ggircs_portal_private.run_graphile_worker_job is 'Trigger function runs a graphile-worker job upon being called';

commit;
20 changes: 14 additions & 6 deletions schema/revert/trigger_functions/run_graphile_worker_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ begin
end if;

-- Get application details
with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
with ad as (select o.operator_name, f.organisation_id, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
Expand All @@ -39,7 +39,7 @@ begin
join ggircs_portal.ciip_user u
on u.id = ar.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;
select operator_name, organisation_id, facility_name, first_name, last_name, email_address from ad into application_details;
-- Choose type of email to sent based on what the status of the application has been changed to
case
when new.application_revision_status = 'submitted' then
Expand All @@ -48,8 +48,10 @@ begin
'sendMail',
json_build_object(
'type', 'notify_admin_submitted',
'applicationId', new.application_id,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name
'operatorName', application_details.operator_name,
'versionNumber', new.version_number
)
);
when new.application_revision_status = 'approved' then
Expand All @@ -66,12 +68,15 @@ begin
perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', status_change_type,
'applicationId', new.application_id,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
'email', application_details.email_address,
'facilityName', application_details.facility_name,
'operatorName', application_details.operator_name,
'status', new.application_revision_status));
'organisationId', application_details.organisation_id,
'status', new.application_revision_status,
'versionNumber', new.version_number));
return new;

-- **ON REQUEST FOR CERTIFICATION**
Expand Down Expand Up @@ -134,6 +139,8 @@ begin
perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'signed_by_certifier',
'applicationId', new.application_id,
'versionNumber', new.version_number,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
Expand All @@ -147,7 +154,7 @@ begin
-- **ON SIGNED BY CERTIFIER**
when tg_argv[0] = 'recertification' then

with ad as (select o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
with ad as (select a.id, o.operator_name, f.facility_name, u.first_name, u.last_name, u.email_address
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on ar.application_id = a.id
Expand All @@ -160,14 +167,15 @@ begin
join ggircs_portal.ciip_user u
on u.id = new.created_by
)
select operator_name, facility_name, first_name, last_name, email_address from ad into application_details;
select id, operator_name, facility_name, first_name, last_name, email_address from ad into application_details;

with certifier as (select u.first_name, u.last_name from ggircs_portal.ciip_user u where u.id = new.certified_by)
select first_name, last_name from certifier into certifier_details;

perform ggircs_portal_private.graphile_worker_job_definer('sendMail',
json_build_object(
'type', 'recertification',
'applicationId', application_details.id,
'email', application_details.email_address,
'firstName', application_details.first_name,
'lastName', application_details.last_name,
Expand Down
Loading

0 comments on commit 34369e7

Please sign in to comment.