diff --git a/app/models/data_privacy_task.rb b/app/models/data_privacy_task.rb index 6733400d1..d28569ccd 100644 --- a/app/models/data_privacy_task.rb +++ b/app/models/data_privacy_task.rb @@ -25,19 +25,38 @@ class DataPrivacyTask < ApplicationModel handle_exception(e) end + # set user inactive before destroy to prevent + # new online notifications or other events while + # the deletion process is running + # https://github.com/zammad/zammad/issues/3942 + def update_inactive(object) + object.update(active: false) + end + def perform_deletable - return if deletable.blank? + return if !deletable_type.constantize.exists?(id: deletable_id) prepare_deletion_preview save! if delete_organization? - deletable.organization.destroy(associations: true) + perform_organization else - deletable.destroy + perform_user end end + def perform_organization + update_inactive(deletable.organization) + deletable.organization.members.find_each { |user| update_inactive(user) } + deletable.organization.destroy(associations: true) + end + + def perform_user + update_inactive(deletable) + deletable.destroy + end + def handle_exception(e) Rails.logger.error e preferences[:error] = "ERROR: #{e.inspect}" diff --git a/spec/models/data_privacy_task_spec.rb b/spec/models/data_privacy_task_spec.rb index 2c6a5f63b..a20ff7faa 100644 --- a/spec/models/data_privacy_task_spec.rb +++ b/spec/models/data_privacy_task_spec.rb @@ -36,18 +36,11 @@ RSpec.describe DataPrivacyTask, type: :model do expect(create(:data_privacy_task, deletable: admin)).to be_truthy end - it 'sets the failed state when task failed' do + it 'sets no error message when user is already deleted' do task = create(:data_privacy_task, deletable: user) user.destroy task.perform - expect(task.reload.state).to eq('failed') - end - - it 'sets an error message when task failed' do - task = create(:data_privacy_task, deletable: user) - user.destroy - task.perform - expect(task.reload.preferences[:error]).to eq("ERROR: #") + expect(task.reload.state).to eq('completed') end end