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

Merged
merged 9 commits into from
Sep 25, 2024
30 changes: 29 additions & 1 deletion app/controllers/v0/caregivers_assistance_claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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 +12,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: %i[create download_pdf]

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

Expand Down Expand Up @@ -51,8 +52,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,
:facilityIds,
services: [],
bbox: []
)
end

def record_submission_attempt
auditor.record(:submission_attempt)
end
Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@
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
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
98 changes: 98 additions & 0 deletions spec/lib/lighthouse/facilities/v1/response_spec.rb
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
63 changes: 63 additions & 0 deletions spec/requests/v0/caregivers_assistance_claims_spec.rb
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' }
Expand Down Expand Up @@ -55,4 +56,66 @@
).to eq(false)
end
end

describe 'GET /v0/caregivers_assistance_claims/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.

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
Loading