Commit 27f3cf90bc54b2cece4eb5bb9074be4461fd95ca

Authored by Victor Costa
1 parent ee9a46b6

api: list all pending tasks for the current person

app/api/entities.rb
... ... @@ -259,6 +259,13 @@ module Api
259 259 root 'tasks', 'task'
260 260 expose :id
261 261 expose :type
  262 + expose :requestor, using: Profile
  263 + expose :status
  264 + expose :created_at
  265 + expose :target do |task, options|
  266 + type_map = {Profile => ::Profile, Environment => ::Environment}.find {|h| task.target.kind_of?(h.last)}
  267 + type_map.first.represent(task.target) unless type_map.nil?
  268 + end
262 269 end
263 270  
264 271 class Environment < Entity
... ...
app/api/v1/tasks.rb
... ... @@ -5,6 +5,8 @@ module Api
5 5  
6 6 # ARTICLE_TYPES = Article.descendants.map{|a| a.to_s}
7 7  
  8 + MAX_PER_PAGE = 50
  9 +
8 10 resource :tasks do
9 11  
10 12 # Collect tasks
... ... @@ -17,9 +19,14 @@ module Api
17 19 # Example Request:
18 20 # GET host/api/v1/tasks?from=2013-04-04-14:41:43&until=2015-04-04-14:41:43&limit=10&private_token=e96fff37c2238fdab074d1dcea8e6317
19 21 get do
20   - tasks = select_filtered_collection_of(environment, 'tasks', params)
21   - tasks = tasks.select {|t| current_person.has_permission?(t.permission, environment)}
22   - present_partial tasks, :with => Entities::Task
  22 + tasks = nil
  23 + if params[:all_pending].present?
  24 + tasks = current_person.all_pending_tasks
  25 + else
  26 + tasks = select_filtered_collection_of(environment, 'tasks', params)
  27 + tasks = tasks.select {|t| current_person.has_permission?(t.permission, environment)}
  28 + end
  29 + present_partial paginate(tasks), :with => Entities::Task
23 30 end
24 31  
25 32 desc "Return the task id"
... ...
app/models/person.rb
... ... @@ -413,6 +413,12 @@ class Person &lt; Profile
413 413 organization.tasks.pending.select{|task| self.has_permission?(task.permission, organization)}
414 414 end
415 415  
  416 + def all_pending_tasks
  417 + self.memberships.map do |organization|
  418 + organization.tasks.pending.select{|task| self.has_permission?(task.permission, organization)}
  419 + end.flatten + tasks.pending
  420 + end
  421 +
416 422 def build_contact(profile, params = {})
417 423 Contact.new(params.merge(:name => name, :email => email, :sender => self, :dest => profile))
418 424 end
... ...
test/api/task_test.rb
... ... @@ -170,4 +170,15 @@ class TasksTest &lt; ActiveSupport::TestCase
170 170  
171 171 assert_equal enterprise, Task.last.target
172 172 end
  173 +
  174 + should 'list all pending tasks for the current person' do
  175 + task1 = create(Task, :requestor => person, :target => person)
  176 + task2 = create(Task, :requestor => person, :target => person)
  177 + task3 = create(Task, :requestor => person, :target => person)
  178 + params[:per_page] = 2
  179 + params[:all_pending] = true
  180 + get "/api/v1/tasks?#{params.to_query}"
  181 + json = JSON.parse(last_response.body)
  182 + assert_equal [task3.id, task2.id], json["tasks"].map {|t| t["id"]}
  183 + end
173 184 end
... ...
test/unit/person_test.rb
... ... @@ -1951,4 +1951,17 @@ class PersonTest &lt; ActiveSupport::TestCase
1951 1951 person.save!
1952 1952 end
1953 1953  
  1954 + should 'return all pending task for a person' do
  1955 + person = create_user('testuser').person
  1956 + community1 = fast_create(Community)
  1957 + community1.add_admin(person)
  1958 + community2 = fast_create(Community)
  1959 + task1 = Task.new
  1960 + task2 = Task.new
  1961 + task3 = Task.new
  1962 + person.tasks << task1
  1963 + community1.tasks << task2
  1964 + community2.tasks << task3
  1965 + assert_equivalent [task2, task1], person.all_pending_tasks
  1966 + end
1954 1967 end
... ...