Module: DaVinciCRDTestKit::V221::HookRequestResourceResolution

Included in:
CoverageInformationSystemActionValidationTest, OrderDispatchCoverageInformationTest
Defined in:
lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb

Instance Method Summary collapse

Instance Method Details

#appointment_based_on_matches_target?(appointment, target_type, target_id) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 74

def appointment_based_on_matches_target?(appointment, target_type, target_id)
  Array(appointment.basedOn).any? do |reference|
    reference_parts(reference.reference) == [target_type, target_id]
  end
end

#appointment_book_service_request(request_body, appointments_bundle_hash, target_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 62

def appointment_book_service_request(request_body, appointments_bundle_hash, target_id)
  target_type = 'ServiceRequest'
  appointments_bundle = parse_bundle(appointments_bundle_hash)
  appointment = (appointments_bundle&.entry || [])
    .filter_map(&:resource)
    .find { |candidate| appointment_based_on_matches_target?(candidate, target_type, target_id) }
  return unless appointment

  find_resource_in_prefetch(request_body, target_type, target_id) ||
    find_resource_in_bundle(mock_ehr_bundle_resource, target_type, target_id)
end

#fallback_source_resource(request_body, target_type, target_id) ⇒ Object



48
49
50
51
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 48

def fallback_source_resource(request_body, target_type, target_id)
  find_resource_in_prefetch(request_body, target_type, target_id) ||
    find_resource_in_bundle(mock_ehr_bundle_resource, target_type, target_id)
end

#find_action_source_resource(action, request) ⇒ Object

Resolve the original resource being updated by a systemAction.

  • appointment-book: look in context.appointments; if the action targets a ServiceRequest, follow the Appointment basedOn reference and resolve that from prefetch or mock EHR data

  • order-sign/order-select: look in context.draftOrders

  • other hooks: resolve from prefetch or mock EHR data because context may carry only ids/references



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 24

def find_action_source_resource(action, request)
  action_resource = action['resource']
  return unless action_resource.is_a?(Hash)

  target_type = action_resource['resourceType']
  target_id = action_resource['id']
  request_body = parse_request_body(request)
  return unless target_type.present? && target_id.present? && request_body

  hook_context_resource(request_body, target_type, target_id) ||
    fallback_source_resource(request_body, target_type, target_id)
end

#find_appointment_book_resource(request_body, target_type, target_id) ⇒ Object



37
38
39
40
41
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 37

def find_appointment_book_resource(request_body, target_type, target_id)
  appointments_bundle = request_body.dig('context', 'appointments')
  find_resource_in_bundle(appointments_bundle, target_type, target_id) ||
    appointment_book_service_request(request_body, appointments_bundle, target_id)
end

#find_draft_orders_resource(request_body, target_type, target_id) ⇒ Object



43
44
45
46
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 43

def find_draft_orders_resource(request_body, target_type, target_id)
  draft_orders_bundle = request_body.dig('context', 'draftOrders')
  find_resource_in_bundle(draft_orders_bundle, target_type, target_id)
end

#find_resource_by_reference(request_body, reference) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 95

def find_resource_by_reference(request_body, reference)
  target_type, target_id = reference_parts(reference)
  return unless target_type.present? && target_id.present?

  find_resource_in_prefetch(request_body, target_type, target_id) ||
    find_resource_in_bundle(mock_ehr_bundle_resource, target_type, target_id)
end

#find_resource_in_bundle(bundle_hash, target_type, target_id) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 103

def find_resource_in_bundle(bundle_hash, target_type, target_id)
  bundle = parse_bundle(bundle_hash)
  return unless bundle&.entry

  bundle.entry
    .filter_map(&:resource)
    .find { |resource| resource.resourceType == target_type && resource.id == target_id }
end

#find_resource_in_prefetch(request_body, target_type, target_id) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 80

def find_resource_in_prefetch(request_body, target_type, target_id)
  Array(request_body['prefetch']&.values).each do |prefetched_value|
    if prefetched_value.is_a?(Hash) &&
       prefetched_value['resourceType'] == target_type &&
       prefetched_value['id'] == target_id
      return FHIR.from_contents(prefetched_value.to_json)
    end

    resource = find_resource_in_bundle(prefetched_value, target_type, target_id)
    return resource if resource
  end

  nil
end

#hook_context_resource(request_body, target_type, target_id) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 53

def hook_context_resource(request_body, target_type, target_id)
  case tested_hook_name
  when 'appointment-book'
    find_appointment_book_resource(request_body, target_type, target_id)
  when 'order-sign', 'order-select'
    find_draft_orders_resource(request_body, target_type, target_id)
  end
end

#matching_request_for_action(action) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 10

def matching_request_for_action(action)
  requests.find do |request|
    response = JSON.parse(request.response_body)
    request.status == 200 && Array(response['systemActions']).any? { |candidate| candidate == action }
  rescue JSON::ParserError
    false
  end
end

#mock_ehr_bundle_resourceObject



4
5
6
7
8
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 4

def mock_ehr_bundle_resource
  @mock_ehr_bundle_resource ||= JSON.parse(mock_ehr_bundle) if mock_ehr_bundle.present?
rescue JSON::ParserError
  nil
end

#parse_bundle(bundle_hash) ⇒ Object



121
122
123
124
125
126
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 121

def parse_bundle(bundle_hash)
  bundle = FHIR.from_contents(bundle_hash.to_json)
  bundle if bundle.is_a?(FHIR::Bundle)
rescue StandardError
  nil
end

#parse_request_body(request) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 128

def parse_request_body(request)
  return unless request&.request_body.present?

  JSON.parse(request.request_body)
rescue JSON::ParserError
  nil
end

#reference_parts(reference) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 112

def reference_parts(reference)
  return if reference.blank?

  parts = reference.split('/')
  return unless parts.length >= 2

  [parts[-2], parts[-1]]
end