Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit cfd35db

Browse files
FIX: If a group is deleted also remove assignments (#592)
This fix addresses an issue if a group is deleted topics will remain assigned to all group members with no way to un-assign those users from the original group assignment. Now if a group is deleted any group assignments are also deleted.
1 parent 541ee41 commit cfd35db

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

plugin.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,16 @@ module ::DiscourseAssign
872872
end
873873
end
874874

875+
on(:group_destroyed) do |group, user_ids|
876+
User
877+
.where(id: user_ids)
878+
.find_each do |user|
879+
user.notifications.for_assignment(group.assignments.select(:id)).destroy_all
880+
end
881+
882+
Assignment.active_for_group(group).destroy_all
883+
end
884+
875885
register_search_advanced_filter(/in:assigned/) do |posts|
876886
posts.where(<<~SQL) if @guardian.can_assign?
877887
topics.id IN (

spec/plugin_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,32 @@
146146
expect_job_enqueued(job: Jobs::AssignNotification, args: { assignment_id: assignment.id })
147147
end
148148
end
149+
150+
describe "on 'group_destroyed'" do
151+
let(:group) { Fabricate(:group) }
152+
let(:user) { Fabricate(:user) }
153+
let(:first_assignment) { Fabricate(:topic_assignment, assigned_to: group) }
154+
let(:second_assignment) { Fabricate(:post_assignment, assigned_to: group) }
155+
156+
before do
157+
group.users << user
158+
Fabricate(
159+
:notification,
160+
notification_type: Notification.types[:assigned],
161+
user: user,
162+
data: { assignment_id: first_assignment.id }.to_json,
163+
)
164+
Fabricate(
165+
:notification,
166+
notification_type: Notification.types[:assigned],
167+
user: user,
168+
data: { assignment_id: second_assignment.id }.to_json,
169+
)
170+
end
171+
172+
it "removes user's notifications related to group assignments" do
173+
expect { group.destroy }.to change { user.notifications.assigned.count }.by(-2)
174+
end
175+
end
149176
end
150177
end

0 commit comments

Comments
 (0)