DEV: Update change status on solve implementation

Update tests to verify that the change status on solve feature is working as expected.
Change the implementation to loop throught the topic assignments and update the status.
This commit is contained in:
Gabriel Grubba 2024-05-08 12:05:49 -03:00
parent b4b6f8da65
commit 14e04c51ca
No known key found for this signature in database
GPG Key ID: 5FE41764F62D556C
2 changed files with 42 additions and 12 deletions

View File

@ -591,19 +591,47 @@ after_initialize do
if defined?(DiscourseAssign) if defined?(DiscourseAssign)
on(:accepted_solution) do |post| on(:accepted_solution) do |post|
next if SiteSetting.assignment_status_on_solve.blank? next if SiteSetting.assignment_status_on_solve.blank?
assigned_user = User.find_by(id: post.topic.assignment.assigned_to_id) assignements = Assignment.where(topic: post.topic)
Assigner.new(post.topic, assigned_user).assign( assignements.each do |assignment|
assigned_user, assigned_user = User.find_by(id: assignment.assigned_to_id)
status: SiteSetting.assignment_status_on_solve, target_id = assignment.target_id
)
target =
case assignment.target_type
when "Post"
Post.find_by(id: target_id)
when "Topic"
Topic.find_by(id: target_id)
else
post.topic
end
Assigner.new(target, assigned_user).assign(
assigned_user,
status: SiteSetting.assignment_status_on_solve,
)
end
end end
on(:unaccepted_solution) do |post| on(:unaccepted_solution) do |post|
next if SiteSetting.assignment_status_on_unsolve.blank? next if SiteSetting.assignment_status_on_unsolve.blank?
assigned_user = User.find_by(id: post.topic.assignment.assigned_to_id) assignements = Assignment.where(topic: post.topic)
Assigner.new(post.topic, assigned_user).assign( assignements.each do |assignment|
assigned_user, assigned_user = User.find_by(id: assignment.assigned_to_id)
status: SiteSetting.assignment_status_on_unsolve, target_id = assignment.target_id
) target =
case assignment.target_type
when "Post"
Post.find_by(id: target_id)
when "Topic"
Topic.find_by(id: target_id)
else
post.topic
end
Assigner.new(target, assigned_user).assign(
assigned_user,
status: SiteSetting.assignment_status_on_unsolve,
)
end
end end
end end
end end

View File

@ -466,7 +466,7 @@ RSpec.describe "Managing Posts solved status" do
DiscourseSolved.accept_answer!(p1, user) DiscourseSolved.accept_answer!(p1, user)
expect(p1.topic.assignment.status).to eq("Done") expect(p1.reload.topic.assignment.reload.status).to eq("Done")
DiscourseSolved.unaccept_answer!(p1) DiscourseSolved.unaccept_answer!(p1)
@ -491,14 +491,16 @@ RSpec.describe "Managing Posts solved status" do
expect(result[:success]).to eq(true) expect(result[:success]).to eq(true)
post_response = Fabricate(:post, topic: topic_question, user: user_3) post_response = Fabricate(:post, topic: topic_question, user: user_3)
Assigner.new(post_response, user_3).assign(user_3)
DiscourseSolved.accept_answer!(post_response, user_1) DiscourseSolved.accept_answer!(post_response, user_1)
expect(topic_question.assignment.assigned_to_id).to eq(user_2.id) expect(topic_question.assignment.assigned_to_id).to eq(user_2.id)
expect(post_response.assignment.assigned_to_id).to eq(user_3.id)
DiscourseSolved.unaccept_answer!(post_response) DiscourseSolved.unaccept_answer!(post_response)
expect(topic_question.assignment.assigned_to_id).to eq(user_2.id) expect(topic_question.assignment.assigned_to_id).to eq(user_2.id)
expect(post_response.assignment.assigned_to_id).to eq(user_3.id)
end end
describe "assigned topic reminder" describe "assigned topic reminder"