Commit da591cc04203ce0011b83f38d469709c6039245f

Authored by Victor Costa
Committed by Leandro Santos
1 parent b047dfa3
Exists in fix_sign_up_form

api: list all pending tasks for the current person

app/api/entities.rb
... ... @@ -263,6 +263,13 @@ module Api
263 263 root 'tasks', 'task'
264 264 expose :id
265 265 expose :type
  266 + expose :requestor, using: Profile
  267 + expose :status
  268 + expose :created_at
  269 + expose :target do |task, options|
  270 + type_map = {Profile => ::Profile, Environment => ::Environment}.find {|h| task.target.kind_of?(h.last)}
  271 + type_map.first.represent(task.target) unless type_map.nil?
  272 + end
266 273 end
267 274  
268 275 class Environment < Entity
... ...
app/api/v1/tasks.rb
... ... @@ -3,8 +3,11 @@ module Api
3 3 class Tasks < Grape::API
4 4 before { authenticate! }
5 5  
  6 + MAX_PER_PAGE = 50
  7 +
6 8 resource :tasks do
7 9  
  10 + paginate max_per_page: MAX_PER_PAGE
8 11 # Collect tasks
9 12 #
10 13 # Parameters:
... ... @@ -15,7 +18,13 @@ module Api
15 18 # Example Request:
16 19 # GET host/api/v1/tasks?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317
17 20 get do
18   - present_tasks(environment)
  21 + if params[:all_pending].present?
  22 + tasks = current_person.all_pending_tasks
  23 + else
  24 + tasks = select_filtered_collection_of(environment, 'tasks', params)
  25 + tasks = tasks.select {|t| current_person.has_permission?(t.permission, environment)}
  26 + end
  27 + present_partial paginate(tasks), :with => Entities::Task
19 28 end
20 29  
21 30 desc "Return the task id"
... ...
app/models/person.rb
... ... @@ -440,6 +440,12 @@ class Person &lt; Profile
440 440 organization.tasks.pending.select{|task| self.has_permission?(task.permission, organization)}
441 441 end
442 442  
  443 + def all_pending_tasks
  444 + [self.memberships, environment].flatten.map do |target|
  445 + target.tasks.pending.select{|task| self.has_permission?(task.permission, target)}
  446 + end.flatten + tasks.pending
  447 + end
  448 +
443 449 def build_contact(profile, params = {})
444 450 Contact.new(params.merge(:name => name, :email => email, :sender => self, :dest => profile))
445 451 end
... ...
test/api/task_test.rb
... ... @@ -271,4 +271,15 @@ class TasksTest &lt; ActiveSupport::TestCase
271 271 assert_equal enterprise, Task.last.target
272 272 end
273 273  
  274 + should 'list all pending tasks for the current person' do
  275 + task1 = create(Task, :requestor => person, :target => person)
  276 + task2 = create(Task, :requestor => person, :target => person)
  277 + task3 = create(Task, :requestor => person, :target => person)
  278 + params[:per_page] = 2
  279 + params[:all_pending] = true
  280 + get "/api/v1/tasks?#{params.to_query}"
  281 + json = JSON.parse(last_response.body)
  282 + assert_equal [task3.id, task2.id], json["tasks"].map {|t| t["id"]}
  283 + end
  284 +
274 285 end
... ...
test/unit/person_test.rb
... ... @@ -1958,6 +1958,7 @@ class PersonTest &lt; ActiveSupport::TestCase
1958 1958 person.save!
1959 1959 end
1960 1960  
  1961 +<<<<<<< HEAD
1961 1962 should 'update profile circles for a person' do
1962 1963 person = create_user('testuser').person
1963 1964 community = fast_create(Community)
... ... @@ -2005,4 +2006,21 @@ class PersonTest &lt; ActiveSupport::TestCase
2005 2006 person.remove_profile_from_circle(community, circle)
2006 2007 assert_equivalent [circle2], ProfileFollower.with_profile(community).with_follower(person).map(&:circle)
2007 2008 end
  2009 +
  2010 + should 'return all pending task for a person' do
  2011 + person = create_user('testuser').person
  2012 + community1 = fast_create(Community)
  2013 + community1.add_admin(person)
  2014 + community2 = fast_create(Community)
  2015 + task1 = Task.new
  2016 + task2 = Task.new
  2017 + task3 = Task.new
  2018 + task4 = Task.new
  2019 + person.tasks << task1
  2020 + community1.tasks << task2
  2021 + community2.tasks << task3
  2022 + person.environment.tasks << task4
  2023 + person.environment.add_admin(person)
  2024 + assert_equivalent [task4, task2, task1], person.all_pending_tasks
  2025 + end
2008 2026 end
... ...