mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
Prevent account deletions when account is linked to a Plaid Item (#2218)
* Prevent account deletions when account is linked to a Plaid Item * Only guard deletions in UI and controller, not at model level
This commit is contained in:
parent
628d266980
commit
a67f36bf64
7 changed files with 39 additions and 25 deletions
|
@ -50,8 +50,12 @@ module AccountableResource
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@account.destroy_later
|
if @account.linked?
|
||||||
redirect_to accounts_path, notice: t("accounts.destroy.success", type: accountable_type.name.underscore.humanize)
|
redirect_to account_path(@account), alert: "Cannot delete a linked account"
|
||||||
|
else
|
||||||
|
@account.destroy_later
|
||||||
|
redirect_to accounts_path, notice: t("accounts.destroy.success", type: accountable_type.name.underscore.humanize)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -45,10 +45,6 @@ module ApplicationHelper
|
||||||
content_for(:header_description) { page_description }
|
content_for(:header_description) { page_description }
|
||||||
end
|
end
|
||||||
|
|
||||||
def family_stream
|
|
||||||
turbo_stream_from Current.family if Current.family
|
|
||||||
end
|
|
||||||
|
|
||||||
def page_active?(path)
|
def page_active?(path)
|
||||||
current_page?(path) || (request.path.start_with?(path) && path != "/")
|
current_page?(path) || (request.path.start_with?(path) && path != "/")
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,13 +15,20 @@ class PlaidAccount < ApplicationRecord
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def find_or_create_from_plaid_data!(plaid_data, family)
|
def find_or_create_from_plaid_data!(plaid_data, family)
|
||||||
find_or_create_by!(plaid_id: plaid_data.account_id) do |a|
|
PlaidAccount.transaction do
|
||||||
a.account = family.accounts.new(
|
plaid_account = find_or_create_by!(plaid_id: plaid_data.account_id)
|
||||||
name: plaid_data.name,
|
|
||||||
balance: plaid_data.balances.current || plaid_data.balances.available,
|
internal_account = family.accounts.find_or_initialize_by(plaid_account_id: plaid_account.id)
|
||||||
currency: plaid_data.balances.iso_currency_code,
|
|
||||||
accountable: TYPE_MAPPING[plaid_data.type].new
|
internal_account.name = plaid_data.name
|
||||||
)
|
internal_account.balance = plaid_data.balances.current || plaid_data.balances.available
|
||||||
|
internal_account.currency = plaid_data.balances.iso_currency_code
|
||||||
|
internal_account.accountable = TYPE_MAPPING[plaid_data.type].new
|
||||||
|
|
||||||
|
internal_account.save!
|
||||||
|
plaid_account.save!
|
||||||
|
|
||||||
|
plaid_account
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,10 @@
|
||||||
<%= form.hidden_field :return_to, value: params[:return_to] %>
|
<%= form.hidden_field :return_to, value: params[:return_to] %>
|
||||||
|
|
||||||
<%= form.text_field :name, placeholder: t(".name_placeholder"), required: "required", label: t(".name_label") %>
|
<%= form.text_field :name, placeholder: t(".name_placeholder"), required: "required", label: t(".name_label") %>
|
||||||
<%= form.money_field :balance, label: t(".balance"), required: true, default_currency: Current.family.currency %>
|
|
||||||
|
<% unless account.linked? %>
|
||||||
|
<%= form.money_field :balance, label: t(".balance"), required: true, default_currency: Current.family.currency %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= yield form %>
|
<%= yield form %>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,13 +13,15 @@
|
||||||
) %>
|
) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% menu.with_item(
|
<% unless account.linked? %>
|
||||||
variant: "button",
|
<% menu.with_item(
|
||||||
text: "Delete account",
|
variant: "button",
|
||||||
href: account_path(account),
|
text: "Delete account",
|
||||||
method: :delete,
|
href: account_path(account),
|
||||||
icon: "trash-2",
|
method: :delete,
|
||||||
confirm: CustomConfirm.for_resource_deletion("account", high_severity: true),
|
icon: "trash-2",
|
||||||
data: { turbo_frame: :_top }
|
confirm: CustomConfirm.for_resource_deletion("account", high_severity: true),
|
||||||
) %>
|
data: { turbo_frame: :_top }
|
||||||
|
) %>
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -30,7 +30,9 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= family_stream %>
|
<% if Current.family %>
|
||||||
|
<%= turbo_stream_from Current.family %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= turbo_frame_tag "modal" %>
|
<%= turbo_frame_tag "modal" %>
|
||||||
<%= turbo_frame_tag "drawer" %>
|
<%= turbo_frame_tag "drawer" %>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<%# locals: (message:) %>
|
<%# locals: (message:) %>
|
||||||
|
|
||||||
<%= tag.div class: "flex gap-3 rounded-lg bg-container-inset p-4 group w-full md:max-w-80 shadow-border-lg",
|
<%= tag.div class: "flex gap-3 rounded-lg bg-container p-4 group w-full md:max-w-80 shadow-border-lg",
|
||||||
data: { controller: "element-removal" } do %>
|
data: { controller: "element-removal" } do %>
|
||||||
<div class="h-5 w-5 shrink-0 p-px text-primary">
|
<div class="h-5 w-5 shrink-0 p-px text-primary">
|
||||||
<div class="flex h-full items-center justify-center rounded-full bg-destructive">
|
<div class="flex h-full items-center justify-center rounded-full bg-destructive">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue