diff --git a/app/controllers/claims_controller.rb b/app/controllers/claims_controller.rb index e716e99d71..aa448dc0c3 100644 --- a/app/controllers/claims_controller.rb +++ b/app/controllers/claims_controller.rb @@ -85,7 +85,7 @@ def check_page_is_in_sequence def handle_magic_link otp = OneTimePassword::Validator.new(params[:code], answers.sent_one_time_password_at) - if otp.valid? # && EligibleEyProvider.eligible_email?(journey_session.answers.email_address) # TODO eligible check once model merged: https://github.com/DFE-Digital/claim-additional-payments-for-teaching/pull/3050 + if otp.valid? journey_session.answers.assign_attributes(email_verified: true) journey_session.save! session[:slugs] << page_sequence.next_required_slug diff --git a/app/forms/journeys/early_years_payment/provider/email_address_form.rb b/app/forms/journeys/early_years_payment/provider/email_address_form.rb index b2c03c0edd..3cd9294fe8 100644 --- a/app/forms/journeys/early_years_payment/provider/email_address_form.rb +++ b/app/forms/journeys/early_years_payment/provider/email_address_form.rb @@ -5,14 +5,16 @@ class EmailAddressForm < Form attribute :email_address, :string def save - journey_session.answers.assign_attributes( - email_address: email_address, - sent_one_time_password_at: Time.now, - email_verified: email_verified - ) - journey_session.save! + if EligibleEyProvider.eligible_email?(email_address) + journey_session.answers.assign_attributes( + email_address: email_address, + sent_one_time_password_at: Time.now, + email_verified: email_verified + ) + journey_session.save! - ClaimMailer.early_years_payment_provider_email(answers, otp_code).deliver_now + ClaimMailer.early_years_payment_provider_email(answers, otp_code).deliver_now + end end private diff --git a/app/models/eligible_ey_provider.rb b/app/models/eligible_ey_provider.rb index 99f8c3008f..3a75dcf358 100644 --- a/app/models/eligible_ey_provider.rb +++ b/app/models/eligible_ey_provider.rb @@ -23,4 +23,10 @@ def self.csv end end end + + def self.eligible_email?(email_address) + where(primary_key_contact_email_address: email_address).or( + where(secondary_contact_email_address: email_address) + ).exists? + end end diff --git a/db/migrate/20240806121023_add_email_indexes_to_eligible_ey_providers.rb b/db/migrate/20240806121023_add_email_indexes_to_eligible_ey_providers.rb new file mode 100644 index 0000000000..f99c12b9b7 --- /dev/null +++ b/db/migrate/20240806121023_add_email_indexes_to_eligible_ey_providers.rb @@ -0,0 +1,6 @@ +class AddEmailIndexesToEligibleEyProviders < ActiveRecord::Migration[7.0] + def change + add_index :eligible_ey_providers, :primary_key_contact_email_address, name: "index_eligible_ey_providers_on_primary_contact_email_address" + add_index :eligible_ey_providers, :secondary_contact_email_address + end +end diff --git a/db/schema.rb b/db/schema.rb index 591bc7119c..39ff67bd09 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_07_31_152713) do +ActiveRecord::Schema[7.0].define(version: 2024_08_06_121023) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "pgcrypto" @@ -190,6 +190,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["local_authority_id"], name: "index_eligible_ey_providers_on_local_authority_id" + t.index ["primary_key_contact_email_address"], name: "index_eligible_ey_providers_on_primary_contact_email_address" + t.index ["secondary_contact_email_address"], name: "index_eligible_ey_providers_on_secondary_contact_email_address" t.index ["urn"], name: "index_eligible_ey_providers_on_urn" end diff --git a/spec/features/early_years_payment/provider/happy_path_spec.rb b/spec/features/early_years_payment/provider/happy_path_spec.rb index 5c5ed7e166..3e37ac21aa 100644 --- a/spec/features/early_years_payment/provider/happy_path_spec.rb +++ b/spec/features/early_years_payment/provider/happy_path_spec.rb @@ -5,6 +5,10 @@ let(:mail) { ActionMailer::Base.deliveries.last } let(:magic_link) { mail[:personalisation].unparsed_value[:magic_link] } + before do + create(:eligible_ey_provider, primary_key_contact_email_address: "johndoe@example.com", secondary_contact_email_address: "janedoe@example.com") + end + scenario "happy path claim" do when_early_years_payment_provider_journey_configuration_exists diff --git a/spec/forms/journeys/early_years_payment/provider/email_address_form_spec.rb b/spec/forms/journeys/early_years_payment/provider/email_address_form_spec.rb index 245c07ddc4..3c8d07318b 100644 --- a/spec/forms/journeys/early_years_payment/provider/email_address_form_spec.rb +++ b/spec/forms/journeys/early_years_payment/provider/email_address_form_spec.rb @@ -14,75 +14,79 @@ it { should have_attributes(email_address: email_address) } - describe "#save" do - subject { form.save } + context "when the email address is eligible" do + before { create(:eligible_ey_provider, primary_key_contact_email_address: email_address) } - around do |example| - travel_to DateTime.new(2024, 1, 1, 12, 0, 0) do - example.run - end - end - - before do - allow(OneTimePassword::Generator).to receive(:new).and_return( - instance_double(OneTimePassword::Generator, code: "111111") - ) - end + describe "#save" do + subject { form.save } - let(:policy) { journey_session.answers.policy } - let(:claim_subject) { I18n.t("#{policy.locale_key}.claim_subject") } + around do |example| + travel_to DateTime.new(2024, 1, 1, 12, 0, 0) do + example.run + end + end - it { should be_truthy } + before do + allow(OneTimePassword::Generator).to receive(:new).and_return( + instance_double(OneTimePassword::Generator, code: "111111") + ) + end - it "sets the email address" do - subject - expect(journey_session.reload.answers.email_address).to( - eq(email_address) - ) - end + let(:policy) { journey_session.answers.policy } + let(:claim_subject) { I18n.t("#{policy.locale_key}.claim_subject") } - it "sends an email" do - subject + it { should be_truthy } - expect(email_address).to have_received_email( - "e0b78a08-601b-40ba-a97f-61fb00a7c951", - magic_link: "https://www.example.com/early-years-payment-provider/consent?code=111111" - ) - end + it "sets the email address" do + subject + expect(journey_session.reload.answers.email_address).to( + eq(email_address) + ) + end - it "updates sent_one_time_password_at" do - subject - expect(journey_session.answers.sent_one_time_password_at).to( - eq(DateTime.new(2024, 1, 1, 12, 0, 0)) - ) - end + it "sends an email" do + subject - it "resets email_verified" do - subject - expect(journey_session.answers.email_verified).to be_nil - end + expect(email_address).to have_received_email( + "e0b78a08-601b-40ba-a97f-61fb00a7c951", + magic_link: "https://www.example.com/early-years-payment-provider/consent?code=111111" + ) + end - context "when the email address has been previously verified, and a new one is submitted" do - before do - journey_session.answers.assign_attributes(email_address: "new@example.com", email_verified: true) - journey_session.save! + it "updates sent_one_time_password_at" do + subject + expect(journey_session.answers.sent_one_time_password_at).to( + eq(DateTime.new(2024, 1, 1, 12, 0, 0)) + ) end it "resets email_verified" do subject expect(journey_session.answers.email_verified).to be_nil end - end - context "when the email address submitted has been previously verified, and is the same" do - before do - journey_session.answers.assign_attributes(email_address: email_address, email_verified: true) - journey_session.save! + context "when the email address has been previously verified, and a new one is submitted" do + before do + journey_session.answers.assign_attributes(email_address: "new@example.com", email_verified: true) + journey_session.save! + end + + it "resets email_verified" do + subject + expect(journey_session.answers.email_verified).to be_nil + end end - it "returns email_verified" do - subject - expect(journey_session.answers.email_verified).to be true + context "when the email address submitted has been previously verified, and is the same" do + before do + journey_session.answers.assign_attributes(email_address: email_address, email_verified: true) + journey_session.save! + end + + it "returns email_verified" do + subject + expect(journey_session.answers.email_verified).to be true + end end end end diff --git a/spec/models/eligible_ey_provider_spec.rb b/spec/models/eligible_ey_provider_spec.rb new file mode 100644 index 0000000000..b34a272fae --- /dev/null +++ b/spec/models/eligible_ey_provider_spec.rb @@ -0,0 +1,28 @@ +require "rails_helper" + +describe EligibleEyProvider do + describe ".eligible_email?" do + subject { described_class.eligible_email?(email) } + + let!(:eligible_ey_provider) { create(:eligible_ey_provider) } + + context "with a valid primary email address" do + let(:email) { eligible_ey_provider.primary_key_contact_email_address } + + it { is_expected.to be true } + end + + context "with a valid secondary email address" do + let(:email) { eligible_ey_provider.secondary_contact_email_address } + + it { is_expected.to be true } + end + + context "with an invalid address" do + let(:email) { "some.other.email@example.com" } + it { is_expected.to be false } + end + + context "an empty email address" + end +end