Skip to content

Commit

Permalink
EDM-322/filter institution programs by facility code (#1244)
Browse files Browse the repository at this point in the history
* Dockerfile updates for local devel

* Filter by facility code and allow disable pagination

* Update spec

* Revert docker

* Fix ternary

* Update spec

* Fix linting

* Fix typo

---------
  • Loading branch information
jefftmarks authored Nov 6, 2024
1 parent 3310ea0 commit 2f29e92
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/controllers/v0/institution_programs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index

render json: search_results
.search_order(@query)
.page(page), meta: @meta
.then { |results| pagination_for(results) }, meta: @meta
end

private
Expand All @@ -40,7 +40,9 @@ def normalized_query_params
query[:name].try(:strip!)
query[:name].try(:downcase!)
query[:preferred_provider].try(:downcase!)
query[:disable_pagination].try(:downcase!)
query[:provider].try(:upcase!)
query[:facility_code].try(:upcase!)
%i[state country type].each do |k|
query[k].try(:upcase!)
end
Expand Down Expand Up @@ -85,6 +87,7 @@ def filter_results(relation)
[
%i[program_type type],
%i[institutions.institution provider],
%i[institutions.facility_code facility_code],
%w[institutions.physical_country country],
%w[institutions.physical_state state],
%w[institutions.preferred_provider preferred_provider]
Expand Down Expand Up @@ -116,5 +119,9 @@ def add_active_search_facets(raw_facets)
add_country_search_facet(raw_facets)
raw_facets
end

def pagination_for(results)
@query[:disable_pagination] == 'true' ? results : results.page(page)
end
end
end
54 changes: 54 additions & 0 deletions spec/controllers/v0/institution_programs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@
expect(response).to match_response_schema('institution_programs')
end

it 'filters by uppercase facility code' do
create(:institution, version_id: Version.last.id, facility_code: 'ABCD1234')
create(:institution_program, institution_id: Institution.last.id)
get(:index, params: { facility_code: Institution.last.facility_code })
expect(JSON.parse(response.body)['data'].count).to eq(1)
expect(response.media_type).to eq('application/json')
expect(response).to match_response_schema('institution_programs')
end

it 'filters by lowercase facility code' do
create(:institution, version_id: Version.last.id, facility_code: 'ABCD1234')
create(:institution_program, institution_id: Institution.last.id)
get(:index, params: { facility_code: Institution.last.facility_code.downcase })
expect(JSON.parse(response.body)['data'].count).to eq(1)
expect(response.media_type).to eq('application/json')
expect(response).to match_response_schema('institution_programs')
end

it 'filter by lowercase state returns results' do
get(:index, params: { state: 'ny' })
expect(JSON.parse(response.body)['data'].count).to eq(3)
Expand Down Expand Up @@ -283,5 +301,41 @@
get(:index, params: { name: 'testville, ak' })
expect(JSON.parse(response.body)['data'].count).to eq(1)
end

context 'when configuring pagination' do
context 'when enabled' do
it 'enables pagination by default' do
get(:index)
expect(JSON.parse(response.body)['links']).not_to be_nil
end

it 'enables pagination with invalid query parameter' do
get(:index, params: { disable_pagination: 'invalid' })
expect(JSON.parse(response.body)['links']).not_to be_nil
end

it 'enables pagination with a lowercase query parameter' do
get(:index, params: { disable_pagination: 'false' })
expect(JSON.parse(response.body)['links']).not_to be_nil
end

it 'enables pagination with an uppercase query parameter' do
get(:index, params: { disable_pagination: 'False' })
expect(JSON.parse(response.body)['links']).not_to be_nil
end
end

context 'when disabled' do
it 'disables pagination with a lowercase query parameter' do
get(:index, params: { disable_pagination: 'true' })
expect(JSON.parse(response.body)['links']).to be_nil
end

it 'disabled pagination with an uppercase query parameter' do
get(:index, params: { disable_pagination: 'True' })
expect(JSON.parse(response.body)['links']).to be_nil
end
end
end
end
end

0 comments on commit 2f29e92

Please sign in to comment.