Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: If a group is deleted also remove assignments #592

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@ module ::DiscourseAssign
end
end

on(:group_destroyed) do |group, user_ids|
user_ids.each do |user_id|
user = User.find(user_id)
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
oblakeerickson marked this conversation as resolved.
Show resolved Hide resolved
user.notifications.for_assignment(group.assignments.select(:id)).destroy_all if user
end

Assignment.active_for_group(group).destroy_all
end

register_search_advanced_filter(/in:assigned/) do |posts|
posts.where(<<~SQL) if @guardian.can_assign?
topics.id IN (
Expand Down
27 changes: 27 additions & 0 deletions spec/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,32 @@
expect_job_enqueued(job: Jobs::AssignNotification, args: { assignment_id: assignment.id })
end
end

describe "on 'group_destroyed'" do
let(:group) { Fabricate(:group) }
let(:user) { Fabricate(:user) }
let(:first_assignment) { Fabricate(:topic_assignment, assigned_to: group) }
let(:second_assignment) { Fabricate(:post_assignment, assigned_to: group) }

before do
group.users << user
Fabricate(
:notification,
notification_type: Notification.types[:assigned],
user: user,
data: { assignment_id: first_assignment.id }.to_json,
)
Fabricate(
:notification,
notification_type: Notification.types[:assigned],
user: user,
data: { assignment_id: second_assignment.id }.to_json,
)
end

it "removes user's notifications related to group assignments" do
expect { group.destroy }.to change { user.notifications.assigned.count }.by(-2)
end
end
end
end