Fixes #3536 - Don't fail job is deletable_id is no longer available.

This commit is contained in:
Rolf Schmidt 2021-06-02 12:37:57 +01:00 committed by Thorsten Eckel
parent 649dff172e
commit 23b05e1f20
3 changed files with 19 additions and 4 deletions

View file

@ -19,6 +19,13 @@ class DataPrivacyTask < ApplicationModel
validates_with DataPrivacyTask::Validation validates_with DataPrivacyTask::Validation
def perform def perform
perform_deletable
update!(state: 'completed')
rescue => e
handle_exception(e)
end
def perform_deletable
return if deletable.blank? return if deletable.blank?
prepare_deletion_preview prepare_deletion_preview
@ -29,10 +36,6 @@ class DataPrivacyTask < ApplicationModel
else else
deletable.destroy deletable.destroy
end end
update!(state: 'completed')
rescue => e
handle_exception(e)
end end
def handle_exception(e) def handle_exception(e)

View file

@ -17,12 +17,14 @@ class DataPrivacyTask::Validation < ActiveModel::Validator
private private
def check_for_user def check_for_user
return if !record.deletable_type_changed?
return if deletable_is_user? return if deletable_is_user?
invalid_because(:deletable, 'is not a User') invalid_because(:deletable, 'is not a User')
end end
def check_for_system_user def check_for_system_user
return if !record.deletable_id_changed?
return if !deletable_is_user? return if !deletable_is_user?
return if deletable.id != 1 return if deletable.id != 1
@ -30,6 +32,7 @@ class DataPrivacyTask::Validation < ActiveModel::Validator
end end
def check_for_current_user def check_for_current_user
return if !record.deletable_id_changed?
return if !deletable_is_user? return if !deletable_is_user?
return if deletable.id != UserInfo.current_user_id return if deletable.id != UserInfo.current_user_id
@ -37,6 +40,7 @@ class DataPrivacyTask::Validation < ActiveModel::Validator
end end
def check_for_last_admin def check_for_last_admin
return if !record.deletable_id_changed?
return if !deletable_is_user? return if !deletable_is_user?
return if !last_admin? return if !last_admin?
@ -44,6 +48,7 @@ class DataPrivacyTask::Validation < ActiveModel::Validator
end end
def check_for_existing_task def check_for_existing_task
return if !record.deletable_id_changed?
return if !deletable_is_user? return if !deletable_is_user?
return if !tasks_exists? return if !tasks_exists?

View file

@ -20,6 +20,13 @@ RSpec.describe DataPrivacyTaskJob, type: :job do
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
it 'checks if deletion does not crash if the user is already deleted' do
task = create(:data_privacy_task, deletable: user)
user.destroy
described_class.perform_now
expect(task.reload.state).to eq('completed')
end
it 'checks if the organization is deleted' do it 'checks if the organization is deleted' do
create(:data_privacy_task, deletable: user) create(:data_privacy_task, deletable: user)
described_class.perform_now described_class.perform_now