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

[10-10CG] Add paginated facilities endpoint to CG controller #18560

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
31 changes: 29 additions & 2 deletions app/controllers/v0/caregivers_assistance_claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

require 'lighthouse/facilities/v1/client'
module V0
# Application for the Program of Comprehensive Assistance for Family Caregivers (Form 10-10CG)
class CaregiversAssistanceClaimsController < ApplicationController
Expand All @@ -11,7 +11,7 @@ class CaregiversAssistanceClaimsController < ApplicationController
before_action :load_user, only: :create

before_action :record_submission_attempt, only: :create
before_action :initialize_claim
before_action :initialize_claim, only: [:create, :download_pdf]
Copy link
Contributor Author

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 this initialize_claim logic more obvious.


rescue_from ::Form1010cg::Service::InvalidVeteranStatus, with: :backend_service_outage

Expand Down Expand Up @@ -51,8 +51,35 @@ def download_pdf
send_data file_contents, filename: client_file_name, type: 'application/pdf', disposition: 'attachment'
end

def facilities
lighthouse_facilities = lighthouse_facilities_service.get_paginated_facilities(lighthouse_facilities_params)
render(json: lighthouse_facilities)
end

private

def lighthouse_facilities_service
@lighthouse_facilities_service ||= Lighthouse::Facilities::V1::Client.new
end

def lighthouse_facilities_params
params.permit(
:zip,
:state,
:lat,
:long,
:radius,
:visn,
:type,
:mobile,
:page,
:per_page,
services: [],
bbox: [],
facilityIds: []
)
end

def record_submission_attempt
auditor.record(:submission_attempt)
end
Expand Down
10 changes: 7 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

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.

post(:download_pdf)
end
end

namespace :form1010cg do
resources :attachments, only: :create
end
Expand Down
5 changes: 5 additions & 0 deletions lib/lighthouse/facilities/v1/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def get_facilities(params)
facilities.reject!(&:mobile?) if params['exclude_mobile']
facilities
end

def get_paginated_facilities(params)
response = perform(:get, '/services/va_facilities/v1/facilities', params)
Lighthouse::Facilities::V1::Response.new(response.body, response.status)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lighthouse/facilities/v1/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || []
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -244,4 +245,55 @@
).to eq(false)
end
end

describe '#facilities' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
96 changes: 96 additions & 0 deletions spec/lib/lighthouse/facilities/v1/response_spec.rb
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
Loading