From 2ddb4003a6761ac0c28eb9897613a13f7e471ec2 Mon Sep 17 00:00:00 2001 From: Elliot Date: Mon, 23 Sep 2024 15:50:23 -0400 Subject: [PATCH] CE Events from CAS Matches (#862) * Find the reason for decline and cancelation more effectively * Bug fixes for missing data * Fix for change in API --- app/models/client_opportunity_match.rb | 29 ++++++++++++++++++++++---- app/models/warehouse/referral_event.rb | 12 +++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/models/client_opportunity_match.rb b/app/models/client_opportunity_match.rb index 901b07d91..584d6028e 100644 --- a/app/models/client_opportunity_match.rb +++ b/app/models/client_opportunity_match.rb @@ -457,16 +457,37 @@ def current_decision end def unsuccessful_decision - initialized_decisions.where.not(decline_reason_id: nil)&.first || - initialized_decisions.where.not(administrative_cancel_reason_id: nil)&.first + return canceled_decision if canceled? + + declined_decision + end + + def unsuccessful_reason + return unsuccessful_decision&.administrative_cancel_reason if canceled? + + unsuccessful_decision&.decline_reason end + # Find the latest decision in the route that was declined with a reason + # If that doesn't exist, pull the latest initialized decision that was declined def declined_decision - @declined_decision ||= initialized_decisions.where.not(decline_reason_id: nil)&.first - @declined_decision ||= initialized_decisions.where(status: :declined)&.last + decision_order = match_route.class.match_steps_for_reporting.keys + @declined_decision ||= initialized_decisions.order_as_specified(type: decision_order).where.not(decline_reason_id: nil)&.last + @declined_decision ||= initialized_decisions.order_as_specified(type: decision_order).where(status: :declined)&.last @declined_decision end + # Find the latest decision in the route that was canceled with a reason + # If that doesn't exist, pull the latest initialized decision that was canceled + def canceled_decision + return nil unless canceled? + + decision_order = match_route.class.match_steps_for_reporting.keys + @canceled_decision ||= initialized_decisions.order_as_specified(type: decision_order).where.not(administrative_cancel_reason_id: nil)&.last + @canceled_decision ||= initialized_decisions.order_as_specified(type: decision_order).where(status: :canceled)&.last + @canceled_decision + end + def clear_current_decision_cache! @current_decision = nil end diff --git a/app/models/warehouse/referral_event.rb b/app/models/warehouse/referral_event.rb index a37a315af..f5299ffbb 100644 --- a/app/models/warehouse/referral_event.rb +++ b/app/models/warehouse/referral_event.rb @@ -20,9 +20,11 @@ def accepted end def rejected - reason = client_opportunity_match.current_decision&.decline_reason + reason = client_opportunity_match.unsuccessful_reason + unsuccessful_decision = client_opportunity_match.unsuccessful_decision + closed_timestamp = unsuccessful_decision&.updated_at || client_opportunity_match.updated_at if reason&.referral_result.present? - update(referral_result: reason.referral_result, referral_result_date: reason.referral_result_date) + update(referral_result: reason.referral_result, referral_result_date: closed_timestamp) else destroy end @@ -62,13 +64,15 @@ def self.sync_match_referrals! ) end else - reason = match.current_decision&.decline_reason + reason = match.unsuccessful_reason + unsuccessful_decision = match.unsuccessful_decision + closed_timestamp = unsuccessful_decision&.updated_at || match.updated_at if reason.present? && reason.referral_result.present? if event.referral_result != reason.referral_result event.update( event: match.sub_program.event, referral_result: reason.referral_result, - referral_result_date: reason.referral_result_date, + referral_result_date: closed_timestamp, ) end else