Skip to content

Commit

Permalink
DRY; plus basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eanders committed Jun 11, 2024
1 parent ebdb068 commit cae5f49
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/models/client_opportunity_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) }
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/availability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/unavailable_as_candidate_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion spec/models/client_opportunity_match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cae5f49

Please sign in to comment.