Skip to content

Commit

Permalink
Update Inquiries::Retriever and Correspondences::Retriever (#16512)
Browse files Browse the repository at this point in the history
Co-authored-by: khoa-v-nguyen <khoa.nguyen@oddball.io>
  • Loading branch information
Khoa-V-Nguyen and khoa-v-nguyen committed Apr 26, 2024
1 parent 55a6fbb commit e76f788
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 48 deletions.
6 changes: 1 addition & 5 deletions modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb
Expand Up @@ -10,11 +10,7 @@ def initialize(user_mock_data:, entity_class:)
end

def call
if fetch_data.is_a?(Array)
fetch_data.map { |item| entity_class.new(item) }
else
entity_class.new(fetch_data)
end
fetch_data.map { |item| entity_class.new(item) }
rescue => e
::ErrorHandler.handle_service_error(e)
end
Expand Down
Expand Up @@ -43,7 +43,13 @@ def filter_data(data)
end

def handle_response_data(response)
response[:Data].presence || response
case response
when Hash
response[:Data]
else
error = JSON.parse(response.body, symbolize_names: true)
error[:Message]
end
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb
Expand Up @@ -45,7 +45,7 @@ def fetch_correspondences(inquiry_id:)
).call

case correspondences
when Hash
when String
[]
else
correspondences
Expand All @@ -64,7 +64,13 @@ def filter_data(data, id = nil)
end

def handle_response_data(response)
response[:Data].presence || raise(InquiriesRetrieverError, response)
case response
when Hash
response[:Data]
else
error = JSON.parse(response.body, symbolize_names: true)
raise(InquiriesRetrieverError, error[:Message])
end
end
end
end
Expand Down
Expand Up @@ -20,25 +20,20 @@
describe '#call' do
context 'when Crm raise an error' do
let(:endpoint) { 'inquiries/1/replies' }
let(:response) do
{ Data: [],
Message: 'Data Validation: No Inquiry Found',
ExceptionOccurred: true,
ExceptionMessage: 'Data Validation: No Inquiry Found',
MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' }
let(:body) do
'{"Data":[],"Message":"Data Validation: No Inquiry Found",' \
'"ExceptionOccurred":true,"ExceptionMessage":"Data Validation:' \
' No Inquiry Found","MessageId":"95f9d1e7-d532-41d7-b43f-78ae9a3e778d"}'
end
let(:failure) { Faraday::Response.new(response_body: body, status: 400) }

before do
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token')
allow(service).to receive(:call).and_return(response)
allow(service).to receive(:call).and_return(failure)
end

it 'returns the error' do
expect(retriever.call).to eq({ Data: [],
Message: 'Data Validation: No Inquiry Found',
ExceptionOccurred: true,
ExceptionMessage: 'Data Validation: No Inquiry Found',
MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' })
expect(retriever.call).to eq('Data Validation: No Inquiry Found')
end
end

Expand Down
Expand Up @@ -7,6 +7,10 @@
described_class.new(user_mock_data:, entity_class: AskVAApi::Inquiries::Entity, icn:)
end

def mock_response(status:, body:)
instance_double(Faraday::Response, status:, body: body.to_json)
end

let(:service) { instance_double(Crm::Service) }
let(:icn) { nil }
let(:error_message) { 'Some error occurred' }
Expand All @@ -25,21 +29,14 @@
',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: No Inquiries found by ' \
'ID A-20240423-30709","MessageId":"ca5b990a-63fe-407d-a364-46caffce12c1"}'
end
let(:failure) do
{
status: 400,
body:,
response_headers: nil,
url: nil
}
end
let(:failure) { Faraday::Response.new(response_body: body, status: 400) }

before do
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token')
allow(service).to receive(:call).and_return(failure)
end

it 'raise CorrespondenceRetrieverrError' do
it 'raise InquiriesRetrieverrError' do
expect { retriever.call }.to raise_error(ErrorHandler::ServiceError)
end
end
Expand Down Expand Up @@ -188,11 +185,7 @@
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token')
allow(service).to receive(:call).and_return(response)
allow_any_instance_of(AskVAApi::Correspondences::Retriever).to receive(:call)
.and_return({ Data: [],
Message: 'Data Validation: No Inquiry Found',
ExceptionOccurred: true,
ExceptionMessage: 'Data Validation: No Inquiry Found',
MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' })
.and_return('Data Validation: No Inquiry Found')
end

it 'returns correspondences as an empty array' do
Expand Down
17 changes: 3 additions & 14 deletions modules/ask_va_api/spec/requests/v0/inquiries_spec.rb
Expand Up @@ -12,7 +12,7 @@
JSON.parse(File.read('modules/ask_va_api/config/locales/get_inquiries_mock_data.json'))['Data']
end
let(:valid_id) { mock_inquiries.first['InquiryNumber'] }
let(:invalid_id) { 'invalid-id' }
let(:invalid_id) { 'A-20240423-30709' }

before do
allow(LogService).to receive(:new).and_return(logger)
Expand Down Expand Up @@ -212,14 +212,7 @@
',"ExceptionOccurred":true,"ExceptionMessage":"Data Validation: No Inquiries found by ' \
'ID A-20240423-30709","MessageId":"ca5b990a-63fe-407d-a364-46caffce12c1"}'
end
let(:failure) do
{
status: 400,
body:,
response_headers: nil,
url: nil
}
end
let(:failure) { Faraday::Response.new(response_body: body, status: 400) }
let(:service) { instance_double(Crm::Service) }

before do
Expand All @@ -234,11 +227,7 @@

it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'AskVAApi::Inquiries::InquiriesRetrieverError: ' \
'{:status=>400, :body=>"{\"Data\":null,\"Message\":\"Data Validation: ' \
'No Inquiries found by ID A-20240423-30709\",\"ExceptionOccurred\":' \
'true,\"ExceptionMessage\":\"Data Validation: No Inquiries found by ID ' \
'A-20240423-30709\",\"MessageId\":\"ca5b990a-63fe-407d-a364-46caffce12c1\"}",' \
' :response_headers=>nil, :url=>nil}'
'Data Validation: No Inquiries found by ID A-20240423-30709'
end
end
end
Expand Down

0 comments on commit e76f788

Please sign in to comment.