Skip to content

Commit

Permalink
feat: updated api to allow ordering on endorsed status
Browse files Browse the repository at this point in the history
  • Loading branch information
ayesha-waris committed Feb 10, 2024
1 parent ac710bf commit d18653c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
5 changes: 4 additions & 1 deletion api/comment_threads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
error 404, [t(:requested_object_not_found)].to_json
end

merge_question_type_responses = value_to_boolean(params["merge_question_type_responses"])

# user is required to return user-specific fields, such as "read" (even if bool_mark_as_read is False)
if params["user_id"]
user = User.only([:id, :username, :read_states]).find_by(external_id: params["user_id"])
Expand Down Expand Up @@ -63,7 +65,8 @@
error 400, [t(:param_exceeds_limit, :param => resp_limit, :limit => size_limit)].to_json
end
presenter.to_hash(
bool_with_responses, resp_skip, resp_limit, bool_recursive, bool_flagged_comments, bool_reverse_order
bool_with_responses, resp_skip, resp_limit, bool_recursive, bool_flagged_comments, bool_reverse_order,
merge_question_type_responses
).to_json
end

Expand Down
53 changes: 34 additions & 19 deletions presenters/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def to_hash(
resp_limit=nil,
recursive=true,
flagged_comments=false,
reverse_order=false
reverse_order=false,
merge_question_type_responses=false
)
raise ArgumentError unless resp_skip >= 0
raise ArgumentError unless resp_limit.nil? or resp_limit >= 1
Expand All @@ -48,7 +49,8 @@ def to_hash(
end
sorting_key_order = reverse_order ? -1 : 1
if with_responses
if @thread.thread_type.discussion? && resp_skip == 0 && resp_limit.nil?
if (@thread.thread_type.discussion? || (@thread.thread_type.question? && merge_question_type_responses)) &&
resp_skip == 0 && resp_limit.nil?
if recursive
content = Comment.where(comment_thread_id: @thread._id).order_by({"sk" => sorting_key_order})
else
Expand All @@ -66,28 +68,41 @@ def to_hash(
end
case @thread.thread_type
when "question"
endorsed_responses = responses.where(endorsed: true)
non_endorsed_responses = responses.where(endorsed: false)
endorsed_response_info = get_paged_merged_responses(
if merge_question_type_responses
response_info = get_paged_merged_responses(

Check warning on line 72 in presenters/thread.rb

View check run for this annotation

Codecov / codecov/patch

presenters/thread.rb#L72

Added line #L72 was not covered by tests
@thread._id,
endorsed_responses,
0,
nil,
recursive,
sorting_key_order
)
non_endorsed_response_info = get_paged_merged_responses(
@thread._id,
non_endorsed_responses,
responses,
resp_skip,
resp_limit,
recursive,
sorting_key_order
)
h["endorsed_responses"] = endorsed_response_info["responses"]
h["non_endorsed_responses"] = non_endorsed_response_info["responses"]
h["non_endorsed_resp_total"] = non_endorsed_response_info["response_count"]
h["resp_total"] = non_endorsed_response_info["response_count"] + endorsed_response_info["response_count"]
)
h["children"] = response_info["responses"]
h["resp_total"] = response_info["response_count"]

Check warning on line 81 in presenters/thread.rb

View check run for this annotation

Codecov / codecov/patch

presenters/thread.rb#L80-L81

Added lines #L80 - L81 were not covered by tests
else
endorsed_responses = responses.where(endorsed: true)
non_endorsed_responses = responses.where(endorsed: false)
endorsed_response_info = get_paged_merged_responses(
@thread._id,
endorsed_responses,
0,
nil,
recursive,
sorting_key_order
)
non_endorsed_response_info = get_paged_merged_responses(
@thread._id,
non_endorsed_responses,
resp_skip,
resp_limit,
recursive,
sorting_key_order
)
h["endorsed_responses"] = endorsed_response_info["responses"]
h["non_endorsed_responses"] = non_endorsed_response_info["responses"]
h["non_endorsed_resp_total"] = non_endorsed_response_info["response_count"]
h["resp_total"] = non_endorsed_response_info["response_count"] + endorsed_response_info["response_count"]
end
when "discussion"
response_info = get_paged_merged_responses(
@thread._id,
Expand Down

0 comments on commit d18653c

Please sign in to comment.