@@ -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
0 commit comments