diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 945f5e8f2f..3a92d27071 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,4 +15,8 @@ def sign_in_user def current_user @current_user ||= sign_in_user&.user end + + def support_user + @support_user ||= sign_in_user&.support_user + end end diff --git a/app/models/dfe_sign_in_user.rb b/app/models/dfe_sign_in_user.rb index f3b6c46045..19f8a4cd7e 100644 --- a/app/models/dfe_sign_in_user.rb +++ b/app/models/dfe_sign_in_user.rb @@ -50,6 +50,12 @@ def user @user ||= user_klass.find_by(email:) end + def support_user + @support_user ||= support_user_klass.find(user.id) + rescue ActiveRecord::RecordNotFound + nil + end + def self.end_session!(session) session.clear end @@ -64,4 +70,14 @@ def user_klass Placements::User end end + + def support_user_klass + case service + # when :claims + # Claims::SupportUser + when :placements + Placements::SupportUser + end + + end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 4ab5b0e272..7fb448fa90 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -34,4 +34,8 @@ factory :placements_user, class: "Placements::User", parent: :user do service { "placements" } end + + factory :placements_support_user, class: "Placements::SupportUser", parent: :user do + service { "placements" } + end end diff --git a/spec/models/dfe_sign_in_user_spec.rb b/spec/models/dfe_sign_in_user_spec.rb index 4d49734bf0..91503928ac 100644 --- a/spec/models/dfe_sign_in_user_spec.rb +++ b/spec/models/dfe_sign_in_user_spec.rb @@ -46,6 +46,63 @@ end end + describe "#support_user" do + describe "claims service" do + it "returns the claims user" do + claims_user = create(:claims_user) + + session = { + "dfe_sign_in_user" => { + "first_name" => claims_user.first_name, + "last_name" => claims_user.last_name, + "email" => claims_user.email + }, + "service" => :claims + } + + dfe_sign_in_user = DfESignInUser.load_from_session(session) + + expect(dfe_sign_in_user.user).to eq claims_user + end + end + + describe "placements service" do + it "returns nil if user is not support" do + placements_user = create(:placements_user) + + session = { + "dfe_sign_in_user" => { + "first_name" => placements_user.first_name, + "last_name" => placements_user.last_name, + "email" => placements_user.email + }, + "service" => :placements + } + + dfe_sign_in_user = DfESignInUser.load_from_session(session) + + expect(dfe_sign_in_user.support_user).to eq nil + end + + it "returns the placements support user" do + support_placements_user = create(:placements_support_user) + + session = { + "dfe_sign_in_user" => { + "first_name" => support_placements_user.first_name, + "last_name" => support_placements_user.last_name, + "email" => support_placements_user.email + }, + "service" => :placements + } + + dfe_sign_in_user = DfESignInUser.load_from_session(session) + + expect(dfe_sign_in_user.support_user).to eq support_placements_user + end + end + end + describe "#user" do describe "claims service" do it "returns the claims user" do