From dde4f7c8473b3520887cc3f778f4e683546b3680 Mon Sep 17 00:00:00 2001 From: KatharinaGrett Date: Mon, 22 Feb 2021 13:50:34 +0100 Subject: [PATCH 1/2] added error handling --- .../admin/localpool/gap_contract_customer_base.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb b/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb index 51628261b..583d55ef5 100644 --- a/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb +++ b/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb @@ -23,13 +23,21 @@ def unassign_and_delete_gap_contract_customer(resource:, **) #because of foreign_key constraints the gap contract customer must be unassigned from the gap contract before it can be deleted resource.object.gap_contract_customer = nil resource.object.save! - gap_contract_customer.delete + begin + gap_contract_customer.delete + rescue ActiveRecord::InvalidForeignKey => e + raise Buzzn::ValidationError.new({gap_contract_costumer: ['gap contract costumer was unassigned but could not be deleted']}, resource.object) + end #in case the gap contract customer is an organisation, #the gap_contract customer contact and the gap contract customer legal representation #can only be deleted after the contact has been deleted, due to foreign_key_constraints unless gap_contract_customer_contact.nil? || gap_contract_customer_legal_representation.nil? - gap_contract_customer_contact.delete - gap_contract_customer_legal_representation.delete + begin + gap_contract_customer_contact.delete + gap_contract_customer_legal_representation.delete + rescue ActiveRecord::InvalidForeignKey => e + raise Buzzn::ValidationError.new({gap_contract_costumer: ['gap contract costumer was unassigned but could not be deleted']}, resource.object) + end end resource.object end From a7173fcd3ae30da4441f2205fff6379aaf46d369 Mon Sep 17 00:00:00 2001 From: KatharinaGrett Date: Tue, 23 Feb 2021 14:38:05 +0100 Subject: [PATCH 2/2] stting savepoint before deletion --- .../admin/localpool/gap_contract_customer_base.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb b/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb index 583d55ef5..bbd6e2a0c 100644 --- a/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb +++ b/lib/buzzn/transactions/admin/localpool/gap_contract_customer_base.rb @@ -23,10 +23,11 @@ def unassign_and_delete_gap_contract_customer(resource:, **) #because of foreign_key constraints the gap contract customer must be unassigned from the gap contract before it can be deleted resource.object.gap_contract_customer = nil resource.object.save! + ActiveRecord::Base.connection.execute 'SAVEPOINT unassign' begin gap_contract_customer.delete rescue ActiveRecord::InvalidForeignKey => e - raise Buzzn::ValidationError.new({gap_contract_costumer: ['gap contract costumer was unassigned but could not be deleted']}, resource.object) + ActiveRecord::Base.connection.execute 'ROLLBACK to unassign' end #in case the gap contract customer is an organisation, #the gap_contract customer contact and the gap contract customer legal representation @@ -36,7 +37,7 @@ def unassign_and_delete_gap_contract_customer(resource:, **) gap_contract_customer_contact.delete gap_contract_customer_legal_representation.delete rescue ActiveRecord::InvalidForeignKey => e - raise Buzzn::ValidationError.new({gap_contract_costumer: ['gap contract costumer was unassigned but could not be deleted']}, resource.object) + ActiveRecord::Base.connection.execute 'ROLLBACK to unassign' end end resource.object