Skip to content

Commit 9db72ef

Browse files
authored
FEATURE: change status on unsolve & fix assign changes (#289)
* FEATURE: change status on unsolve & fix assign changes When a topic is unsolved, it should have an option, defined in the settings, to change its status to that state. Fix assign changes when a topic was solved, previously it was changing the assignee. * DEV: Change names in tests and remove comments * 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. * DEV: address review feedback
1 parent e405ef1 commit 9db72ef

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

config/locales/server.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ en:
1313
notify_on_staff_accept_solved: "Send notification to the topic creator when a post is marked as solution by a staff."
1414
ignore_solved_topics_in_assigned_reminder: "Prevent assigned reminder from including solved topics. only relevant when discourse-assign."
1515
assignment_status_on_solve: "When a topic is solved update all assignments to this status"
16+
assignment_status_on_unsolve: "When a topic is unsolved update all assignments to this status"
1617
disable_solved_education_message: "Disable education message for solved topics."
1718
accept_solutions_topic_author: "Allow the topic author to accept a solution."
1819
solved_add_schema_markup: "Add QAPage schema markup to HTML."

config/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ discourse_solved:
3737
assignment_status_on_solve:
3838
type: string
3939
default: ""
40+
assignment_status_on_unsolve:
41+
type: string
42+
default: ""
4043
disable_solved_education_message:
4144
default: false
4245
accept_solutions_topic_author:

plugin.rb

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def self.skip_db?
303303
if SiteSetting.prioritize_solved_topics_in_search
304304
condition = <<~SQL
305305
EXISTS (
306-
SELECT 1
306+
SELECT 1
307307
FROM topic_custom_fields
308308
WHERE topic_id = topics.id
309309
AND name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
@@ -336,7 +336,7 @@ def self.skip_db?
336336
topics.id IN (
337337
SELECT topic_id
338338
FROM topic_custom_fields
339-
WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
339+
WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
340340
AND value IS NOT NULL
341341
)
342342
SQL
@@ -349,7 +349,7 @@ def self.skip_db?
349349
topics.id NOT IN (
350350
SELECT topic_id
351351
FROM topic_custom_fields
352-
WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
352+
WHERE name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
353353
AND value IS NOT NULL
354354
)
355355
SQL
@@ -361,15 +361,15 @@ def self.skip_db?
361361
topics.id IN (
362362
SELECT t.id
363363
FROM topics t
364-
JOIN category_custom_fields cc
364+
JOIN category_custom_fields cc
365365
ON t.category_id = cc.category_id
366-
AND cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}'
366+
AND cc.name = '#{::DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD}'
367367
AND cc.value = 'true'
368-
)
369-
OR
368+
)
369+
OR
370370
topics.id IN (
371-
SELECT topic_id
372-
FROM topic_tags
371+
SELECT topic_id
372+
FROM topic_tags
373373
WHERE tag_id IN (?)
374374
)
375375
SQL
@@ -404,7 +404,7 @@ def self.skip_db?
404404
->(r) do
405405
sql = <<~SQL
406406
NOT EXISTS (
407-
SELECT 1
407+
SELECT 1
408408
FROM topic_custom_fields
409409
WHERE topic_id = topics.id
410410
AND name = '#{::DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD}'
@@ -591,10 +591,25 @@ def self.skip_db?
591591
if defined?(DiscourseAssign)
592592
on(:accepted_solution) do |post|
593593
next if SiteSetting.assignment_status_on_solve.blank?
594-
Assigner.new(post.topic, post.acting_user).assign(
595-
post.acting_user,
596-
status: SiteSetting.assignment_status_on_solve,
597-
)
594+
assignments = Assignment.includes(:target).where(topic: post.topic)
595+
assignments.each do |assignment|
596+
assigned_user = User.find_by(id: assignment.assigned_to_id)
597+
Assigner.new(assignment.target, assigned_user).assign(
598+
assigned_user,
599+
status: SiteSetting.assignment_status_on_solve,
600+
)
601+
end
602+
end
603+
on(:unaccepted_solution) do |post|
604+
next if SiteSetting.assignment_status_on_unsolve.blank?
605+
assignments = Assignment.includes(:target).where(topic: post.topic)
606+
assignments.each do |assignment|
607+
assigned_user = User.find_by(id: assignment.assigned_to_id)
608+
Assigner.new(assignment.target, assigned_user).assign(
609+
assigned_user,
610+
status: SiteSetting.assignment_status_on_unsolve,
611+
)
612+
end
598613
end
599614
end
600615
end

spec/integration/solved_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
SiteSetting.assign_allowed_on_groups = "#{group.id}"
440440
SiteSetting.assigns_public = true
441441
SiteSetting.assignment_status_on_solve = "Done"
442+
SiteSetting.assignment_status_on_unsolve = "New"
442443
SiteSetting.ignore_solved_topics_in_assigned_reminder = false
443444
group.add(p1.acting_user)
444445
group.add(user)
@@ -458,6 +459,50 @@
458459
expect(p1.topic.assignment.reload.status).to eq("Done")
459460
end
460461

462+
it "update all assignments to this status when a post is unaccepted" do
463+
assigner = Assigner.new(p1.topic, user)
464+
result = assigner.assign(user)
465+
expect(result[:success]).to eq(true)
466+
467+
DiscourseSolved.accept_answer!(p1, user)
468+
469+
expect(p1.reload.topic.assignment.reload.status).to eq("Done")
470+
471+
DiscourseSolved.unaccept_answer!(p1)
472+
473+
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq(nil)
474+
expect(p1.reload.topic.assignment.reload.status).to eq("New")
475+
end
476+
477+
it "does not update the assignee when a post is accepted" do
478+
user_1 = Fabricate(:user)
479+
user_2 = Fabricate(:user)
480+
user_3 = Fabricate(:user)
481+
group.add(user_1)
482+
group.add(user_2)
483+
group.add(user_3)
484+
485+
topic_question = Fabricate(:topic, user: user_1)
486+
post_question = Fabricate(:post, topic: topic_question, user: user_1)
487+
488+
user_2_response = Fabricate(:post, topic: topic_question, user: user_2)
489+
assigner = Assigner.new(topic_question, user_2)
490+
result = assigner.assign(user_2)
491+
expect(result[:success]).to eq(true)
492+
493+
post_response = Fabricate(:post, topic: topic_question, user: user_3)
494+
Assigner.new(post_response, user_3).assign(user_3)
495+
496+
DiscourseSolved.accept_answer!(post_response, user_1)
497+
498+
expect(topic_question.assignment.assigned_to_id).to eq(user_2.id)
499+
expect(post_response.assignment.assigned_to_id).to eq(user_3.id)
500+
DiscourseSolved.unaccept_answer!(post_response)
501+
502+
expect(topic_question.assignment.assigned_to_id).to eq(user_2.id)
503+
expect(post_response.assignment.assigned_to_id).to eq(user_3.id)
504+
end
505+
461506
describe "assigned topic reminder"
462507
it "excludes solved topics when ignore_solved_topics_in_assigned_reminder is false" do
463508
other_topic = Fabricate(:topic, title: "Topic that should be there")

0 commit comments

Comments
 (0)