Skip to content

Commit

Permalink
Make user_submissions index endpoint more useful and add region_id pa…
Browse files Browse the repository at this point in the history
…ram to list_within_range
  • Loading branch information
RyanTG committed Jan 10, 2025
1 parent 2c23eb0 commit 3f7f289
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 27 deletions.
27 changes: 16 additions & 11 deletions app/controllers/api/v1/user_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class UserSubmissionsController < InheritedResources::Base

api :GET, '/api/v1/region/:region/user_submissions.json', 'Fetch user submissions for a single region'
param :region, String, desc: 'Name of the Region you want to see user submissions for', required: true
param :submission_type, String, desc: 'Type of submission to filter to. Multiple filters can be formatted as `;submission_type[]=remove_machine;submission_type[]=new_lmx` etc.', required: false
def index
user_submissions = apply_scopes(UserSubmission)
user_submissions = user_submissions.select { |s| s.submission_type == UserSubmission::REMOVE_MACHINE_TYPE }
submission_type = params[:submission_type].blank? ? %w[new_lmx remove_machine new_condition new_msx confirm_location] : params[:submission_type]

region_id = Region.where(name: params[:region]).pluck(:id).first

user_submissions = UserSubmission.where( submission_type: submission_type, region_id: region_id).limit(300).order('created_at DESC')

return_response(user_submissions, 'user_submissions')
end
Expand Down Expand Up @@ -40,7 +44,6 @@ def location
formats ['json']
def delete_location
except = %i[user_id machine_id comment user_name location_name machine_name high_score city_name lat lon]
user_submissions = apply_scopes(UserSubmission)
user_submissions = UserSubmission.where(created_at: (1.year.ago)..(Date.today.end_of_day), submission_type: UserSubmission::DELETE_LOCATION_TYPE)
sorted_submissions = user_submissions.order('created_at DESC')

Expand Down Expand Up @@ -80,22 +83,24 @@ def top_users
param :lon, String, desc: 'Longitude', required: true
param :max_distance, String, desc: 'Closest location within "max_distance" miles, max of 250', required: false
param :min_date_of_submission, String, desc: 'Earliest date to consider updates from, format YYYY-MM-DD', required: false
param :submission_type, String, desc: 'Type of submission to filter to', required: false
param :submission_type, String, desc: 'Type of submission to filter to. Multiple filters can be formatted as `;submission_type[]=remove_machine;submission_type[]=new_lmx` etc.', required: false
param :region_id, String, desc: 'Limit results to a region', required: false
def list_within_range
if params[:max_distance].blank?
max_distance = MAX_MILES_TO_SEARCH_FOR_USER_SUBMISSIONS
elsif params[:max_distance].to_i > 250
max_distance = 250
else
max_distance = params[:max_distance].to_i
else
max_distance = [250, params[:max_distance].to_i].min
end
min_date_of_submission = params[:min_date_of_submission] ? params[:min_date_of_submission].to_date.beginning_of_day : 1.month.ago.beginning_of_day

user_submissions = nil
if params[:submission_type]
user_submissions = UserSubmission.where.not(lat: nil).where(created_at: min_date_of_submission..Date.today.end_of_day, submission_type: params[:submission_type]).near([params[:lat], params[:lon]], max_distance, order: false)

submission_type = params[:submission_type].blank? ? %w[new_lmx remove_machine new_condition new_msx confirm_location] : params[:submission_type]

if params[:region_id].blank?
user_submissions = UserSubmission.where.not(lat: nil).where(created_at: min_date_of_submission..Date.today.end_of_day, submission_type: submission_type).near([params[:lat], params[:lon]], max_distance, order: false)
else
user_submissions = UserSubmission.where.not(lat: nil).where(created_at: min_date_of_submission..Date.today.end_of_day).near([params[:lat], params[:lon]], max_distance, order: false)
user_submissions = UserSubmission.where.not(lat: nil).where(created_at: min_date_of_submission..Date.today.end_of_day, submission_type: submission_type, region_id: params[:region_id]).near([params[:lat], params[:lon]], max_distance, order: false)
end

sorted_submissions = user_submissions.order('created_at DESC')
Expand Down
58 changes: 42 additions & 16 deletions spec/requests/api/v1/user_submissions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

