Skip to content

Commit

Permalink
fix: 500 error if paginated_stats are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
adeel.tajamul committed Nov 23, 2023
1 parent f408aa2 commit ceb9dc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 13 additions & 6 deletions api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@
]
}}
]).to_a[0]
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
if paginated_stats["pagination"].empty?
data = []
num_pages = 0
page = 0
total_count = 0
else
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
end
end
else
# If a list of usernames is provided, then sort by the order in which those names appear
Expand Down
8 changes: 8 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ def build_structure_and_response(course_id, authors, build_initial_stats = true,
expect(res["user_stats"]).to eq expected_result
end

it "API doesn't fail if user has no activity" do
invalid_course_id = "course-v1:edX+DNE+Not_EXISTS"
get "/api/v1/users/#{invalid_course_id}/stats"
expect(last_response.status).to eq(200)
res = parse(last_response.body)
expect(res["user_stats"]).to eq []
end

it "returns user's stats filtered by user with default/activity sort" do
usernames = %w[userauthor-1 userauthor-2 userauthor-3].sample(2)
usernames = usernames.shuffle.join(',')
Expand Down

0 comments on commit ceb9dc1

Please sign in to comment.