Add field to expose viewable submissions that have comments

closes VICE-2655
flag=react_inbox

Test Plan
1. Test work
2. go to http://localhost:3000/graphiql
3. Verify that new field returns submission data correctly

Here is a graphiql Query to help with testing.

  legacyNode(_id: "5", type: User) {
    ... on User {
      id
      email
      viewableSubmissionsConnection {
        nodes {
          _id
          commentsConnection {
            nodes {
              comment
              author {
                name
              }
              _id
            }
          }
        }
      }
    }
  }

Change-Id: Ie7a152ba41c5a66f0ddcaa7dc54df1e8fb5dd92a
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/288571
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Omar Soto-Fortuño <omar.soto@instructure.com>
Product-Review: Omar Soto-Fortuño <omar.soto@instructure.com>
QA-Review: Caleb Guanzon <cguanzon@instructure.com>
This commit is contained in:
Jason Gillett 2022-03-30 18:07:59 -06:00
parent 82113f7f40
commit e075ce961d
2 changed files with 138 additions and 75 deletions

View File

@ -302,6 +302,26 @@ module Types
end
end
field :viewable_submissions_connection, Types::SubmissionType.connection_type, null: true do
description "All submissions with comments that the current_user is able to view"
end
def viewable_submissions_connection
return unless object == current_user
submissions = []
ssi_scope = current_user.visible_stream_item_instances(only_active_courses: true)
ssi_scope = ssi_scope.eager_load(:stream_item).where("stream_items.asset_type=?", "Submission")
ssi_scope = ssi_scope.joins("INNER JOIN #{Submission.quoted_table_name} ON submissions.id=asset_id")
ssi_scope = ssi_scope.where("submissions.workflow_state <> 'deleted' AND submissions.submission_comments_count>0")
Shard.partition_by_shard(ssi_scope, ->(sii) { sii.stream_item_id }) do |shard_stream_items|
submission_ids = StreamItem.where(id: shard_stream_items.map(&:stream_item_id)).pluck(:asset_id)
submissions += Submission.where(id: submission_ids)
end
submissions
end
field :submission_comments_connection, Types::SubmissionCommentType.connection_type, null: true
def submission_comments_connection
return unless object == current_user

View File

@ -793,6 +793,69 @@ describe Types::UserType do
end
end
describe "submission comments" do
before(:once) do
@course = Course.create! name: "TEST"
@teacher = course_with_user("TeacherEnrollment", course: @course, name: "Mr Teacher", active_all: true).user
@student = course_with_user("StudentEnrollment", course: @course, name: "Mr Student 1", active_all: true).user
assignment = @course.assignments.create!(
name: "Test Assignment",
moderated_grading: true,
grader_count: 10,
final_grader: @teacher
)
assignment2 = @course.assignments.create!(
name: "Assignment without Comments",
moderated_grading: true,
grader_count: 10,
final_grader: @teacher
)
assignment.grade_student(@student, grade: 1, grader: @teacher, provisional: true)
assignment2.grade_student(@student, grade: 1, grader: @teacher, provisional: true)
@student_submission_1 = assignment.submissions.find_by(user: @student)
@sc1 = @student_submission_1.add_comment(author: @student, comment: "First comment")
@sc2 = @student_submission_1.add_comment(author: @teacher, comment: "Second comment")
@sc3 = @student_submission_1.add_comment(author: @teacher, comment: "Third comment")
end
let(:teacher_type) do
GraphQLTypeTester.new(@teacher, current_user: @teacher, domain_root_account: @course.account.root_account, request: ActionDispatch::TestRequest.create)
end
describe "viewableSubmissionsConnection field" do
it "only gets submissions with comments" do
query_result = teacher_type.resolve("viewableSubmissionsConnection { nodes { _id } }")
expect(query_result.count).to eq 1
expect(query_result[0].to_i).to eq @student_submission_1.id
end
it "can retrieve submission comments" do
query_result = teacher_type.resolve("viewableSubmissionsConnection { nodes { commentsConnection { nodes { comment }} } }")
expect(query_result[0].count).to eq 3
expect(query_result[0]).to match_array ["First comment", "Second comment", "Third comment"]
end
it "can get createdAt" do
query_result = teacher_type.resolve("viewableSubmissionsConnection { nodes { commentsConnection { nodes { createdAt }} } }")
retrieved_values = query_result[0].map { |string_date| Time.parse(string_date) }
expect(retrieved_values).to all(be_within(1.minute).of(@sc1.created_at))
end
it "can get assignment names" do
expect(teacher_type.resolve("viewableSubmissionsConnection { nodes { assignment { name } } }")[0]).to eq @student_submission_1.assignment.name
end
it "can get course names" do
expect(teacher_type.resolve("viewableSubmissionsConnection { nodes { commentsConnection { nodes { course { name } } } } }")[0]).to match_array %w[TEST TEST TEST]
end
end
describe "submissionCommentsConnection field" do
def submission_comments_mutation_str(teacher_id)
<<~GQL
{
@ -818,27 +881,6 @@ describe Types::UserType do
GQL
end
describe "submission comments" do
before(:once) do
course = Course.create! name: "TEST"
@teacher = course_with_user("TeacherEnrollment", course: course, name: "Mr Teacher", active_all: true).user
student = course_with_user("StudentEnrollment", course: course, name: "Mr Student 1", active_all: true).user
assignment = course.assignments.create!(
name: "Test Assignment",
moderated_grading: true,
grader_count: 10,
final_grader: @teacher
)
assignment.grade_student(student, grade: 1, grader: @teacher, provisional: true)
@submission = assignment.submissions.find_by(user: student)
@sc1 = @submission.add_comment(author: student, comment: "First comment")
@sc2 = @submission.add_comment(author: @teacher, comment: "Second comment")
@sc3 = @submission.add_comment(author: @teacher, comment: "Third comment")
end
it "can get comments" do
result = CanvasSchema.execute(
submission_comments_mutation_str(@teacher.id),
@ -858,7 +900,7 @@ describe Types::UserType do
nodes = result.dig("data", "legacyNode", "submissionCommentsConnection", "nodes")
expect(nodes.map { |c| c["submissionId"] }).to match_array [@submission.id.to_s, @submission.id.to_s, @submission.id.to_s]
expect(nodes.map { |c| c["submissionId"] }).to match_array [@student_submission_1.id.to_s, @student_submission_1.id.to_s, @student_submission_1.id.to_s]
end
it "can get createdAt" do
@ -897,3 +939,4 @@ describe Types::UserType do
end
end
end
end