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

Filter by office code #339

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/api/datastore/v1/searching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module V1
class Searching < Base
version 'v1', using: :path

route_setting :authorised_consumers, %w[crime-review]
route_setting :authorised_consumers, %w[crime-review crime-apply]

resource :searches do
desc 'Search the Datastore.'
Expand All @@ -25,6 +25,9 @@ class Searching < Base

optional :reviewed_after, type: DateTime
optional :reviewed_before, type: DateTime

# Apply search spike
optional :office_code, type: String
end

optional :sorting, type: JSON, desc: 'Sorting JSON.', default: Sorting.new.attributes do
Expand Down
5 changes: 5 additions & 0 deletions app/models/search_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SearchFilter
attribute :work_stream, array: true, default: -> { [] }
attribute :case_type, array: true, default: -> { [] }
attribute :application_type, array: true, default: -> { [] }
attribute :office_code, :string

def active_filters
attributes.compact_blank.keys
Expand Down Expand Up @@ -84,4 +85,8 @@ def filter_case_type(scope)
def filter_application_type(scope)
scope.where(application_type:)
end

def filter_office_code(scope)
scope.where(office_code:)
end
end
35 changes: 35 additions & 0 deletions spec/api/datastore/v1/searching/filter_by_office_code_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rails_helper'

RSpec.describe 'searches filter by office code' do
subject(:api_request) do
post '/api/v1/searches', params: { search: search, pagination: {} }
end

let(:records) { JSON.parse(response.body).fetch('records') }
let(:search) { {} }

before do
CrimeApplication.insert_all(
[
{ submitted_application: { provider_details: { office_code: 'AB12C' } } },
{ submitted_application: { provider_details: { office_code: 'ZX45Y' } } },
]
)

api_request
end

it 'defaults to returning all office codes' do
expect(records.count).to be 2
expect(records.pluck('office_code').uniq).to match_array(%w[AB12C ZX45Y])
end

describe 'filtering by `AB12C` office code' do
let(:search) { { office_code: 'AB12C' } }

it 'returns only applications with office code `AB12C`' do
expect(records.count).to be 1
expect(records.pluck('office_code').uniq).to eq(['AB12C'])
end
end
end
Loading