Skip to content

Commit

Permalink
MHV-63085: Added api endpoint for getting a user's treatment faciliti…
Browse files Browse the repository at this point in the history
…es (#19484)

* MHV-63085: Added api endpoint for getting a user's treatment facilities

* MHV-63085: Changed the api endpoint to get patient

* MHV-63085: Unit test fix

---------

Co-authored-by: Matthew Wright <matwright2010@gmail.com>
  • Loading branch information
mattwrightva and wright0776 authored Nov 22, 2024
1 parent da46155 commit 9ff4ff7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 92 deletions.
25 changes: 14 additions & 11 deletions lib/medical_records/bb_internal/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ def get_bbmi_notification_setting
response.body
end

# Retrieves the patient information by user ID.
# @return [Hash] A hash containing the patient's details
#
def get_patient
response = perform(:get, "usermgmt/patient/uid/#{@session.user_id}", nil, token_headers)
patient = response.body

raise Common::Exceptions::ServiceError.new(detail: 'Patient not found') if patient.blank?

patient
end

private

##
Expand All @@ -156,24 +168,15 @@ def get_session
@session = super

# Supplement session with patientId
session.patient_id = get_patient_id
patient = get_patient
session.patient_id = patient['ipas']&.first&.dig('patientId')
# Put ICN back into the session
session.icn = icn

session.save
session
end

def get_patient_id
response = perform(:get, "usermgmt/patient/uid/#{@session.user_id}", nil, token_headers)

patient_id = response.body['ipas']&.first&.dig('patientId')

raise Common::Exceptions::ServiceError.new(detail: 'Patient ID not found for user') if patient_id.blank?

patient_id
end

##
# Overriding MHVSessionBasedClient's method, because we need more control over the path.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module MyHealth
module V1
module MedicalRecords
class PatientController < MrController
# Gets a user's treatment facilities
# @return [Array] of treatment facilities and related user info
def index
resource = bb_client.get_patient
render json: resource.to_json
end
end
end
end
end
1 change: 1 addition & 0 deletions modules/my_health/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get :status, on: :collection
end
resources :military_service, only: %i[index]
resources :patient, only: %i[index]
resources :imaging, only: %i[index], defaults: { format: :json } do
get 'request', on: :member, action: :request_download
get :status, on: :collection, action: :request_status
Expand Down
34 changes: 32 additions & 2 deletions spec/lib/medical_records/bb_internal/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

describe BBInternal::Client do
before(:all) do
# The "get_patient" cassette also contains the session auth call.
VCR.use_cassette 'mr_client/bb_internal/get_patient' do
VCR.use_cassette 'mr_client/bb_internal/session_auth' do
@client ||= begin
client = BBInternal::Client.new(session: { user_id: '11375034', icn: '1012740022V620959' })
client.authenticate
Expand Down Expand Up @@ -169,4 +168,35 @@
end
end
end

describe '#get_patient' do
it 'retrieves the patient information by user ID' do
VCR.use_cassette 'mr_client/bb_internal/get_patient' do
patient = client.get_patient

expect(patient).to be_a(Hash)

expect(patient).to have_key('ipas')
expect(patient['ipas']).to be_an(Array)
expect(patient['ipas']).not_to be_empty

expect(patient).to have_key('facilities')
expect(patient['facilities']).to be_an(Array)
expect(patient['facilities']).not_to be_empty

first_facility = patient['facilities'].first
expect(first_facility).to be_a(Hash)
expect(first_facility['facilityInfo']).to have_key('name')
end
end

it 'raises a ServiceError when the patient is not found' do
empty_response = double('Response', body: nil)
allow(client).to receive(:perform).and_return(empty_response)

expect { client.get_patient }.to raise_error(Common::Exceptions::ServiceError) do |error|
expect(error.errors.first[:detail]).to eq('Patient not found')
end
end
end
end
129 changes: 50 additions & 79 deletions spec/support/vcr_cassettes/mr_client/bb_internal/get_patient.yml

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions spec/support/vcr_cassettes/mr_client/bb_internal/session_auth.yml

Large diffs are not rendered by default.

0 comments on commit 9ff4ff7

Please sign in to comment.