Skip to content

Commit

Permalink
Refactor emails_helper
Browse files Browse the repository at this point in the history
This refactors the emails_helper module so that
it uses the same method for sending emails about
status updates, whether it is denied or approved.
This is with the aim of making the workflow more
open to extension, because at the moment it is not.
  • Loading branch information
Jonathan Young committed Sep 8, 2023
1 parent d1b7538 commit 7b97567
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
19 changes: 5 additions & 14 deletions app/controllers/annual_leave_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def update_status
@annual_leave_request = line_reports_leave_requests.find(params[:annual_leave_request_id])

if @annual_leave_request.update(annual_leave_request_params)
send_status_update_email
redirect_to_status_update_confirmation_page
helpers.send_status_updated_email(@annual_leave_request)
redirect_to status_update_confirmation_page
else
render "approve" if annual_leave_request_params[:status] == "approved"
render "deny" if annual_leave_request_params[:status] == "denied"
Expand All @@ -63,21 +63,12 @@ def annual_leave_request_params
params.require(:annual_leave_request).permit(:date_from, :date_to, :days_required, :status, :confirm_approval, :denial_reason)
end

def send_status_update_email
def status_update_confirmation_page
case annual_leave_request_params[:status]
when "approved"
helpers.send_approved_request_email(@annual_leave_request)
confirm_annual_leave_request_approval_path
when "denied"
helpers.send_denied_request_email(@annual_leave_request)
end
end

def redirect_to_status_update_confirmation_page
case annual_leave_request_params[:status]
when "approved"
redirect_to confirm_annual_leave_request_approval_path
when "denied"
redirect_to confirm_annual_leave_request_denial_path
confirm_annual_leave_request_denial_path
end
end
end
43 changes: 25 additions & 18 deletions app/helpers/emails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ def send_new_request_email(annual_leave_request)
)
end

def send_approved_request_email(annual_leave_request)
def send_status_updated_email(annual_leave_request)
case annual_leave_request.status
when "approved"
client.send_email(approval_email_hash(annual_leave_request))
when "denied"
client.send_email(denial_email_hash(annual_leave_request))
end
end

private

def client
@client ||= Notifications::Client.new(notify_api_key)
end

def notify_api_key
ENV["NOTIFY_API_KEY"]
end

def approval_email_hash(annual_leave_request)
user = annual_leave_request.user
line_manager = user.line_manager

client.send_email(
{
email_address: user.email,
template_id: "34542d49-8b91-412c-9393-c186a04a7d1c",
personalisation: {
Expand All @@ -29,14 +47,13 @@ def send_approved_request_email(annual_leave_request)
date_from: annual_leave_request.date_from.to_fs(:rfc822),
date_to: annual_leave_request.date_to.to_fs(:rfc822),
},
)
}
end

def send_denied_request_email(annual_leave_request)
def denial_email_hash(annual_leave_request)
user = annual_leave_request.user
line_manager = user.line_manager

client.send_email(
{
email_address: user.email,
template_id: "ec9035df-9c98-4e0e-8826-47768c311745",
personalisation: {
Expand All @@ -46,16 +63,6 @@ def send_denied_request_email(annual_leave_request)
date_to: annual_leave_request.date_to.to_fs(:rfc822),
denial_reason: annual_leave_request.denial_reason,
},
)
end

private

def client
@client ||= Notifications::Client.new(notify_api_key)
end

def notify_api_key
ENV["NOTIFY_API_KEY"]
}
end
end
20 changes: 10 additions & 10 deletions spec/helpers/emails_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
end
end

describe "#send_approved_request_email" do
it "sends an approval email to the user" do
describe "#send_status_updated_email" do
it "sends a denial email to the user when status is updated to 'denied'" do
@client = Notifications::Client.new(ENV["NOTIFY_TEST_API_KEY"])
annual_leave_request.status = "denied"
annual_leave_request.denial_reason = "some reason"

response_notification = helper.send_approved_request_email(annual_leave_request)
response_notification = helper.send_status_updated_email(annual_leave_request)
response = client.get_notification(response_notification.id)

expect(response.email_address).to eq(user.email)
expect(response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Approved")
expect(response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Denied")
end
end

describe "#send_denied_request_email" do
it "sends a denial email to the user" do
it "sends an approval email to the user when status is updated to 'approved'" do
@client = Notifications::Client.new(ENV["NOTIFY_TEST_API_KEY"])
annual_leave_request.denial_reason = "some reason"
annual_leave_request.status = "approved"

response_notification = helper.send_denied_request_email(annual_leave_request)
response_notification = helper.send_status_updated_email(annual_leave_request)
response = client.get_notification(response_notification.id)

expect(response.email_address).to eq(user.email)
expect(response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Denied")
expect(response.subject).to eq("GOV.UK Holiday Logger – Annual Leave Request Approved")
end
end
end

0 comments on commit 7b97567

Please sign in to comment.