Skip to content

Commit cac9a76

Browse files
committedNov 27, 2023
FEATURE: Allow to reassign to same user
Currently, when only one user is available for the random auto-assign and if that user was already assigned, then the assign will fail. This patch addresses this issue by allowing to reassign a user who’s already assigned. A new parameter (`allow_self_reassign`) has been added to `Assigner#assign` with a default value of `false`.
1 parent eb073fe commit cac9a76

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed
 

‎lib/assigner.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def first_post
195195
topic.posts.where(post_number: 1).first
196196
end
197197

198-
def forbidden_reasons(assign_to:, type:, note:, status:)
198+
def forbidden_reasons(assign_to:, type:, note:, status:, allow_self_reassign:)
199199
case
200200
when assign_to.is_a?(User) && !can_assignee_see_target?(assign_to)
201201
if topic.private_message?
@@ -211,7 +211,7 @@ def forbidden_reasons(assign_to:, type:, note:, status:)
211211
end
212212
when !can_be_assigned?(assign_to)
213213
assign_to.is_a?(User) ? :forbidden_assign_to : :forbidden_group_assign_to
214-
when already_assigned?(assign_to, type, note, status)
214+
when !allow_self_reassign && already_assigned?(assign_to, type, note, status)
215215
assign_to.is_a?(User) ? :already_assigned : :group_already_assigned
216216
when Assignment.where(topic: topic, active: true).count >= ASSIGNMENTS_PER_TOPIC_LIMIT &&
217217
!reassign?
@@ -252,15 +252,27 @@ def update_details(assign_to, note, status, skip_small_action_post: false)
252252
{ success: true }
253253
end
254254

255-
def assign(assign_to, note: nil, skip_small_action_post: false, status: nil)
255+
def assign(
256+
assign_to,
257+
note: nil,
258+
skip_small_action_post: false,
259+
status: nil,
260+
allow_self_reassign: false
261+
)
256262
assigned_to_type = assign_to.is_a?(User) ? "User" : "Group"
257263

258264
if topic.private_message? && SiteSetting.invite_on_assign
259265
assigned_to_type == "Group" ? invite_group(assign_to) : invite_user(assign_to)
260266
end
261267

262268
forbidden_reason =
263-
forbidden_reasons(assign_to: assign_to, type: assigned_to_type, note: note, status: status)
269+
forbidden_reasons(
270+
assign_to: assign_to,
271+
type: assigned_to_type,
272+
note: note,
273+
status: status,
274+
allow_self_reassign: allow_self_reassign,
275+
)
264276
return { success: false, reason: forbidden_reason } if forbidden_reason
265277

266278
if no_assignee_change?(assign_to) && details_change?(note, status)
@@ -271,7 +283,8 @@ def assign(assign_to, note: nil, skip_small_action_post: false, status: nil)
271283
action_code[:user] = topic.assignment.present? ? "reassigned" : "assigned"
272284
action_code[:group] = topic.assignment.present? ? "reassigned_group" : "assigned_group"
273285

274-
skip_small_action_post = skip_small_action_post || no_assignee_change?(assign_to)
286+
skip_small_action_post =
287+
skip_small_action_post || (!allow_self_reassign && no_assignee_change?(assign_to))
275288

276289
if @target.assignment
277290
Jobs.enqueue(

‎lib/random_assign_utils.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def assign_user!
8787
return create_post_template if post_template
8888
Assigner
8989
.new(topic, Discourse.system_user)
90-
.assign(assigned_user)
90+
.assign(assigned_user, allow_self_reassign: true)
9191
.then do |result|
9292
next if result[:success]
9393
no_one!
@@ -104,7 +104,7 @@ def create_post_template
104104
).create!
105105
Assigner
106106
.new(post, Discourse.system_user)
107-
.assign(assigned_user)
107+
.assign(assigned_user, allow_self_reassign: true)
108108
.then do |result|
109109
next if result[:success]
110110
PostDestroyer.new(Discourse.system_user, post).destroy

‎spec/lib/assigner_spec.rb

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,33 +264,47 @@ def assigned_to?(assignee)
264264
expect(second_assign[:success]).to eq(true)
265265
end
266266

267-
it "fails to assign when the assigned user and note is the same" do
268-
assigner = described_class.new(topic, moderator_2)
269-
assigner.assign(moderator, note: "note me down")
267+
context "when 'allow_self_reassign' is false" do
268+
subject(:assign) do
269+
assigner.assign(moderator, note: other_note, allow_self_reassign: self_reassign)
270+
end
270271

271-
assign = assigner.assign(moderator, note: "note me down")
272+
let(:self_reassign) { false }
273+
let(:assigner) { described_class.new(topic, moderator_2) }
274+
let(:note) { "note me down" }
272275

273-
expect(assign[:success]).to eq(false)
274-
expect(assign[:reason]).to eq(:already_assigned)
275-
end
276+
before { assigner.assign(moderator, note: note) }
276277

277-
it "fails to assign when the assigned user and note is the same" do
278-
assigner = described_class.new(post, moderator_2)
279-
assigner.assign(moderator, note: "note me down")
278+
context "when the assigned user and the note is the same" do
279+
let(:other_note) { note }
280280

281-
assign = assigner.assign(moderator, note: "note me down")
281+
it "fails to assign" do
282+
expect(assign).to match(success: false, reason: :already_assigned)
283+
end
284+
end
282285

283-
expect(assign[:success]).to eq(false)
284-
expect(assign[:reason]).to eq(:already_assigned)
286+
context "when the assigned user is the same but the note is different" do
287+
let(:other_note) { "note me down again" }
288+
289+
it "allows assignment" do
290+
expect(assign).to match(success: true)
291+
end
292+
end
285293
end
286294

287-
it "allows assign when the assigned user is same but note is different" do
288-
assigner = described_class.new(topic, moderator_2)
289-
assigner.assign(moderator, note: "note me down")
295+
context "when 'allow_self_reassign' is true" do
296+
subject(:assign) { assigner.assign(moderator, allow_self_reassign: self_reassign) }
290297

291-
assign = assigner.assign(moderator, note: "note me down again")
298+
let(:self_reassign) { true }
299+
let(:assigner) { described_class.new(topic, moderator_2) }
292300

293-
expect(assign[:success]).to eq(true)
301+
context "when the assigned user is the same" do
302+
before { assigner.assign(moderator) }
303+
304+
it "allows assignment" do
305+
expect(assign).to match(success: true)
306+
end
307+
end
294308
end
295309

296310
it "fails to assign when the assigned user cannot view the pm" do

‎spec/lib/random_assign_utils_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@
182182
end
183183
end
184184

185+
context "when in a group of one person" do
186+
let(:fields) do
187+
{
188+
"assignees_group" => {
189+
"value" => group_1.id,
190+
},
191+
"assigned_topic" => {
192+
"value" => topic_1.id,
193+
},
194+
}
195+
end
196+
197+
context "when user is already assigned" do
198+
before { described_class.automation_script!(ctx, fields, automation) }
199+
200+
it "reassigns them" do
201+
expect { auto_assign }.to change { topic_1.reload.assignment.id }
202+
expect(topic_1.assignment.assigned_to).to eq(user_1)
203+
end
204+
end
205+
end
206+
185207
context "when assignees_group is not provided" do
186208
let(:fields) { { "assigned_topic" => { "value" => topic_1.id } } }
187209

0 commit comments

Comments
 (0)
Please sign in to comment.