mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
Lazy load Plaid link tokens, fix link issues on broadcast (#2302)
* Lazy load Plaid link tokens, fix link issues on broadcast * Fix alert styles
This commit is contained in:
parent
c701755b02
commit
d21e385962
19 changed files with 155 additions and 123 deletions
|
@ -5,6 +5,15 @@ module Family::PlaidConnectable
|
|||
has_many :plaid_items, dependent: :destroy
|
||||
end
|
||||
|
||||
def can_connect_plaid_us?
|
||||
plaid(:us).present?
|
||||
end
|
||||
|
||||
# If Plaid provider is configured and user is in the EU region
|
||||
def can_connect_plaid_eu?
|
||||
plaid(:eu).present? && self.eu?
|
||||
end
|
||||
|
||||
def create_plaid_item!(public_token:, item_name:, region:)
|
||||
public_token_response = plaid(region).exchange_public_token(public_token)
|
||||
|
||||
|
@ -34,6 +43,6 @@ module Family::PlaidConnectable
|
|||
|
||||
private
|
||||
def plaid(region)
|
||||
@plaid ||= Provider::Registry.plaid_provider_for_region(region)
|
||||
Provider::Registry.plaid_provider_for_region(region)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,24 +23,22 @@ class PlaidItem < ApplicationRecord
|
|||
scope :needs_update, -> { where(status: :requires_update) }
|
||||
|
||||
def get_update_link_token(webhooks_url:, redirect_url:)
|
||||
begin
|
||||
family.get_link_token(
|
||||
webhooks_url: webhooks_url,
|
||||
redirect_url: redirect_url,
|
||||
region: plaid_region,
|
||||
access_token: access_token
|
||||
)
|
||||
rescue Plaid::ApiError => e
|
||||
error_body = JSON.parse(e.response_body)
|
||||
family.get_link_token(
|
||||
webhooks_url: webhooks_url,
|
||||
redirect_url: redirect_url,
|
||||
region: plaid_region,
|
||||
access_token: access_token
|
||||
)
|
||||
rescue Plaid::ApiError => e
|
||||
error_body = JSON.parse(e.response_body)
|
||||
|
||||
if error_body["error_code"] == "ITEM_NOT_FOUND"
|
||||
# Mark the connection as invalid but don't auto-delete
|
||||
update!(status: :requires_update)
|
||||
raise PlaidConnectionLostError
|
||||
else
|
||||
raise e
|
||||
end
|
||||
if error_body["error_code"] == "ITEM_NOT_FOUND"
|
||||
# Mark the connection as invalid but don't auto-delete
|
||||
update!(status: :requires_update)
|
||||
end
|
||||
|
||||
Sentry.capture_exception(e)
|
||||
nil
|
||||
end
|
||||
|
||||
def destroy_later
|
||||
|
@ -118,6 +116,4 @@ class PlaidItem < ApplicationRecord
|
|||
def supported_products
|
||||
available_products + billed_products
|
||||
end
|
||||
|
||||
class PlaidConnectionLostError < StandardError; end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,7 @@ class PlaidItem::WebhookProcessor
|
|||
@webhook_type = parsed["webhook_type"]
|
||||
@webhook_code = parsed["webhook_code"]
|
||||
@item_id = parsed["item_id"]
|
||||
@error = parsed["error"]
|
||||
end
|
||||
|
||||
def process
|
||||
|
@ -21,6 +22,10 @@ class PlaidItem::WebhookProcessor
|
|||
plaid_item.sync_later
|
||||
when [ "HOLDINGS", "DEFAULT_UPDATE" ]
|
||||
plaid_item.sync_later
|
||||
when [ "ITEM", "ERROR" ]
|
||||
if error["error_code"] == "ITEM_LOGIN_REQUIRED"
|
||||
plaid_item.update!(status: :requires_update)
|
||||
end
|
||||
else
|
||||
Rails.logger.warn("Unhandled Plaid webhook type: #{webhook_type}:#{webhook_code}")
|
||||
end
|
||||
|
@ -30,7 +35,7 @@ class PlaidItem::WebhookProcessor
|
|||
end
|
||||
|
||||
private
|
||||
attr_reader :webhook_type, :webhook_code, :item_id
|
||||
attr_reader :webhook_type, :webhook_code, :item_id, :error
|
||||
|
||||
def plaid_item
|
||||
@plaid_item ||= PlaidItem.find_by(plaid_id: item_id)
|
||||
|
|
|
@ -28,6 +28,14 @@ class Provider::PlaidSandbox < Provider::Plaid
|
|||
)
|
||||
end
|
||||
|
||||
def reset_login(item)
|
||||
client.sandbox_item_reset_login(
|
||||
Plaid::SandboxItemResetLoginRequest.new(
|
||||
access_token: item.access_token
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
def create_client
|
||||
raise "Plaid sandbox is not supported in production" if Rails.env.production?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue