From cae5f492f3283fd6ee9c10a2013be98017b3eb32 Mon Sep 17 00:00:00 2001 From: Elliot Anders Date: Tue, 11 Jun 2024 09:14:26 -0400 Subject: [PATCH] DRY; plus basic tests --- app/models/client_opportunity_match.rb | 8 +++---- app/models/concerns/availability.rb | 4 ++-- app/models/unavailable_as_candidate_for.rb | 4 ++++ spec/models/client_opportunity_match_spec.rb | 23 +++++++++++++++++++- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/models/client_opportunity_match.rb b/app/models/client_opportunity_match.rb index 15c932de7..70b915c21 100644 --- a/app/models/client_opportunity_match.rb +++ b/app/models/client_opportunity_match.rb @@ -646,7 +646,7 @@ def restrictions_on_reopening def reopen!(contact, user: nil) self.class.transaction do - client.make_unavailable_in(match_route: match_route, expires_at: nil, user: user, match: self, reason: 'Active Match') + client.make_unavailable_in(match_route: match_route, expires_at: nil, user: user, match: self, reason: UnavailableAsCandidateFor::ACTIVE_MATCH_TEXT) update(closed: false, active: true, closed_reason: nil) current_decision.update(status: :pending) MatchEvents::Reopened.create(match_id: id, contact_id: contact.id) @@ -661,7 +661,7 @@ def reopen!(contact, user: nil) def activate!(touch_referral_event: true, user: nil) self.class.transaction do update!(active: true) - client.make_unavailable_in(match_route: match_route, expires_at: nil, user: user, match: self, reason: 'Active Match') if match_route.should_prevent_multiple_matches_per_client + client.make_unavailable_in(match_route: match_route, expires_at: nil, user: user, match: self, reason: UnavailableAsCandidateFor::ACTIVE_MATCH_TEXT) if match_route.should_prevent_multiple_matches_per_client opportunity.update(available_candidate: false) add_default_contacts! requirements_with_inherited.each { |req| req.apply_to_match(self) } @@ -750,10 +750,10 @@ def succeeded!(touch_referral_event: true, user: nil) end client.update available: false # Prevent matching on any route - client.make_unavailable_in_all_routes(user: user, match: self, reason: 'Successful Match') + client.make_unavailable_in_all_routes(user: user, match: self, reason: UnavailableAsCandidateFor::SUCCESSFUL_MATCH_TEXT) else # Prevent matching on this route again - client.make_unavailable_in(match_route: route, user: user, match: self, reason: 'Successful Match') + client.make_unavailable_in(match_route: route, user: user, match: self, reason: UnavailableAsCandidateFor::SUCCESSFUL_MATCH_TEXT) end # Cleanup other matches on the same opportunity diff --git a/app/models/concerns/availability.rb b/app/models/concerns/availability.rb index 25f79afe4..0f19e1554 100644 --- a/app/models/concerns/availability.rb +++ b/app/models/concerns/availability.rb @@ -86,7 +86,7 @@ def unavailable( # rubocop:disable Metrics/ParameterLists client_opportunity_matches.open.each(&:destroy!) # Prevent any new matches for this client # This will re-queue the client once the date is passed - make_unavailable_in_all_routes(expires_at: expires_at, user: user, match: match, reason: 'Parked') + make_unavailable_in_all_routes(expires_at: expires_at, user: user, match: match, reason: UnavailableAsCandidateFor::PARKED_TEXT) end if cancel_specific @@ -103,7 +103,7 @@ def unavailable( # rubocop:disable Metrics/ParameterLists opportunity.update!(available_candidate: true) # Delete any non-active open matches client_opportunity_matches.on_route(route).proposed.each(&:delete) - make_unavailable_in(match_route: route, expires_at: expires_at, user: user, match: match, reason: 'Parked') + make_unavailable_in(match_route: route, expires_at: expires_at, user: user, match: match, reason: UnavailableAsCandidateFor::PARKED_TEXT) end end Matching::RunEngineJob.perform_later diff --git a/app/models/unavailable_as_candidate_for.rb b/app/models/unavailable_as_candidate_for.rb index 2c0dea088..3258355ca 100644 --- a/app/models/unavailable_as_candidate_for.rb +++ b/app/models/unavailable_as_candidate_for.rb @@ -14,6 +14,10 @@ class UnavailableAsCandidateFor < ApplicationRecord belongs_to :match, class_name: 'ClientOpportunityMatch', optional: true has_paper_trail + ACTIVE_MATCH_TEXT = 'Active Match'.freeze + SUCCESSFUL_MATCH_TEXT = 'Successful Match'.freeze + PARKED_TEXT = 'Parked'.freeze + scope :for_route, ->(route) do route_name = MatchRoutes::Base.route_name_from_route(route) where(match_route_type: route_name) diff --git a/spec/models/client_opportunity_match_spec.rb b/spec/models/client_opportunity_match_spec.rb index 1919805e5..b47c809d7 100644 --- a/spec/models/client_opportunity_match_spec.rb +++ b/spec/models/client_opportunity_match_spec.rb @@ -4,7 +4,7 @@ RSpec.describe ClientOpportunityMatch, type: :model do let!(:match) { create :client_opportunity_match, active: false } let(:priority) { create :priority_vispdat_priority } - let(:default_route) { create :default_route, match_prioritization: priority } + let(:default_route) { create :default_route, match_prioritization: priority, should_prevent_multiple_matches_per_client: true } let(:provider_route) { create :provider_route, match_prioritization: priority } describe 'Match initiation on the default route' do describe 'when the initial user has email schedule set to immediate' do @@ -53,6 +53,27 @@ }.by(1) end end + it 'generates an unavailable for on the route on activation' do + match.activate!(user: user) + aggregate_failures do + expect(UnavailableAsCandidateFor.count).to eq(1) + expect(UnavailableAsCandidateFor.first.route).to eq(default_route) + expect(UnavailableAsCandidateFor.first.reason).to eq(UnavailableAsCandidateFor::ACTIVE_MATCH_TEXT) + expect(UnavailableAsCandidateFor.first.user_id).to eq(user.id) + expect(UnavailableAsCandidateFor.first.match_id).to eq(match.id) + end + end + + it 'generates an unavailable for on the route on success' do + match.succeeded!(user: user) + aggregate_failures do + expect(UnavailableAsCandidateFor.count).to eq(MatchRoutes::Base.all_routes.count) + expect(UnavailableAsCandidateFor.pluck(:match_route_type).sort).to eq(MatchRoutes::Base.all_routes.map(&:name).sort) + expect(UnavailableAsCandidateFor.pluck(:reason).uniq).to eq([UnavailableAsCandidateFor::SUCCESSFUL_MATCH_TEXT]) + expect(UnavailableAsCandidateFor.pluck(:user_id).uniq).to eq([user.id]) + expect(UnavailableAsCandidateFor.pluck(:match_id).uniq).to eq([match.id]) + end + end end describe 'when the initial user has email schedule set to daily' do