-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PART 1] Add task pagination endpoint for user tasks #14252
Changes from 1 commit
27d78c2
320a08c
57bea1e
f953efb
7d82126
19ed556
f9ab9a5
3ec5607
99928e0
b9cf987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# shared code for controllers that paginate tasks. | ||
# requires methods: | ||
# * total_items | ||
# * allowed_params | ||
# | ||
module TaskPaginationConcern | ||
extend ActiveSupport::Concern | ||
|
||
def pagination_json | ||
{ | ||
tasks: json_tasks(task_pager.paged_tasks), | ||
task_page_count: task_pager.task_page_count, | ||
total_task_count: task_pager.total_task_count, | ||
tasks_per_page: TaskPager::TASKS_PER_PAGE | ||
} | ||
end | ||
|
||
private | ||
|
||
def task_pager | ||
@task_pager ||= TaskPager.new( | ||
assignee: assignee, | ||
tab_name: params[Constants.QUEUE_CONFIG.TAB_NAME_REQUEST_PARAM.to_sym], | ||
page: params[Constants.QUEUE_CONFIG.PAGE_NUMBER_REQUEST_PARAM.to_sym], | ||
sort_order: params[Constants.QUEUE_CONFIG.SORT_DIRECTION_REQUEST_PARAM.to_sym], | ||
sort_by: params[Constants.QUEUE_CONFIG.SORT_COLUMN_REQUEST_PARAM.to_sym], | ||
filters: params[Constants.QUEUE_CONFIG.FILTER_COLUMN_REQUEST_PARAM.to_sym] | ||
) | ||
end | ||
|
||
def json_tasks(tasks) | ||
tasks = AppealRepository.eager_load_legacy_appeals_for_tasks(tasks) | ||
params = { user: current_user } | ||
|
||
AmaAndLegacyTaskSerializer.new( | ||
tasks: tasks, params: params, ama_serializer: WorkQueue::TaskSerializer | ||
).call | ||
end | ||
end | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class Users::TaskPagesController < UsersController | ||
include TaskPaginationConcern | ||
|
||
hschallhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# /users/{user.id}/task_pages? | ||
# tab=on_hold& | ||
# sort_by=case_details_link& | ||
# order=desc& | ||
# filter[]=col%3Ddocket_type%26val%3Dlegacy& | ||
# filter[]=col%3Dtask_action%26val%3Dtranslation& | ||
# page=3 | ||
# | ||
hschallhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# params = <ActionController::Parameters { | ||
# "tab"=>"on_hold", | ||
# "sort_by"=>"case_details_link", | ||
# "order"=>"desc", | ||
# "filter"=>[ | ||
# "col=docketNumberColumn&val=legacy,evidence_submission", | ||
# "col=taskColumn&val=Unaccredited rep,Extension" | ||
# ], | ||
# "page"=>"3" | ||
# }> | ||
|
||
def index | ||
render json: pagination_json | ||
end | ||
|
||
private | ||
|
||
def assignee | ||
user | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,10 +141,6 @@ def completed_tasks_tab | |
::OrganizationCompletedTasksTab.new(assignee: self, show_regional_office_column: show_regional_office_in_queue?) | ||
end | ||
|
||
def ama_task_serializer | ||
WorkQueue::TaskSerializer | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
private | ||
|
||
def clean_url | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ def attach_tasks_to_tab(tab) | |
# This allows us to only instantiate TaskPager if we are using the task pages API. | ||
task_page_count: task_pager.task_page_count, | ||
total_task_count: tab.tasks.count, | ||
task_page_endpoint_base_path: "#{assignee_is_org? ? "#{assignee.path}/" : ''}#{endpoint}" | ||
task_page_endpoint_base_path: "#{assignee_is_org? ? "#{assignee.path}/" : "users/#{assignee.id}/"}#{endpoint}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. URL the front end will use to request paged tasks |
||
) | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,10 +218,11 @@ | |
patch "/messages/:id", to: "inbox#update" | ||
end | ||
|
||
resources :users, only: [:index, :update] | ||
resources :users, only: [:index] do | ||
resources :users, only: [:index, :update] do | ||
resources :task_pages, only: [:index], controller: 'users/task_pages' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New route for the pagination of user tasks |
||
get 'represented_organizations', on: :member | ||
end | ||
|
||
get 'user', to: 'users#search' | ||
get 'user_info/represented_organizations' | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Organizations::TaskPagesController, :postgres, type: :controller do | ||
let(:organization) { create(:organization) } | ||
let(:url) { organization.url } | ||
require_relative "../paged_tasks_shared_examples" | ||
|
||
let(:assignee) { create(:organization) } | ||
let(:url) { assignee.url } | ||
let(:params) { { organization_url: url, tab: tab_name } } | ||
|
||
let(:user) { create(:user) } | ||
|
||
before do | ||
organization.add_user(user) | ||
assignee.add_user(user) | ||
User.authenticate!(user: user) | ||
end | ||
|
||
describe "GET organization/:organization_url/task_pages" do | ||
context "when user is member of the organization and the organization has tasks" do | ||
let(:tab_name) { Constants.QUEUE_CONFIG.UNASSIGNED_TASKS_TAB_NAME } | ||
let(:task_count) { 4 } | ||
|
||
before { create_list(:ama_task, task_count, assigned_to: organization) } | ||
|
||
subject do | ||
get(:index, params: { organization_url: url, tab: tab_name }) | ||
expect(response.status).to eq(200) | ||
JSON.parse(response.body) | ||
end | ||
|
||
it "returns correct elements of the response" do | ||
expect(subject.keys).to match_array(%w[tasks task_page_count total_task_count tasks_per_page]) | ||
end | ||
|
||
it "returns correct number of tasks" do | ||
expect(subject["tasks"]["data"].size).to eq(task_count) | ||
end | ||
|
||
it "returns correct task_page_count" do | ||
expect(subject["task_page_count"]).to eq((task_count.to_f / TaskPager::TASKS_PER_PAGE).ceil) | ||
end | ||
|
||
it "returns correct total_task_count" do | ||
expect(subject["total_task_count"]).to eq(task_count) | ||
end | ||
|
||
it "returns correct tasks_per_page" do | ||
expect(subject["tasks_per_page"]).to eq(TaskPager::TASKS_PER_PAGE) | ||
end | ||
|
||
it "only instantiates TaskPager a single time" do | ||
task_pager = instance_double(TaskPager) | ||
expect(TaskPager).to receive(:new).and_return(task_pager).exactly(1).times | ||
|
||
expect(task_pager).to receive(:paged_tasks) | ||
expect(task_pager).to receive(:task_page_count) | ||
expect(task_pager).to receive(:total_task_count) | ||
|
||
# Prevent this call from actually firing since it will fail due to the instance_double. | ||
allow_any_instance_of(::Organizations::TaskPagesController).to receive(:json_tasks).and_return([]) | ||
|
||
subject | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pulled out into spec/controllers/paged_tasks_shared_examples.rb |
||
it_behaves_like "paged assignee tasks" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
# Shared examples for user and organization task pages controllers | ||
|
||
shared_examples "paged assignee tasks" do | ||
let(:tab_name) { assignee.class.default_active_tab } | ||
let(:task_count) { 4 } | ||
hschallhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
before { create_list(:ama_task, task_count, assigned_to: assignee) } | ||
|
||
subject do | ||
get(:index, params: params) | ||
expect(response.status).to eq(200) | ||
JSON.parse(response.body) | ||
end | ||
|
||
it "returns correct elements of the response" do | ||
expect(subject.keys).to match_array(%w[tasks task_page_count total_task_count tasks_per_page]) | ||
end | ||
|
||
it "returns correct number of tasks" do | ||
expect(subject["tasks"]["data"].size).to eq(task_count) | ||
end | ||
|
||
it "returns correct task_page_count" do | ||
expect(subject["task_page_count"]).to eq((task_count.to_f / TaskPager::TASKS_PER_PAGE).ceil) | ||
end | ||
|
||
it "returns correct total_task_count" do | ||
expect(subject["total_task_count"]).to eq(task_count) | ||
end | ||
|
||
it "returns correct tasks_per_page" do | ||
expect(subject["tasks_per_page"]).to eq(TaskPager::TASKS_PER_PAGE) | ||
end | ||
|
||
it "only instantiates TaskPager a single time" do | ||
task_pager = instance_double(TaskPager) | ||
expect(TaskPager).to receive(:new).and_return(task_pager).exactly(1).times | ||
|
||
expect(task_pager).to receive(:paged_tasks) | ||
expect(task_pager).to receive(:task_page_count) | ||
expect(task_pager).to receive(:total_task_count) | ||
|
||
# Prevent this call from actually firing since it will fail due to the instance_double. | ||
allow_any_instance_of(TaskPaginationConcern).to receive(:json_tasks).and_return([]) | ||
|
||
subject | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Users::TaskPagesController, :postgres, type: :controller do | ||
require_relative "../paged_tasks_shared_examples" | ||
|
||
let(:assignee) { create(:user) } | ||
let(:url) { assignee.id } | ||
let(:params) { { user_id: url, tab: tab_name } } | ||
|
||
before do | ||
User.authenticate!(user: assignee) | ||
end | ||
|
||
describe "GET user/:user_id/task_pages" do | ||
it_behaves_like "paged assignee tasks" | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled out of app/controllers/organizations/task_pages_controller.rb to be reused in app/controllers/users/task_pages_controller.rb