From b7874852eccf5697f4e0b4d6f0d5c9f199a948cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 26 Jul 2021 09:52:19 +0200 Subject: [PATCH] Delete the notification when the WP gets destroyed --- app/services/work_packages/delete_service.rb | 11 +++++++++ .../delete_service_integration_spec.rb | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/app/services/work_packages/delete_service.rb b/app/services/work_packages/delete_service.rb index 2d1a520fd8..5ac546efd6 100644 --- a/app/services/work_packages/delete_service.rb +++ b/app/services/work_packages/delete_service.rb @@ -44,6 +44,7 @@ class WorkPackages::DeleteService < ::BaseServices::Delete end destroy_descendants(descendants, result) + delete_associated(model) end result @@ -58,4 +59,14 @@ class WorkPackages::DeleteService < ::BaseServices::Delete result.add_dependent!(ServiceResult.new(success: descendant.destroy, result: descendant)) end end + + def delete_associated(model) + delete_notifications_resource(model.id) + end + + def delete_notifications_resource(id) + Notification + .where(resource_type: :WorkPackage, resource_id: id) + .delete_all + end end diff --git a/spec/services/work_packages/delete_service_integration_spec.rb b/spec/services/work_packages/delete_service_integration_spec.rb index 0296333bf7..f1a2a24560 100644 --- a/spec/services/work_packages/delete_service_integration_spec.rb +++ b/spec/services/work_packages/delete_service_integration_spec.rb @@ -92,4 +92,28 @@ describe WorkPackages::DeleteService, 'integration', type: :model do expect { work_package.reload }.to raise_error(ActiveRecord::RecordNotFound) end end + + describe 'with a notification' do + let!(:work_package) { FactoryBot.create :work_package, project: project } + let!(:notification) do + FactoryBot.create :notification, + recipient: user, + actor: user, + resource: work_package, + project: project + end + + let(:instance) do + described_class.new(user: user, + model: work_package) + end + + subject { instance.call } + + it 'deletes the notification' do + expect(subject).to be_success + expect { work_package.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end end