From d2c16ecc458431d252809b3661746888e22f7c21 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Wed, 9 Oct 2024 11:59:41 +0100 Subject: [PATCH 1/2] Ensure date of birth is present when claiming ECT We were able to get past this check because we were calling the API regardless of whether the pending_induction_submission was valid. This change ensures we return before calling if validation fails. --- .../appropriate_bodies/claim_an_ect/find_ect.rb | 2 ++ .../claim_an_ect/find_ect_spec.rb | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/services/appropriate_bodies/claim_an_ect/find_ect.rb b/app/services/appropriate_bodies/claim_an_ect/find_ect.rb index 9ce3f21d..6212c10d 100644 --- a/app/services/appropriate_bodies/claim_an_ect/find_ect.rb +++ b/app/services/appropriate_bodies/claim_an_ect/find_ect.rb @@ -15,6 +15,8 @@ def import_from_trs! # b) as another pending induction submission? # we probably want a guard clause here or to make the if statement # below a case and add different errors to the :base + return unless pending_induction_submission.valid?(:find_ect) + pending_induction_submission.assign_attributes(appropriate_body:, **find_matching_record_in_trs) pending_induction_submission.save(context: :find_ect) end diff --git a/spec/services/appropriate_bodies/claim_an_ect/find_ect_spec.rb b/spec/services/appropriate_bodies/claim_an_ect/find_ect_spec.rb index 14bac9ff..1c23512a 100644 --- a/spec/services/appropriate_bodies/claim_an_ect/find_ect_spec.rb +++ b/spec/services/appropriate_bodies/claim_an_ect/find_ect_spec.rb @@ -1,6 +1,7 @@ RSpec.describe AppropriateBodies::ClaimAnECT::FindECT do + let(:appropriate_body) { FactoryBot.build(:appropriate_body) } + describe "#initialize" do - let(:appropriate_body) { FactoryBot.build(:appropriate_body) } let(:pending_induction_submission) { FactoryBot.create(:pending_induction_submission) } it "assigns the provided appropriate body and pending induction submission params" do @@ -11,7 +12,17 @@ end end - describe "#import_from_trs" do + describe "#import_from_trs!" do + context "when the pending_induction_submission is invalid" do + let(:pending_induction_submission) { FactoryBot.create(:pending_induction_submission, date_of_birth: nil) } + + it "returns nil" do + find_ect = AppropriateBodies::ClaimAnECT::FindECT.new(appropriate_body:, pending_induction_submission:) + + expect(find_ect.import_from_trs!).to be_nil + end + end + context "when there is a match" do it "makes a call to the TRS API client with the expected parameters" it "assigns the incoming attributes to the pending_induction_submission and returns it" From 6b5f8cd0321af3a7bcc24561d4e14af4f68fcc4f Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Wed, 9 Oct 2024 12:14:27 +0100 Subject: [PATCH 2/2] Ensure date_of_birth is provided to find_teacher This is an extra level of safety that we're not retrieiving teachers from the TRS API when claiming them with just one bit of information - we need two. --- lib/TRS/api_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/TRS/api_client.rb b/lib/TRS/api_client.rb index 01766fda..14cfbc35 100644 --- a/lib/TRS/api_client.rb +++ b/lib/TRS/api_client.rb @@ -9,7 +9,7 @@ def initialize end end - def find_teacher(trn:, date_of_birth: nil) + def find_teacher(trn:, date_of_birth:) response = @connection.get("/v3/persons/#{trn}", dateOfBirth: date_of_birth) if response.success?