-
Notifications
You must be signed in to change notification settings - Fork 61
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
base: master
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
|
@@ -121,9 +121,13 @@ | |
resource :hca_attachments, only: :create | ||
resource :form1010_ezr_attachments, only: :create | ||
|
||
resources :caregivers_assistance_claims, only: :create | ||
post 'caregivers_assistance_claims/download_pdf', to: 'caregivers_assistance_claims#download_pdf' | ||
|
||
resources :caregivers_assistance_claims, only: :create do | ||
collection do | ||
get(:facilities) | ||
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. Added new |
||
post(:download_pdf) | ||
end | ||
end | ||
|
||
namespace :form1010cg do | ||
resources :attachments, only: :create | ||
end | ||
|
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
require 'rails_helper' | ||
require 'saved_claim/caregivers_assistance_claim' | ||
require 'lighthouse/facilities/v1/client' | ||
|
||
RSpec.describe V0::CaregiversAssistanceClaimsController, type: :controller do | ||
describe '::auditor' do | ||
|
@@ -244,4 +245,55 @@ | |
).to eq(false) | ||
end | ||
end | ||
|
||
describe '#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. As mentioned above, I'd like to eventually move these to request specs. 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. I deleted these and moved them to the request spec. |
||
let(:mock_facility_response) do | ||
{ | ||
'fake_response' => [ | ||
{ '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 | ||
|
||
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', | ||
services: ['1'], | ||
bbox: ['2'], | ||
facilityIds: ['vha_123', 'vha_456'] | ||
} | ||
end | ||
|
||
subject { get(:facilities, params:) } | ||
|
||
it 'calls the Lighthouse facilities service with the permitted params' do | ||
subject | ||
|
||
expect(lighthouse_service).to have_received(:get_paginated_facilities).with(ActionController::Parameters.new(params).permit!) | ||
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 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'rails_helper' | ||
require 'lighthouse/facilities/v1/response' | ||
|
||
RSpec.describe Lighthouse::Facilities::V1::Response, type: :model do | ||
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) } | ||
|
||
subject { 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 |
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.
The new
facilities
method does not require initializing a claim. These are the only other two exposed routes in this controller. I think this also makes thisinitialize_claim
logic more obvious.