add created at filter logic to users finder and API

This commit is contained in:
James Lopez 2017-07-07 09:29:00 +02:00
parent b08df253ef
commit 1a7d2aba3b
3 changed files with 22 additions and 1 deletions

View file

@ -29,6 +29,7 @@ class UsersFinder
users = by_active(users)
users = by_external_identity(users)
users = by_external(users)
users = by_created_at(users)
users
end
@ -71,4 +72,16 @@ class UsersFinder
users.external
end
def by_created_at(users)
if params[:created_after].present?
users = users.where(users.klass.arel_table[:created_at].gteq(params[:created_after]))
end
if params[:created_before].present?
users = users.where(users.klass.arel_table[:created_at].lteq(params[:created_before]))
end
users
end
end

View file

@ -48,6 +48,8 @@ module API
optional :active, type: Boolean, default: false, desc: 'Filters only active users'
optional :external, type: Boolean, default: false, desc: 'Filters only external users'
optional :blocked, type: Boolean, default: false, desc: 'Filters only blocked users'
optional :created_after, type: DateTime, desc: 'Return users created after the specified time'
optional :created_before, type: DateTime, desc: 'Return users created before the specified time'
all_or_none_of :extern_uid, :provider
use :pagination
@ -55,6 +57,10 @@ module API
get do
authenticated_as_admin! if params[:external].present? || (params[:extern_uid].present? && params[:provider].present?)
unless current_user.admin?
params.except!(:created_after, :created_before)
end
users = UsersFinder.new(current_user, params).execute
authorized = can?(current_user, :read_users_list)

View file

@ -47,7 +47,9 @@ describe UsersFinder do
end
it 'filters by created_at' do
users = described_class.new(user, created_after: 2.days.ago, created_before: Time.now + 2.days).execute
users = described_class.new(user,
created_after: 2.days.ago,
created_before: Time.now + 2.days).execute
expect(users.count).to eq(4)
end