Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Personal health care contacts: Selects only valid contact types #16062

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions lib/va_profile/profile/v3/health_benefit_bio_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@
require 'va_profile/models/associated_person'
require 'va_profile/models/message'

module VAProfile::Profile::V3
class HealthBenefitBioResponse < VAProfile::Response
attr_reader :body
module VAProfile
module Profile
module V3
class HealthBenefitBioResponse < VAProfile::Response
attr_reader :body

attribute :contacts, Array[VAProfile::Models::AssociatedPerson]
attribute :messages, Array[VAProfile::Models::Message]
attribute :contacts, Array[VAProfile::Models::AssociatedPerson]
attribute :messages, Array[VAProfile::Models::Message]

def initialize(response)
@body = response.body
contacts = body.dig('profile', 'health_benefit', 'associated_persons')
&.sort_by { |p| VAProfile::Models::AssociatedPerson::CONTACT_TYPES.index(p['contact_type']) }
messages = body['messages']
super(response.status, { contacts:, messages: })
end
def initialize(response)
@body = response.body
contacts = body.dig('profile', 'health_benefit', 'associated_persons')
&.select { |p| valid_contact_types.include?(p['contact_type']) }
&.sort_by { |p| valid_contact_types.index(p['contact_type']) }
messages = body['messages']
super(response.status, { contacts:, messages: })
end

def metadata
{ status:, messages: }
end

private

def metadata
{ status:, messages: }
def valid_contact_types
[
VAProfile::Models::AssociatedPerson::EMERGENCY_CONTACT,
VAProfile::Models::AssociatedPerson::OTHER_EMERGENCY_CONTACT,
VAProfile::Models::AssociatedPerson::PRIMARY_NEXT_OF_KIN,
VAProfile::Models::AssociatedPerson::OTHER_NEXT_OF_KIN
]
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require 'rails_helper'
require 'va_profile/profile/v3/health_benefit_bio_response'

describe VAProfile::Profile::V3::HealthBenefitBioResponse do
subject { described_class.new(response) }

let(:response) do
double('Faraday::Response',
status: 200,
body: {
'profile' => {
'health_benefit' => {
'associated_persons' => [{
'contact_type' => contact_type
}]
}
}
})
end

describe 'Emergency contact' do
let(:contact_type) { 'Emergency Contact' }
radavis marked this conversation as resolved.
Show resolved Hide resolved

it 'includes contact' do
expect(subject.contacts).not_to be_empty
end
end

describe 'Other emergency contact' do
let(:contact_type) { 'Other emergency contact' }

it 'includes contact' do
expect(subject.contacts).not_to be_empty
end
end

describe 'Primary Next of Kin' do
let(:contact_type) { 'Primary Next of Kin' }

it 'includes contact' do
expect(subject.contacts).not_to be_empty
end
end

describe 'Other Next of Kin' do
let(:contact_type) { 'Other Next of Kin' }

it 'includes contact' do
expect(subject.contacts).not_to be_empty
end
end

describe 'Invalid contact type' do
let(:contact_type) { 'Invalid type' }

it 'includes contact' do
radavis marked this conversation as resolved.
Show resolved Hide resolved
expect(subject.contacts).to be_empty
end
end
end
56 changes: 43 additions & 13 deletions spec/lib/va_profile/profile/v3/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,60 @@
describe '#get_health_benefit_bio' do
let(:user) { build(:user, :loa3, idme_uuid:) }

around do |ex|
VCR.use_cassette(cassette) { ex.run }
end

context '200 response' do
let(:idme_uuid) { 'dd681e7d6dea41ad8b80f8d39284ef29' }
let(:cassette) { 'va_profile/profile/v3/health_benefit_bio_200' }

it 'returns the contacts (aka associated_persons) for a user, sorted' do
VCR.use_cassette('va_profile/profile/v3/health_benefit_bio_200') do
response = subject.get_health_benefit_bio
expect(response.status).to eq(200)
expect(response.contacts.size).to eq(4)
types = response.contacts.map(&:contact_type)
expect(types).to match_array(VAProfile::Models::AssociatedPerson::CONTACT_TYPES)
end
response = subject.get_health_benefit_bio
expect(response.status).to eq(200)
expect(response.contacts.size).to eq(4)
types = response.contacts.map(&:contact_type)
valid_contact_types = [
VAProfile::Models::AssociatedPerson::EMERGENCY_CONTACT,
VAProfile::Models::AssociatedPerson::OTHER_EMERGENCY_CONTACT,
VAProfile::Models::AssociatedPerson::PRIMARY_NEXT_OF_KIN,
VAProfile::Models::AssociatedPerson::OTHER_NEXT_OF_KIN
]
expect(types).to match_array(valid_contact_types)
end
end

context '404 response' do
let(:idme_uuid) { '88f572d4-91af-46ef-a393-cba6c351e252' }
let(:cassette) { 'va_profile/profile/v3/health_benefit_bio_404' }

it 'includes messages recieved from the api' do
radavis marked this conversation as resolved.
Show resolved Hide resolved
VCR.use_cassette('va_profile/profile/v3/health_benefit_bio_404') do
response = subject.get_health_benefit_bio
expect(response.status).to eq(404)
expect(response.contacts.size).to eq(0)
expect(response.messages.size).to eq(1)
end
response = subject.get_health_benefit_bio
expect(response.status).to eq(404)
expect(response.contacts.size).to eq(0)
expect(response.messages.size).to eq(1)
end
end

context '500 response' do
let(:idme_uuid) { '88f572d4-91af-46ef-a393-cba6c351e252' }
let(:cassette) { 'va_profile/profile/v3/health_benefit_bio_500' }

it 'includes messages recieved from the api' do
response = subject.get_health_benefit_bio
expect(response.status).to eq(500)
expect(response.contacts.size).to eq(0)
expect(response.messages.size).to eq(1)
end
end

context 'api timeout' do
let(:idme_uuid) { '88f572d4-91af-46ef-a393-cba6c351e252' }
let(:cassette) { 'va_profile/profile/v3/health_benefit_bio_500' }

it 'raises an error' do
allow_any_instance_of(Faraday::Connection).to receive(:post).and_raise(Faraday::TimeoutError)
expect { subject.get_health_benefit_bio }.to raise_error(Common::Exceptions::GatewayTimeout)
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading