mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-27 17:19:39 +02:00
Add context to plaid sync errors
This commit is contained in:
parent
d73e7eacce
commit
9ec94cd1fa
3 changed files with 21 additions and 4 deletions
|
@ -28,9 +28,13 @@ class Family < ApplicationRecord
|
||||||
account.sync_data(start_date: start_date)
|
account.sync_data(start_date: start_date)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
plaid_data = []
|
||||||
|
|
||||||
plaid_items.each do |plaid_item|
|
plaid_items.each do |plaid_item|
|
||||||
plaid_item.sync_data(start_date: start_date)
|
plaid_data << plaid_item.sync_data(start_date: start_date)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
plaid_data
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_sync
|
def post_sync
|
||||||
|
|
|
@ -35,11 +35,13 @@ class PlaidItem < ApplicationRecord
|
||||||
def sync_data(start_date: nil)
|
def sync_data(start_date: nil)
|
||||||
update!(last_synced_at: Time.current)
|
update!(last_synced_at: Time.current)
|
||||||
|
|
||||||
fetch_and_load_plaid_data
|
plaid_data = fetch_and_load_plaid_data
|
||||||
|
|
||||||
accounts.each do |account|
|
accounts.each do |account|
|
||||||
account.sync_data(start_date: start_date)
|
account.sync_data(start_date: start_date)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
plaid_data
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_sync
|
def post_sync
|
||||||
|
@ -53,10 +55,12 @@ class PlaidItem < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
def fetch_and_load_plaid_data
|
def fetch_and_load_plaid_data
|
||||||
|
data = {}
|
||||||
item = plaid_provider.get_item(access_token).item
|
item = plaid_provider.get_item(access_token).item
|
||||||
update!(available_products: item.available_products, billed_products: item.billed_products)
|
update!(available_products: item.available_products, billed_products: item.billed_products)
|
||||||
|
|
||||||
fetched_accounts = plaid_provider.get_item_accounts(self).accounts
|
fetched_accounts = plaid_provider.get_item_accounts(self).accounts
|
||||||
|
data[:accounts] = fetched_accounts || []
|
||||||
|
|
||||||
internal_plaid_accounts = fetched_accounts.map do |account|
|
internal_plaid_accounts = fetched_accounts.map do |account|
|
||||||
internal_plaid_account = plaid_accounts.find_or_create_from_plaid_data!(account, family)
|
internal_plaid_account = plaid_accounts.find_or_create_from_plaid_data!(account, family)
|
||||||
|
@ -65,6 +69,7 @@ class PlaidItem < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
fetched_transactions = safe_fetch_plaid_data(:get_item_transactions)
|
fetched_transactions = safe_fetch_plaid_data(:get_item_transactions)
|
||||||
|
data[:transactions] = fetched_transactions || []
|
||||||
|
|
||||||
if fetched_transactions
|
if fetched_transactions
|
||||||
transaction do
|
transaction do
|
||||||
|
@ -81,6 +86,7 @@ class PlaidItem < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
fetched_investments = safe_fetch_plaid_data(:get_item_investments)
|
fetched_investments = safe_fetch_plaid_data(:get_item_investments)
|
||||||
|
data[:investments] = fetched_investments || []
|
||||||
|
|
||||||
if fetched_investments
|
if fetched_investments
|
||||||
transaction do
|
transaction do
|
||||||
|
@ -95,6 +101,7 @@ class PlaidItem < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
fetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities)
|
fetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities)
|
||||||
|
data[:liabilities] = fetched_liabilities || []
|
||||||
|
|
||||||
if fetched_liabilities
|
if fetched_liabilities
|
||||||
transaction do
|
transaction do
|
||||||
|
@ -109,6 +116,8 @@ class PlaidItem < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
def safe_fetch_plaid_data(method)
|
def safe_fetch_plaid_data(method)
|
||||||
|
|
|
@ -9,7 +9,8 @@ class Sync < ApplicationRecord
|
||||||
start!
|
start!
|
||||||
|
|
||||||
begin
|
begin
|
||||||
syncable.sync_data(start_date: start_date)
|
data = syncable.sync_data(start_date: start_date)
|
||||||
|
update!(data: data) if data
|
||||||
complete!
|
complete!
|
||||||
rescue StandardError => error
|
rescue StandardError => error
|
||||||
fail! error
|
fail! error
|
||||||
|
@ -29,7 +30,10 @@ class Sync < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def fail!(error)
|
def fail!(error)
|
||||||
Sentry.capture_exception(error)
|
Sentry.capture_exception(error) do |scope|
|
||||||
|
scope.set_context("sync", { id: id })
|
||||||
|
end
|
||||||
|
|
||||||
update! status: :failed, error: error.message, last_ran_at: Time.current
|
update! status: :failed, error: error.message, last_ran_at: Time.current
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue