mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
It would take ages to go through all files now and assess all belongs_to associations. So I just declare the old default and then we can move on and apply the new default for the application while these classes still use the old one. All new models will then use the new default which is the goal of this excercise and we can refactor old classes when we touch them anyway.
26 lines
749 B
Ruby
26 lines
749 B
Ruby
# frozen_string_literal: true
|
|
|
|
class StripeAccount < ApplicationRecord
|
|
self.belongs_to_required_by_default = false
|
|
|
|
belongs_to :enterprise
|
|
validates :stripe_user_id, :stripe_publishable_key, presence: true
|
|
validates :enterprise_id, uniqueness: true
|
|
|
|
def deauthorize_and_destroy
|
|
accounts = StripeAccount.where(stripe_user_id: stripe_user_id)
|
|
|
|
# Only deauthorize the user if it is not linked to multiple accounts
|
|
return destroy if accounts.count > 1
|
|
|
|
destroy && Stripe::OAuth.deauthorize(stripe_user_id: stripe_user_id)
|
|
rescue Stripe::OAuth::OAuthError
|
|
Bugsnag.notify(
|
|
RuntimeError.new("StripeDeauthorizeFailure"),
|
|
stripe_account: stripe_user_id,
|
|
enterprise_id: enterprise_id
|
|
)
|
|
true
|
|
end
|
|
end
|