Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit 4c4a239

Browse files
authored
eselkin/Add uhd batch avs groups resourceTypes and selects Document and Encounter types (#27511)
* Fixes extraction response that is just 1 Bundle of various resourceTypes. * Slight simplification to cut down on repeat functions. * Add a test to make sure we cover that the group by works and selects correctly * reduce lines * repeated .body * fix info to debug * Not sure how that got there
1 parent 8ba3e24 commit 4c4a239

2 files changed

Lines changed: 93 additions & 75 deletions

File tree

lib/unified_health_data/service.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,25 @@ def get_immunizations
285285
end
286286

287287
# Use of this is behind va_online_scheduling_uhd_avs_metadata flipper
288-
def get_all_avs_metadata(start_date:, end_date:)
288+
def get_all_avs_metadata(start_date:, end_date:) # rubocop:disable Metrics/MethodLength
289+
validate_icn!
289290
with_monitoring do
290291
response = uhd_client.get_all_avs(patient_id: @user.icn, start_date:, end_date:)
291-
# SCDF returns a bundle of bundles: DocumentReference bundle, Encounter bundle (and possibly a third).
292-
document_reference_bundle, encounter_bundle = response.body&.dig('entry')
293-
doc_ref_entries = extract_all_entries(document_reference_bundle)
294-
encounter_entries = extract_all_entries(encounter_bundle)
292+
# SCDF returns a bundle: DocumentReference, Encounter, and other types.
293+
body = response.body
294+
if body.nil? || !body.is_a?(Hash) || body['entry'].blank?
295+
user_uuid = @user.user_account_uuid
296+
Rails.logger.debug { "UHD: Missing or invalid response for AVS metadata request for user #{user_uuid}." }
297+
return [[], []]
298+
end
299+
entries = extract_all_entries(body)
300+
grouped = entries.group_by { |entry| entry['resourceType'] }
301+
doc_ref_entries = grouped['DocumentReference'] || []
302+
encounter_entries = grouped['Encounter'] || []
295303
if doc_ref_entries.empty? || encounter_entries.empty?
296304
user_uuid = @user.user_account_uuid
297305
to_log = "DocumentReference entries: #{doc_ref_entries.size}, Encounter entries: #{encounter_entries.size}"
298-
Rails.logger.info("UHD: Missing expected bundles in AVS metadata response for user #{user_uuid}. #{to_log}")
306+
Rails.logger.debug { "UHD: Missing data in AVS metadata response for user #{user_uuid}. #{to_log}" }
299307
return [[], []]
300308
end
301309
[doc_ref_entries, encounter_entries]

spec/lib/unified_health_data/service_spec.rb

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,87 +1779,88 @@
17791779
describe '#get_all_avs_metadata' do
17801780
let(:all_avs_response) do
17811781
{
1782+
'resourceType' => 'Bundle',
1783+
'type' => 'collection',
17821784
'entry' => [
17831785
{
17841786
'resource' => {
1785-
'resourceType' => 'Bundle',
1786-
'entry' => [
1787-
{
1788-
'resource' => {
1789-
'resourceType' => 'DocumentReference',
1790-
'id' => 'doc-1',
1791-
'type' => {
1792-
'coding' => [
1793-
{
1794-
'system' => 'https://fhir.cerner.com/codeSet/72',
1795-
'code' => '4189669',
1796-
'display' => 'Ambulatory Patient Summary',
1797-
'userSelected' => true
1798-
},
1799-
{
1800-
'system' => 'http://loinc.org',
1801-
'code' => '96345-4',
1802-
'display' => 'Ambulatory Patient Summary',
1803-
'userSelected' => false
1804-
}
1805-
]
1806-
},
1807-
'context' => {
1808-
'encounter' => [
1809-
{ 'reference' => 'Encounter/enc-1' }
1810-
]
1811-
}
1787+
'resourceType' => 'DocumentReference',
1788+
'id' => 'doc-1',
1789+
'type' => {
1790+
'coding' => [
1791+
{
1792+
'system' => 'https://fhir.cerner.com/codeSet/72',
1793+
'code' => '4189669',
1794+
'display' => 'Ambulatory Patient Summary',
1795+
'userSelected' => true
1796+
},
1797+
{
1798+
'system' => 'http://loinc.org',
1799+
'code' => '96345-4',
1800+
'display' => 'Ambulatory Patient Summary',
1801+
'userSelected' => false
18121802
}
1813-
},
1814-
{
1815-
'resource' => {
1816-
'resourceType' => 'DocumentReference',
1817-
'id' => 'doc-2',
1818-
'type' => {
1819-
'coding' => [
1820-
{
1821-
'system' => 'https://fhir.cerner.com/codeSet/72',
1822-
'code' => '2820526',
1823-
'display' => 'Primary Care Note',
1824-
'userSelected' => true
1825-
}
1826-
]
1827-
},
1828-
'context' => {
1829-
'encounter' => [
1830-
{ 'reference' => 'Encounter/enc-2' }
1831-
]
1832-
}
1803+
]
1804+
},
1805+
'context' => {
1806+
'encounter' => [
1807+
{ 'reference' => 'Encounter/enc-1' }
1808+
]
1809+
}
1810+
}
1811+
},
1812+
{
1813+
'resource' => {
1814+
'resourceType' => 'DocumentReference',
1815+
'id' => 'doc-2',
1816+
'type' => {
1817+
'coding' => [
1818+
{
1819+
'system' => 'https://fhir.cerner.com/codeSet/72',
1820+
'code' => '2820526',
1821+
'display' => 'Primary Care Note',
1822+
'userSelected' => true
18331823
}
1834-
}
1824+
]
1825+
},
1826+
'context' => {
1827+
'encounter' => [
1828+
{ 'reference' => 'Encounter/enc-2' }
1829+
]
1830+
}
1831+
}
1832+
},
1833+
{
1834+
'resource' => {
1835+
'resourceType' => 'Encounter',
1836+
'id' => 'enc-1',
1837+
'appointment' => [
1838+
{ 'reference' => 'Appointment/4818609' }
18351839
]
18361840
}
18371841
},
18381842
{
18391843
'resource' => {
1840-
'resourceType' => 'Bundle',
1841-
'entry' => [
1842-
{
1843-
'resource' => {
1844-
'resourceType' => 'Encounter',
1845-
'id' => 'enc-1',
1846-
'appointment' => [
1847-
{ 'reference' => 'Appointment/4818609' }
1848-
]
1849-
}
1850-
},
1851-
{
1852-
'resource' => {
1853-
'resourceType' => 'Encounter',
1854-
'id' => 'enc-2',
1855-
'appointment' => [
1856-
{ 'reference' => 'Appointment/4818609' },
1857-
{ 'reference' => 'Appointment/9990001' }
1858-
]
1859-
}
1860-
}
1844+
'resourceType' => 'Encounter',
1845+
'id' => 'enc-2',
1846+
'appointment' => [
1847+
{ 'reference' => 'Appointment/4818609' },
1848+
{ 'reference' => 'Appointment/9990001' }
18611849
]
18621850
}
1851+
},
1852+
{
1853+
'resource' => {
1854+
'resourceType' => 'OperationOutcome',
1855+
'id' => 'oo-1',
1856+
'issue' => [{ 'severity' => 'information', 'code' => 'informational' }]
1857+
}
1858+
},
1859+
{
1860+
'resource' => {
1861+
'resourceType' => 'Appointment',
1862+
'id' => 'appt-1'
1863+
}
18631864
}
18641865
]
18651866
}
@@ -1882,6 +1883,15 @@
18821883
expect(encounters.first['id']).to eq('enc-1')
18831884
expect(encounters.last['id']).to eq('enc-2')
18841885
end
1886+
1887+
it 'excludes non-DocumentReference and non-Encounter resource types' do
1888+
result = service.get_all_avs_metadata(start_date: '2025-01-01', end_date: '2025-12-31')
1889+
1890+
doc_refs, encounters = result
1891+
all_returned = doc_refs + encounters
1892+
returned_types = all_returned.map { |entry| entry['resourceType'] }.uniq
1893+
expect(returned_types).to contain_exactly('DocumentReference', 'Encounter')
1894+
end
18851895
end
18861896

18871897
describe '#get_appt_avs' do

0 commit comments

Comments
 (0)