Module: DaVinciCRDTestKit::V221::HookRequestResourceResolution

Included in:
CoverageInformationCardAbsenceTest, 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)


111
112
113
114
115
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 111

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



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

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

#coverage_information_extension_only_payload?(resource_hash) ⇒ Boolean

Returns:

  • (Boolean)


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

def coverage_information_extension_only_payload?(resource_hash)
  return false unless resource_hash.is_a?(Hash)

  strip_coverage_info_extensions(resource_hash).keys.all? { |key| ['resourceType', 'id', 'meta'].include?(key) }
end

#fallback_source_resource(request_body, target_type, target_id) ⇒ Object



85
86
87
88
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 85

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



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 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



80
81
82
83
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 80

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



132
133
134
135
136
137
138
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 132

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



140
141
142
143
144
145
146
147
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 140

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 117

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



90
91
92
93
94
95
96
97
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 90

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

#normalize_value(value) ⇒ Object



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 63

def normalize_value(value)
  case value
  when Hash
    value.transform_values { |child| normalize_value(child) }
  when Array
    value.map { |child| normalize_value(child) }.sort_by(&:to_json)
  else
    value
  end
end

#only_coverage_information_changed?(source_resource_hash, updated_resource_hash) ⇒ Boolean

Returns:

  • (Boolean)


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

def only_coverage_information_changed?(source_resource_hash, updated_resource_hash)
  return true if coverage_information_extension_only_payload?(updated_resource_hash)

  source_without_coverage_info = normalize_value(strip_coverage_info_extensions(source_resource_hash))
  updated_without_coverage_info = normalize_value(strip_coverage_info_extensions(updated_resource_hash))

  source_without_coverage_info == updated_without_coverage_info
end

#parse_bundle(bundle_hash) ⇒ Object



158
159
160
161
162
163
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 158

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



165
166
167
168
169
170
171
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 165

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



149
150
151
152
153
154
155
156
# File 'lib/davinci_crd_test_kit/server/v2.2.1/verify_response/hook_request_resource_resolution.rb', line 149

def reference_parts(reference)
  return if reference.blank?

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

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

#strip_coverage_info_extensions(resource_hash) ⇒ Object



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

def strip_coverage_info_extensions(resource_hash)
  normalized_hash = resource_hash.deep_dup
  return normalized_hash unless normalized_hash['extension'].is_a?(Array)

  normalized_hash['extension'] = normalized_hash['extension'].reject do |extension|
    extension['url'] == DaVinciCRDTestKit::CardsIdentification::COVERAGE_INFO_EXT_URL
  end
  normalized_hash.delete('extension') if normalized_hash['extension'].empty?
  normalized_hash
end