From da591cc04203ce0011b83f38d469709c6039245f Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 18 Jul 2016 10:29:09 -0300 Subject: [PATCH] api: list all pending tasks for the current person --- app/api/entities.rb | 7 +++++++ app/api/v1/tasks.rb | 11 ++++++++++- app/models/person.rb | 6 ++++++ test/api/task_test.rb | 11 +++++++++++ test/unit/person_test.rb | 18 ++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/api/entities.rb b/app/api/entities.rb index c868246..dec30e2 100644 --- a/app/api/entities.rb +++ b/app/api/entities.rb @@ -263,6 +263,13 @@ module Api root 'tasks', 'task' expose :id expose :type + expose :requestor, using: Profile + expose :status + expose :created_at + expose :target do |task, options| + type_map = {Profile => ::Profile, Environment => ::Environment}.find {|h| task.target.kind_of?(h.last)} + type_map.first.represent(task.target) unless type_map.nil? + end end class Environment < Entity diff --git a/app/api/v1/tasks.rb b/app/api/v1/tasks.rb index c099493..8654335 100644 --- a/app/api/v1/tasks.rb +++ b/app/api/v1/tasks.rb @@ -3,8 +3,11 @@ module Api class Tasks < Grape::API before { authenticate! } + MAX_PER_PAGE = 50 + resource :tasks do + paginate max_per_page: MAX_PER_PAGE # Collect tasks # # Parameters: @@ -15,7 +18,13 @@ module Api # Example Request: # GET host/api/v1/tasks?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317 get do - present_tasks(environment) + if params[:all_pending].present? + tasks = current_person.all_pending_tasks + else + tasks = select_filtered_collection_of(environment, 'tasks', params) + tasks = tasks.select {|t| current_person.has_permission?(t.permission, environment)} + end + present_partial paginate(tasks), :with => Entities::Task end desc "Return the task id" diff --git a/app/models/person.rb b/app/models/person.rb index 828b536..b264c30 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -440,6 +440,12 @@ class Person < Profile organization.tasks.pending.select{|task| self.has_permission?(task.permission, organization)} end + def all_pending_tasks + [self.memberships, environment].flatten.map do |target| + target.tasks.pending.select{|task| self.has_permission?(task.permission, target)} + end.flatten + tasks.pending + end + def build_contact(profile, params = {}) Contact.new(params.merge(:name => name, :email => email, :sender => self, :dest => profile)) end diff --git a/test/api/task_test.rb b/test/api/task_test.rb index 3b0c11b..15f8581 100644 --- a/test/api/task_test.rb +++ b/test/api/task_test.rb @@ -271,4 +271,15 @@ class TasksTest < ActiveSupport::TestCase assert_equal enterprise, Task.last.target end + should 'list all pending tasks for the current person' do + task1 = create(Task, :requestor => person, :target => person) + task2 = create(Task, :requestor => person, :target => person) + task3 = create(Task, :requestor => person, :target => person) + params[:per_page] = 2 + params[:all_pending] = true + get "/api/v1/tasks?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal [task3.id, task2.id], json["tasks"].map {|t| t["id"]} + end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 6fbf2a1..931b6e7 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1958,6 +1958,7 @@ class PersonTest < ActiveSupport::TestCase person.save! end +<<<<<<< HEAD should 'update profile circles for a person' do person = create_user('testuser').person community = fast_create(Community) @@ -2005,4 +2006,21 @@ class PersonTest < ActiveSupport::TestCase person.remove_profile_from_circle(community, circle) assert_equivalent [circle2], ProfileFollower.with_profile(community).with_follower(person).map(&:circle) end + + should 'return all pending task for a person' do + person = create_user('testuser').person + community1 = fast_create(Community) + community1.add_admin(person) + community2 = fast_create(Community) + task1 = Task.new + task2 = Task.new + task3 = Task.new + task4 = Task.new + person.tasks << task1 + community1.tasks << task2 + community2.tasks << task3 + person.environment.tasks << task4 + person.environment.add_admin(person) + assert_equivalent [task4, task2, task1], person.all_pending_tasks + end end -- libgit2 0.21.2