mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
refactor(accounts): Improve destroy_later atomicity and add controller error handling (#1395)
* refactor(accounts): Improve destroy_later atomicity and add controller error handling * Address PR comment --------- Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
@@ -113,8 +113,13 @@ class AccountsController < ApplicationController
|
|||||||
if @account.linked?
|
if @account.linked?
|
||||||
redirect_to account_path(@account), alert: t("accounts.destroy.cannot_delete_linked")
|
redirect_to account_path(@account), alert: t("accounts.destroy.cannot_delete_linked")
|
||||||
else
|
else
|
||||||
@account.destroy_later
|
begin
|
||||||
redirect_to accounts_path, notice: t("accounts.destroy.success", type: @account.accountable_type)
|
@account.destroy_later
|
||||||
|
redirect_to accounts_path, notice: t("accounts.destroy.success", type: @account.accountable_type)
|
||||||
|
rescue => e
|
||||||
|
Rails.logger.error "Failed to schedule account #{@account.id} for deletion: #{e.message}"
|
||||||
|
redirect_to accounts_path, alert: t("accounts.destroy.failed")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
class DestroyJob < ApplicationJob
|
class DestroyJob < ApplicationJob
|
||||||
queue_as :low_priority
|
queue_as :low_priority
|
||||||
|
self.enqueue_after_transaction_commit = :never
|
||||||
|
|
||||||
def perform(model)
|
def perform(model)
|
||||||
model.destroy
|
model.destroy
|
||||||
|
|||||||
@@ -311,8 +311,10 @@ class Account < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy_later
|
def destroy_later
|
||||||
mark_for_deletion!
|
transaction do
|
||||||
DestroyJob.perform_later(self)
|
mark_for_deletion!
|
||||||
|
DestroyJob.perform_later(self)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Override destroy to handle error recovery for accounts
|
# Override destroy to handle error recovery for accounts
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ en:
|
|||||||
destroy:
|
destroy:
|
||||||
success: "%{type} account scheduled for deletion"
|
success: "%{type} account scheduled for deletion"
|
||||||
cannot_delete_linked: "Cannot delete a linked account. Please unlink it first."
|
cannot_delete_linked: "Cannot delete a linked account. Please unlink it first."
|
||||||
|
failed: "Resource deletion failed. Try again later."
|
||||||
empty:
|
empty:
|
||||||
empty_message: Add an account either via connection, importing or entering manually.
|
empty_message: Add an account either via connection, importing or entering manually.
|
||||||
new_account: New account
|
new_account: New account
|
||||||
|
|||||||
Reference in New Issue
Block a user