Sort PlannerItems API by applicable dates

Closes FALCOR-391

Test Plan:
* As a student
* In a course with Student Planner enabled
* GET /api/v1/planner/items
* The response should be returned in order by the objects'
  due dates, todo dates, or posted dates
* GET the next page (bookmark) of results
* The response should continue in the same order

Change-Id: Ic0b6ed8f51551ff8839cc11913e94b26709bc0c7
Reviewed-on: https://gerrit.instructure.com/116478
Tested-by: Jenkins
Reviewed-by: Steven Burnett <sburnett@instructure.com>
QA-Review: Dan Sasaki <dsasaki@instructure.com>
Product-Review: Dan Minkevitch <dan@instructure.com>
This commit is contained in:
Dan Minkevitch 2017-06-23 15:13:40 -07:00
parent e736fc2020
commit fcd7816d00
3 changed files with 69 additions and 10 deletions

View File

@ -295,7 +295,7 @@ class PlannerOverridesController < ApplicationController
each_with_object([]) do |(scope_name, scope), all_scopes|
next if scope.blank?
base_model = scope_name == :ungraded_quiz ? Quizzes::Quiz : Assignment
collection = item_collection(scope_name.to_s, scope, base_model, :id)
collection = item_collection(scope_name.to_s, scope, base_model, :due_at, :created_at, :id)
all_scopes << collection
end
scopes
@ -305,7 +305,7 @@ class PlannerOverridesController < ApplicationController
item_collection('unread_discussion_topics',
DiscussionTopic.active.todo_date_between(start_date, end_date).
unread_for(@current_user),
DiscussionTopic, :id)
DiscussionTopic, :todo_date, :posted_at, :delayed_post_at, :last_reply_at, :created_at, :id)
end
def unread_submission_collection
@ -313,25 +313,25 @@ class PlannerOverridesController < ApplicationController
Assignment.active.joins(:submissions).
where(submissions: {id: Submission.unread_for(@current_user).pluck(:id)}).
due_between_with_overrides(start_date, end_date),
Assignment, :id)
Assignment, :due_at, :created_at, :id)
end
def planner_note_collection
item_collection('planner_notes',
PlannerNote.where(user: @current_user, todo_date: @start_date...@end_date),
PlannerNote, :id)
PlannerNote, :todo_date, :created_at, :id)
end
def page_collection
item_collection('pages',
@current_user.wiki_pages_needing_viewing(default_opts),
WikiPage, :id)
WikiPage, :todo_date, :created_at, :id)
end
def ungraded_discussion_collection
item_collection('ungraded_discussions',
@current_user.discussion_topics_needing_viewing(default_opts),
DiscussionTopic, :id)
DiscussionTopic, :todo_date, :posted_at, :delayed_post_at, :last_reply_at, :created_at, :id)
end
def item_collection(label, scope, base_model, *order_by)

View File

@ -521,8 +521,7 @@ class DiscussionTopic < ActiveRecord::Base
where("discussion_topic_participants.id IS NOT NULL
AND (discussion_topic_participants.user_id = :user
AND discussion_topic_participants.workflow_state = 'read')",
user: user).
distinct
user: user)
}
scope :unread_for, lambda { |user|
# TODO: Fix for when participants doesn't include user
@ -530,8 +529,7 @@ class DiscussionTopic < ActiveRecord::Base
where("discussion_topic_participants.id IS NULL
OR (discussion_topic_participants.user_id = :user
AND discussion_topic_participants.workflow_state <> 'read')",
user: user).
distinct
user: user)
}
scope :published, -> { where("discussion_topics.workflow_state = 'active'") }

View File

@ -174,6 +174,67 @@ describe PlannerOverridesController do
end
end
context "date sorting" do
it "should return results in order by object type then date" do
wiki_page_model(course: @course)
@page.todo_date = 1.day.from_now
@page.save!
@assignment3 = course_assignment
@assignment3.due_at = 1.week.ago
@assignment3.save!
get :items_index
response_json = json_parse(response.body)
expect(response_json.length).to eq 4
expect(response_json.map { |i| i["plannable_id"] }).to eq [@assignment3.id, @assignment.id, @assignment2.id, @page.id]
end
end
context "pagination" do
PER_PAGE = 5
def test_page(idx = 0, bookmark = nil)
opts = { per_page: PER_PAGE }
opts.merge(page: bookmark) if bookmark.present?
page = get :items_index, opts
links = Api.parse_pagination_links(page.headers['Link'])
response_json = json_parse(page.body)
expect(response_json.length).to eq PER_PAGE
ids = response_json.map { |i| i["plannable_id"] }
expected_ids = []
PER_PAGE.times.with_index(idx) {|i| expected_ids << @assignments[i].id}
expect(ids).to eq expected_ids
links.detect { |l| l[:rel] == "next" }["page"]
end
before :once do
@assignments = []
20.downto(0) do |i|
asg = course_assignment
asg.due_at = i.days.ago
asg.save!
@assignments << asg
end
end
it "should adhere to per_page" do
get :items_index, per_page: 2
response_json = json_parse(response.body)
expect(response_json.length).to eq 2
expect(response_json.map { |i| i["plannable_id"] }).to eq [@assignments[0].id, @assignments[1].id]
end
it "should paginate results in correct order" do
next_page = ''
10.times do |i|
next_page = test_page(i, next_page)
end
end
end
context "new activity filter" do
it "should return newly created & unseen items" do
dt = @course.discussion_topics.create!(title: "Yes", message: "Please", user: @teacher, todo_date: Time.zone.now)