Skip to content

Commit

Permalink
send email when eligible_for_funding changes from false to true (#2188)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkesh authored Feb 7, 2025
1 parent f5f3dea commit f90025b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
14 changes: 14 additions & 0 deletions app/mailers/application_funding_eligibility_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ApplicationFundingEligibilityMailer < ApplicationMailer
ELIGIBLE_FOR_FUNDING_TEMPLATE = "87b6e2f6-e2ef-4354-9c3d-876099918507".freeze

def eligible_for_funding_mail(to:, full_name:, provider_name:, course_name:, ecf_id:)
template_mail(ELIGIBLE_FOR_FUNDING_TEMPLATE,
to:,
personalisation: {
full_name:,
provider_name:,
course_name:,
ecf_id:,
})
end
end
14 changes: 13 additions & 1 deletion app/services/applications/change_funding_eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def change_funding_eligibility
funding_eligiblity_status_code =
(eligible_for_funding ? :marked_funded_by_policy : :marked_ineligible_by_policy)

application.update!(eligible_for_funding:, funding_eligiblity_status_code:)
application.update!(eligible_for_funding:, funding_eligiblity_status_code:).tap do
send_eligible_for_funding_email if application.saved_changes["eligible_for_funding"] == [false, true]
end
end

private
Expand All @@ -49,5 +51,15 @@ def validate_funding_eligiblity_status_with_funded_place
def declared_as_billable_or_changeable?
application.declarations.billable_or_changeable.count.positive?
end

def send_eligible_for_funding_email
ApplicationFundingEligibilityMailer.eligible_for_funding_mail(
to: application.user.email,
full_name: application.user.full_name,
provider_name: application.lead_provider.name,
course_name: application.course.name,
ecf_id: application.ecf_id,
).deliver_later
end
end
end
5 changes: 4 additions & 1 deletion spec/features/npq_separation/admin/applications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.feature "Listing and viewing applications", type: :feature do
include Helpers::AdminLogin
include Helpers::MailHelper

let(:applications_per_page) { Pagy::DEFAULT[:limit] }
let(:applications_in_order) { Application.order(created_at: :asc) }
Expand Down Expand Up @@ -303,7 +304,9 @@

expect(page).to have_css(".govuk-error-message", text: "Choose whether the Application is eligible for funding")
choose "Yes", visible: :all
click_button "Continue"
perform_enqueued_jobs { click_button "Continue" }

expect_mail_to_have_been_sent(to: application.user.email, template_id: ApplicationFundingEligibilityMailer::ELIGIBLE_FOR_FUNDING_TEMPLATE)

expect(page).to have_css("h1", text: "Application for #{application.user.full_name}")
within(".govuk-summary-list:nth-of-type(3)") do |summary_list|
Expand Down
55 changes: 54 additions & 1 deletion spec/services/applications/change_funding_eligibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

let(:application) { create(:application, :accepted) }

before { allow(ApplicationFundingEligibilityMailer).to receive(:eligible_for_funding_mail).and_call_original }

describe "validations" do
it { is_expected.to validate_presence_of :application }

Expand Down Expand Up @@ -56,7 +58,7 @@

before { service.eligible_for_funding = true }

context "with valid update" do
context "with valid update from false to true" do
it { is_expected.to be true }

it "changes eligibility_for_funding" do
Expand All @@ -72,6 +74,57 @@
.from(nil)
.to("marked_funded_by_policy")
end

it "sends an email" do
expect(ApplicationFundingEligibilityMailer).to receive(:eligible_for_funding_mail).with(
to: application.user.email,
full_name: application.user.full_name,
provider_name: application.lead_provider.name,
course_name: application.course.name,
ecf_id: application.ecf_id,
)
make_change
end
end

context "with valid update from true to false" do
let(:application) { create(:application, :pending, :eligible_for_funding) }

before { service.eligible_for_funding = false }

it { is_expected.to be true }

it "changes eligibility_for_funding" do
expect { make_change }
.to change { application.reload.eligible_for_funding }
.from(true)
.to(false)
end

it "sets funding_eligibility_status_code" do
expect { make_change }
.to change { application.reload.funding_eligiblity_status_code }
.from(nil)
.to("marked_ineligible_by_policy")
end

it "does not send an email" do
expect(ApplicationFundingEligibilityMailer).not_to receive(:eligible_for_funding_mail)
make_change
end
end

context "with a valid update from true to true" do
let(:application) { create(:application, :pending, :eligible_for_funding) }

before { service.eligible_for_funding = true }

it { is_expected.to be true }

it "does not send an email" do
expect(ApplicationFundingEligibilityMailer).not_to receive(:eligible_for_funding_mail)
make_change
end
end

context "with invalid update" do
Expand Down
7 changes: 7 additions & 0 deletions spec/support/helpers/mail_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Helpers
module MailHelper
def expect_mail_to_have_been_sent(to:, template_id:)
expect(ActionMailer::Base.deliveries.count { |mail| mail.to == [to] && mail[:template_id].unparsed_value == template_id }).to eq(1)
end
end
end

0 comments on commit f90025b

Please sign in to comment.