From cafa127b0408a1e111f79e615afe97e097138788 Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Fri, 30 Aug 2024 11:47:14 +0100 Subject: [PATCH] Provider confirmation email When the provider has verified the claim we want to email their organisation with a confirmation email, letting them know a claim has been verified and who did it. Also fixes the `have_received_email_matcher` to show the template id if a match cant be found. --- .../provider/verify_claim_form.rb | 4 ++ app/mailers/application_mailer.rb | 3 +- app/mailers/claim_mailer.rb | 37 ++++++++++++- .../provider/verify_claim_form_spec.rb | 52 ++++++++++++++++--- spec/support/have_received_email_matcher.rb | 2 +- 5 files changed, 89 insertions(+), 9 deletions(-) diff --git a/app/forms/journeys/further_education_payments/provider/verify_claim_form.rb b/app/forms/journeys/further_education_payments/provider/verify_claim_form.rb index 8e9d21a8ae..4050970c3c 100644 --- a/app/forms/journeys/further_education_payments/provider/verify_claim_form.rb +++ b/app/forms/journeys/further_education_payments/provider/verify_claim_form.rb @@ -93,6 +93,10 @@ def save claim.save! + ClaimMailer + .further_education_payment_provider_confirmation_email(claim) + .deliver_later + true end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 255911f399..7d432cc3f9 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -41,7 +41,8 @@ class ApplicationMailer < Mail::Notify::Mailer CLAIM_UPDATE_AFTER_THREE_WEEKS_NOTIFY_TEMPLATE_ID: "95c11ee0-c9a9-4c6e-a040-54b1e1907b2c".freeze, CLAIM_REJECTED_NOTIFY_TEMPLATE_ID: "a1bb5f64-585f-4b03-b9db-0b20ad801b34".freeze, - CLAIM_PROVIDER_VERIFICATION_EMAIL_TEMPLATE_ID: "9a25fe46-2ee4-4a5c-8d47-0f04f058a87d".freeze + CLAIM_PROVIDER_VERIFICATION_EMAIL_TEMPLATE_ID: "9a25fe46-2ee4-4a5c-8d47-0f04f058a87d".freeze, + CLAIM_PROVIDER_VERIFICATION_CONFIRMATION_EMAIL_TEMPLATE_ID: "70942fe1-5838-4d37-904c-9d070f2582f0".freeze } EARLY_YEARS_PAYMENTS = { diff --git a/app/mailers/claim_mailer.rb b/app/mailers/claim_mailer.rb index 0736e6ed1b..05da41e95c 100644 --- a/app/mailers/claim_mailer.rb +++ b/app/mailers/claim_mailer.rb @@ -83,7 +83,7 @@ def early_years_payment_provider_email(claim, one_time_password, email) end def further_education_payment_provider_verification_email(claim) - unknown_policy_check(claim) + policy_check!(claim, Policies::FurtherEducationPayments) personalisation = { recipient_name: claim.school.name, @@ -103,6 +103,32 @@ def further_education_payment_provider_verification_email(claim) ) end + def further_education_payment_provider_confirmation_email(claim) + policy_check!(claim, Policies::FurtherEducationPayments) + + verification = claim.eligibility.verification + + verifier = verification.fetch("verifier") + verifier_name = "#{verifier.fetch("first_name")} #{verifier.fetch("last_name")}" + verification_date = verification.fetch("created_at").to_date + + personalisation = { + recipient_name: claim.school.name, + claim_reference: claim.reference, + claimant_name: claim.full_name, + verifier_name: verifier_name, + verification_date: l(verification_date) + } + + template_id = template_ids(claim)[:CLAIM_PROVIDER_VERIFICATION_CONFIRMATION_EMAIL_TEMPLATE_ID] + + template_mail( + template_id, + to: claim.school.eligible_fe_provider.primary_key_contact_email_address, + personalisation: personalisation + ) + end + private def set_common_instance_variables(claim) @@ -143,4 +169,13 @@ def unknown_policy_check(claim) def early_years_payment_provider_magic_link(one_time_password, email) "https://#{ENV["CANONICAL_HOSTNAME"]}/#{Journeys::EarlyYearsPayment::Provider::Authenticated::ROUTING_NAME}/claim?code=#{one_time_password}&email=#{email}" end + + def policy_check!(claim, policy) + return if claim.policy == policy + + raise( + ArgumentError, + "Claim policy does not match the expected policy `#{policy}`" + ) + end end diff --git a/spec/forms/journeys/further_education_payments/provider/verify_claim_form_spec.rb b/spec/forms/journeys/further_education_payments/provider/verify_claim_form_spec.rb index 835c20c6c1..6698bf5084 100644 --- a/spec/forms/journeys/further_education_payments/provider/verify_claim_form_spec.rb +++ b/spec/forms/journeys/further_education_payments/provider/verify_claim_form_spec.rb @@ -3,9 +3,32 @@ RSpec.describe Journeys::FurtherEducationPayments::Provider::VerifyClaimForm, type: :model do let(:journey) { Journeys::FurtherEducationPayments::Provider } - let(:eligibility) { create(:further_education_payments_eligibility) } + let(:school) do + create( + :school, + :further_education, + :fe_eligible, + name: "Springfield A and M" + ) + end + + let(:eligibility) do + create( + :further_education_payments_eligibility, + school: school + ) + end - let(:claim) { eligibility.claim } + let(:claim) do + create( + :claim, + first_name: "Edna", + surname: "Krabappel", + reference: "ABC123", + policy: Policies::FurtherEducationPayments, + eligibility: eligibility + ) + end let(:journey_session) do create( @@ -13,7 +36,7 @@ answers: { claim_id: claim.id, dfe_sign_in_uid: "123", - dfe_sign_in_first_name: "Seymoure", + dfe_sign_in_first_name: "Seymour", dfe_sign_in_last_name: "Skinner", dfe_sign_in_email: "seymore.skinner@springfield-elementary.edu" } @@ -129,11 +152,15 @@ ) end - it "verifies the claim" do + before do travel_to DateTime.new(2024, 1, 1, 12, 0, 0) do - form.save + perform_enqueued_jobs do + form.save + end end + end + it "verifies the claim" do expect(claim.reload.eligibility.verification).to match( { "assertions" => [ @@ -164,7 +191,7 @@ ], "verifier" => { "dfe_sign_in_uid" => "123", - "first_name" => "Seymoure", + "first_name" => "Seymour", "last_name" => "Skinner", "email" => "seymore.skinner@springfield-elementary.edu" }, @@ -172,5 +199,18 @@ } ) end + + it "sends the provider a confirmation email" do + expect( + claim.school.eligible_fe_provider.primary_key_contact_email_address + ).to have_received_email( + "70942fe1-5838-4d37-904c-9d070f2582f0", + recipient_name: "Springfield A and M", + claim_reference: "ABC123", + claimant_name: "Edna Krabappel", + verifier_name: "Seymour Skinner", + verification_date: "1 January 2024" + ) + end end end diff --git a/spec/support/have_received_email_matcher.rb b/spec/support/have_received_email_matcher.rb index d3967c3b6b..0b6c53985e 100644 --- a/spec/support/have_received_email_matcher.rb +++ b/spec/support/have_received_email_matcher.rb @@ -23,7 +23,7 @@ found = ActionMailer::Base.deliveries.map do |mail| <<-TEXT.squish To: #{mail.to} - - template id: #{mail["template_id"]} - + template id: #{mail.try(:template_id)} - personalisation: #{mail["personalisation"]}" TEXT end