-
Notifications
You must be signed in to change notification settings - Fork 62
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
[10-10CG] Add paginated facilities endpoint to CG controller #18560
Changes from 8 commits
0314d31
10cfc58
68e2042
eb8f574
c5902e1
5a63341
90d6545
800847c
38854aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ def initialize(body, status) | |
self.body = body | ||
self.status = status | ||
parsed_body = JSON.parse(body) | ||
self.data = parsed_body['data'] | ||
self.data = parsed_body['data'] || [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no facilities are returned, the lighthouse api does not return a data key or object. This handles that scenario by setting it to an empty array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💅 This wasn't an issue when we were pulling Facilities for specific States, but definitely comes in to play now. Nice! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! It's only an issue with the pagination params with a page that is outside of the total results (ie give me page 10 with 10 per page of a total result set of 5). |
||
self.meta = parsed_body['meta'] | ||
self.links = parsed_body['links'] | ||
if meta | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'lighthouse/facilities/v1/response' | ||
|
||
RSpec.describe Lighthouse::Facilities::V1::Response, type: :model do | ||
subject { described_class.new(response_body, response_status) } | ||
|
||
let(:data) do | ||
[ | ||
{ 'id' => 'nca_042', 'attributes' => { 'name' => 'Facility One', 'facilityType' => 'va_health_facility' } }, | ||
{ 'id' => 'nca_043', 'attributes' => { 'name' => 'Facility Two', 'facilityType' => 'va_health_facility' } } | ||
] | ||
end | ||
|
||
let(:meta) do | ||
{ | ||
'pagination' => { | ||
'currentPage' => 1, | ||
'perPage' => 10, | ||
'totalEntries' => 20 | ||
}, | ||
'distances' => [ | ||
{ 'distance' => 5.0 }, | ||
{ 'distance' => 10.0 } | ||
] | ||
} | ||
end | ||
|
||
let(:response_body) do | ||
body = { 'links' => {} } | ||
body['meta'] = meta unless meta.nil? | ||
body['data'] = data unless data.nil? | ||
body.to_json | ||
end | ||
|
||
let(:response_status) { 200 } | ||
let(:response) { described_class.new(response_body, response_status) } | ||
|
||
describe '#initialize' do | ||
it 'parses the response body and sets attributes' do | ||
expect(subject.body).to eq(response_body) | ||
expect(subject.status).to eq(response_status) | ||
expect(subject.data).to be_an(Array) | ||
expect(subject.meta).to be_a(Hash) | ||
expect(subject.current_page).to eq(1) | ||
expect(subject.per_page).to eq(10) | ||
expect(subject.total_entries).to eq(20) | ||
end | ||
|
||
context 'no data attribute' do | ||
let(:data) { nil } | ||
|
||
it 'sets data to empty array' do | ||
expect(subject.body).to eq(response_body) | ||
expect(subject.status).to eq(response_status) | ||
expect(subject.data).to be_an(Array) | ||
expect(subject.meta).to be_a(Hash) | ||
expect(subject.current_page).to eq(1) | ||
expect(subject.per_page).to eq(10) | ||
expect(subject.total_entries).to eq(20) | ||
end | ||
end | ||
end | ||
|
||
describe '#facilities' do | ||
it 'returns a paginated collection of facilities' do | ||
facilities = subject.facilities | ||
expect(facilities).to be_an_instance_of(WillPaginate::Collection) | ||
expect(facilities.size).to eq(2) | ||
expect(facilities.current_page).to eq(1) | ||
expect(facilities.per_page).to eq(10) | ||
expect(facilities.total_entries).to eq(20) | ||
|
||
facility = facilities.first | ||
expect(facility).to be_an_instance_of(Lighthouse::Facilities::Facility) | ||
expect(facility.distance).to eq(5.0) | ||
end | ||
|
||
it 'creates facilities with underscore attributes' do | ||
facility = subject.facilities.first | ||
expect(facility.attributes.keys).to include(:name, :facility_type) | ||
end | ||
|
||
context 'data is nil' do | ||
let(:data) { nil } | ||
|
||
it 'returns response with no facilities' do | ||
facilities = subject.facilities | ||
expect(facilities).to be_an_instance_of(WillPaginate::Collection) | ||
expect(facilities.size).to eq(0) | ||
expect(facilities.current_page).to eq(1) | ||
expect(facilities.per_page).to eq(10) | ||
expect(facilities.total_entries).to eq(20) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'lighthouse/facilities/v1/client' | ||
|
||
RSpec.describe 'V0::CaregiversAssistanceClaims', type: :request do | ||
let(:uri) { 'http://localhost:3000' } | ||
|
@@ -55,4 +56,66 @@ | |
).to eq(false) | ||
end | ||
end | ||
|
||
describe 'GET /v0/caregivers_assistance_claims/facilities' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New request spec |
||
subject do | ||
get('/v0/caregivers_assistance_claims/facilities', params:, headers:) | ||
end | ||
|
||
let(:headers) do | ||
{ | ||
'ACCEPT' => 'application/json', | ||
'CONTENT_TYPE' => 'application/json' | ||
} | ||
end | ||
|
||
let(:params) do | ||
{ | ||
'zip' => '90210', | ||
'state' => 'CA', | ||
'lat' => '34.0522', | ||
'long' => '-118.2437', | ||
'radius' => '50', | ||
'visn' => '1', | ||
'type' => '1', | ||
'mobile' => '1', | ||
'page' => '1', | ||
'per_page' => '10', | ||
'facilityIds' => 'vha_123,vha_456', | ||
'services' => ['1'], | ||
'bbox' => ['2'] | ||
} | ||
end | ||
|
||
let(:mock_facility_response) do | ||
{ | ||
'data' => [ | ||
{ 'id' => 'vha_123', 'attributes' => { 'name' => 'Facility 1' } }, | ||
{ 'id' => 'vha_456', 'attributes' => { 'name' => 'Facility 2' } } | ||
] | ||
} | ||
end | ||
let(:lighthouse_service) { double('Lighthouse::Facilities::V1::Client') } | ||
|
||
before do | ||
allow(Lighthouse::Facilities::V1::Client).to receive(:new).and_return(lighthouse_service) | ||
allow(lighthouse_service).to receive(:get_paginated_facilities).and_return(mock_facility_response) | ||
end | ||
|
||
it 'returns the response as JSON' do | ||
subject | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.body).to eq(mock_facility_response.to_json) | ||
end | ||
|
||
it 'calls the Lighthouse facilities service with the permitted params' do | ||
subject | ||
|
||
expected_params = ActionController::Parameters.new(params).permit! | ||
|
||
expect(lighthouse_service).to have_received(:get_paginated_facilities) | ||
.with(expected_params) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added new
facilities
route, and moved the post declaration here. This is functionally the same as before, just easier to read.