Skip to content

Commit c79523b

Browse files
eselkinCopilot
andauthored
Fix/make sure start date before end date (#27611)
* Makes sure start date before end date (Always today -- so request must always be past) to get AVS --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 4a1251f commit c79523b

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

lib/unified_health_data/service.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,16 @@ def get_immunizations
287287
# Use of this is behind va_online_scheduling_uhd_avs_metadata flipper
288288
def get_all_avs_metadata(start_date:, end_date:) # rubocop:disable Metrics/MethodLength
289289
validate_icn!
290+
start_d = (start_date || default_start_date).to_s
291+
end_d = (end_date || default_end_date).to_s
292+
293+
if start_d > end_d
294+
Rails.logger.error("UHD: start_d not before end_d | start_d: #{start_d}, end_d: #{end_d}")
295+
return [[], []]
296+
end
297+
290298
with_monitoring do
291-
response = uhd_client.get_all_avs(patient_id: @user.icn, start_date:, end_date:)
299+
response = uhd_client.get_all_avs(patient_id: @user.icn, start_date: start_d, end_date: end_d)
292300
# SCDF returns a bundle: DocumentReference, Encounter, and other types.
293301
body = response.body
294302
if body.nil? || !body.is_a?(Hash) || body['entry'].blank?

modules/vaos/app/services/vaos/v2/appointments_service.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ def get_appointment(appointment_id, include = {}, tp_client = 'vagov')
268268
# We always fetch facility and clinic information when getting a single appointment
269269
include[:facilities] = true
270270
include[:clinics] = true
271+
start_dt = appointment[:start]&.to_datetime
272+
should_fetch_avs = include[:avs] && start_dt&.past?
271273

272-
avs_metadata = fetch_all_avs_metadata(appointment[:start]&.to_datetime, [appointment],
273-
include_avs: include[:avs])
274+
avs_metadata = fetch_all_avs_metadata(start_dt, [appointment], include_avs: should_fetch_avs)
274275
prepare_appointment(appointment, include, avs_metadata)
275276

276277
check_appointments_migration_override([appointment])
@@ -519,22 +520,30 @@ def extract_facility_identifiers(appointments)
519520
# Returns a hash indexed by appointment id (without any prefix like CERNER)
520521
#
521522
# @return [Hash{String => Array<UnifiedHealthData::AfterVisitSummary>}]
522-
def fetch_all_avs_metadata(start_date, appointments = [], include_avs: false)
523+
def fetch_all_avs_metadata(start_date, appointments = [], include_avs: false) # rubocop:disable Metrics/MethodLength
523524
return {} unless include_avs
524525
return {} unless Flipper.enabled?(:va_online_scheduling_uhd_avs_metadata, user) &&
525526
Flipper.enabled?(APPOINTMENTS_FETCH_OH_AVS, user) &&
526527
appointments.any? { |appt| VAOS::AppointmentsHelper.cerner?(appt) }
527528
return {} if user.icn.nil? || !start_date.respond_to?(:to_date)
528529

529-
start_date_str = start_date.to_date.to_s
530-
end_date_str = Time.zone.today.to_s
531-
doc_refs, encounters = unified_health_data_service.get_all_avs_metadata(start_date: start_date_str,
532-
end_date: end_date_str)
530+
start_date = start_date.to_date
531+
end_date = Time.zone.today
532+
533+
return {} if start_date > end_date
534+
535+
doc_refs, encounters = unified_health_data_service.get_all_avs_metadata(start_date:, end_date:)
533536
clinical_notes_adapter.build_avs_metadata_by_appointment(encounters, doc_refs)
534537
rescue => e
535538
err_stack = e.backtrace.reject { |line| line.include?('gems') }.compact.join("\n ")
536-
Rails.logger.error("VAOS: Error retrieving AVS metadata for user #{user.user_account_uuid}:" \
537-
"#{e.class}, #{e.message} \n #{err_stack}")
539+
original_status = e.respond_to?(:original_status) ? e.original_status : nil
540+
Rails.logger.error(
541+
"VAOS: Error retrieving AVS metadata for user #{user.user_account_uuid}:" \
542+
"#{e.class}, #{e.message}" \
543+
" | start_date: #{start_date}, end_date: #{end_date}" \
544+
" | upstream_status: #{original_status}" \
545+
" \n #{err_stack}"
546+
)
538547
{}
539548
end
540549

modules/vaos/spec/requests/vaos/v2/appointments_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,24 @@ def stub_clinics
893893
end
894894
end
895895

896+
it 'does not fetch avs when OH appointment is in the future even if avs is requested' do
897+
Timecop.freeze(DateTime.parse('2023-09-01T12:00:00Z')) do
898+
VCR.use_cassette('vaos/v2/appointments/get_appointment_200_with_facility_200_with_avs_cerner',
899+
match_requests_on: %i[method path query]) do
900+
allow(Rails.logger).to receive(:info).at_least(:once)
901+
expect_any_instance_of(VAOS::V2::AppointmentsService).to receive(:fetch_all_avs_metadata)
902+
.with(anything, anything, include_avs: false).and_call_original
903+
904+
get '/vaos/v2/appointments/70060?_include=avs', headers: inflection_header
905+
expect(response).to have_http_status(:ok)
906+
data = JSON.parse(response.body)['data']
907+
908+
expect(data['id']).to eq('70060')
909+
expect(data['attributes']['avsPdf']).to be_nil
910+
end
911+
end
912+
end
913+
896914
it 'has access and returns appointment with OH avs' do
897915
avs_show_metadata = {
898916
'523938333130383130' => [

spec/lib/unified_health_data/service_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,27 @@
18951895
returned_types = all_returned.map { |entry| entry['resourceType'] }.uniq
18961896
expect(returned_types).to contain_exactly('DocumentReference', 'Encounter')
18971897
end
1898+
1899+
it 'returns empty arrays when start_date is after end_date' do
1900+
expect_any_instance_of(UnifiedHealthData::Client).not_to receive(:get_all_avs)
1901+
result = service.get_all_avs_metadata(start_date: '2025-12-31', end_date: '2025-01-01')
1902+
expect(result).to eq([[], []])
1903+
end
1904+
1905+
it 'does not short-circuit when start_date equals end_date' do
1906+
result = service.get_all_avs_metadata(start_date: '2025-06-15', end_date: '2025-06-15')
1907+
doc_refs, encounters = result
1908+
expect(doc_refs.size).to eq(2)
1909+
expect(encounters.size).to eq(2)
1910+
end
1911+
1912+
it 'accepts Date objects for start_date and end_date' do
1913+
result = service.get_all_avs_metadata(start_date: Date.new(2025, 1, 1), end_date: Date.new(2025, 12, 31))
1914+
1915+
doc_refs, encounters = result
1916+
expect(doc_refs.size).to eq(2)
1917+
expect(encounters.size).to eq(2)
1918+
end
18981919
end
18991920

19001921
describe '#get_avs_binary_data' do

0 commit comments

Comments
 (0)