describe Api::V1::UserSubmissionsController, type: :request do
before(:each) do
@region = FactoryBot.create(:region, name: 'portland')
@region = FactoryBot.create(:region, name: 'portland', id: 41)
@other_region = FactoryBot.create(:region, name: 'clackamas', id: 42)
end

describe '#list_within_range' do
Expand All @@ -29,7 +30,7 @@
expect(response).to be_successful
json = JSON.parse(response.body)['user_submissions']

expect(json.count).to eq(7)
expect(json.count).to eq(6)
end

it 'sets a max_distance limit of 250 miles' do
Expand Down Expand Up @@ -75,30 +76,55 @@

expect(json.count).to eq(1)
end

it 'respects region filter' do
location = FactoryBot.create(:location, lat: '45.6008356', lon: '-122.760606', region_id: @region.id)
other_location = FactoryBot.create(:location, lat: '45.6008356', lon: '-122.760606', region_id: @other_region.id)

FactoryBot.create(:user_submission, user: @user, location: location, lat: location.lat, lon: location.lon, submission_type: 'new_lmx', created_at: Time.now.strftime('%Y-%m-%d'), region_id: @region.id)
FactoryBot.create(:user_submission, user: @user, location: location, lat: location.lat, lon: location.lon, submission_type: 'remove_machine', created_at: Time.now.strftime('%Y-%m-%d'), region_id: @region.id)
FactoryBot.create(:user_submission, user: @user, location: other_location, lat: other_location.lat, lon: other_location.lon, submission_type: 'new_lmx', created_at: Time.now.strftime('%Y-%m-%d'), region_id: @other_region.id)
FactoryBot.create(:user_submission, user: @user, location: other_location, lat: other_location.lat, lon: other_location.lon, submission_type: 'remove_machine', created_at: Time.now.strftime('%Y-%m-%d'), region_id: @other_region.id)

get '/api/v1/user_submissions/list_within_range.json', params: { lat: '45.6008356', lon: '-122.760606', region_id: @region.id }

expect(response).to be_successful
json = JSON.parse(response.body)['user_submissions']

expect(json.count).to eq(2)
end
end

describe '#index' do
it 'returns all submissions within scope' do
FactoryBot.create(:user_submission, region: @region, submission_type: 'remove_machine', submission: 'foo')
FactoryBot.create(:user_submission, region: FactoryBot.create(:region, name: 'chicago'), submission_type: 'remove_machine', submission: 'foo')
FactoryBot.create(:user_submission, region: @region, submission_type: 'new_lmx', submission: 'added in region')
FactoryBot.create(:user_submission, region: @region, submission_type: 'remove_machine', submission: 'removed in region')
FactoryBot.create(:user_submission, region: @other_region, submission_type: 'remove_machine', submission: 'removed elsewhere')
FactoryBot.create(:user_submission, region: @other_region, submission_type: 'add_lmx', submission: 'added elsewhere')

get "/api/v1/region/#{@region.name}/user_submissions.json"

expect(response.body).to include('remove_machine')
expect(response.body).to include('foo')
expect(response).to be_successful
json = JSON.parse(response.body)['user_submissions']

expect(response.body).to_not include('bar')
end
expect(json.count).to eq(2)
expect(response.body).to include('added in region')
expect(response.body).to include('removed in region')

it 'only shows remove_machine submissions' do
FactoryBot.create(:user_submission, region: @region, submission_type: 'remove_machine', submission: 'removed foo from bar')
FactoryBot.create(:user_submission, region: @region, submission_type: 'DO_NOT_SHOW', submission: 'hope this does not show')
get "/api/v1/region/#{@region.name}/user_submissions.json"
expect(response.body).to_not include('added elsewhere')
expect(response.body).to_not include('removed elsewhere')

get "/api/v1/region/#{@region.name}/user_submissions.json?submission_type=remove_machine"

expect(response.body).to include('remove_machine')
expect(response.body).to include('removed foo from bar')
expect(response).to be_successful
json = JSON.parse(response.body)['user_submissions']

expect(json.count).to eq(1)
expect(response.body).to_not include('added in region')
expect(response.body).to include('removed in region')

expect(response.body).to_not include('DO_NOT_SHOW')
expect(response.body).to_not include('hope this does not show')
expect(response.body).to_not include('added elsewhere')
expect(response.body).to_not include('removed elsewhere')
end
end

Expand Down

0 comments on commit 3f7f289

Please sign in to comment.