Skip to content

Commit ceb9dc1

Browse files
author
adeel.tajamul
committed
fix: 500 error if paginated_stats are empty
1 parent f408aa2 commit ceb9dc1

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

api/users.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@
7171
]
7272
}}
7373
]).to_a[0]
74-
total_count = paginated_stats["pagination"][0]["total_count"]
75-
num_pages = [1, (total_count / per_page.to_f).ceil].max
76-
data = paginated_stats["data"].map do |user_stats|
77-
{
78-
:username => user_stats["username"]
79-
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
74+
if paginated_stats["pagination"].empty?
75+
data = []
76+
num_pages = 0
77+
page = 0
78+
total_count = 0
79+
else
80+
total_count = paginated_stats["pagination"][0]["total_count"]
81+
num_pages = [1, (total_count / per_page.to_f).ceil].max
82+
data = paginated_stats["data"].map do |user_stats|
83+
{
84+
:username => user_stats["username"]
85+
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
86+
end
8087
end
8188
else
8289
# If a list of usernames is provided, then sort by the order in which those names appear

spec/api/user_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ def build_structure_and_response(course_id, authors, build_initial_stats = true,
521521
expect(res["user_stats"]).to eq expected_result
522522
end
523523

524+
it "API doesn't fail if user has no activity" do
525+
invalid_course_id = "course-v1:edX+DNE+Not_EXISTS"
526+
get "/api/v1/users/#{invalid_course_id}/stats"
527+
expect(last_response.status).to eq(200)
528+
res = parse(last_response.body)
529+
expect(res["user_stats"]).to eq []
530+
end
531+
524532
it "returns user's stats filtered by user with default/activity sort" do
525533
usernames = %w[userauthor-1 userauthor-2 userauthor-3].sample(2)
526534
usernames = usernames.shuffle.join(',')

0 commit comments

Comments
 (0)