@@ -6,87 +6,93 @@ class SyncLinkageJob < ApplicationJob
66 queue_as :default
77
88 def perform ( linkage )
9+ begin
10+ Rails . logger . info "[#{ linkage . id } ] Begin Linkage Sync"
11+ Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Running'"
12+ linkage . update ( sync_status : :running )
13+
14+ maybe_account = Account . find_by ( id : linkage . maybe_account_id )
15+ Rails . logger . error "[#{ linkage . id } ] Maybe Account: '#{ maybe_account . display_name } ' [#{ maybe_account . identifier } ]; Type: #{ maybe_account . accountable_type } "
16+ simplefin_account = Account . find_by ( id : linkage . simplefin_account_id )
17+ Rails . logger . error "[#{ linkage . id } ] SimpleFIN Account: '#{ simplefin_account . display_name } ' [#{ simplefin_account . identifier } ]"
918
10- Rails . logger . info "[#{ linkage . id } ] Begin Linkage Sync"
11- Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Running'"
12- linkage . update ( sync_status : :running )
13-
14- maybe_account = Account . find_by ( id : linkage . maybe_account_id )
15- Rails . logger . error "[#{ linkage . id } ] Maybe Account: '#{ maybe_account . display_name } ' [#{ maybe_account . identifier } ]; Type: #{ maybe_account . accountable_type } "
16- simplefin_account = Account . find_by ( id : linkage . simplefin_account_id )
17- Rails . logger . error "[#{ linkage . id } ] SimpleFIN Account: '#{ simplefin_account . display_name } ' [#{ simplefin_account . identifier } ]"
18-
19- maybe_client = MaybeClientService . connect
20- if maybe_client . nil?
21- Rails . logger . error "[#{ linkage . id } ] Failed to connect to Maybe PostgreSQL"
22- Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
23- linkage . update ( sync_status : :error , last_sync : Time . current )
24- maybe_client . close
25- return
26- end
19+ maybe_client = MaybeClientService . connect
20+ if maybe_client . nil?
21+ Rails . logger . error "[#{ linkage . id } ] Failed to connect to Maybe PostgreSQL"
22+ Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
23+ linkage . update ( sync_status : :error )
24+ maybe_client . close
25+ return
26+ end
2727
28- username = Setting . find_by ( key : 'simplefin_username' ) &.value
29- password = Setting . find_by ( key : 'simplefin_password' ) &.value
28+ username = Setting . find_by ( key : 'simplefin_username' ) &.value
29+ password = Setting . find_by ( key : 'simplefin_password' ) &.value
3030
31- if username . blank? || password . blank?
32- Rails . logger . error "[#{ linkage . id } ] Missing credentials for SimpleFIN"
33- Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
34- linkage . update ( sync_status : :error , last_sync : Time . current )
35- maybe_client . close
36- return
37- else
38- simplefin_client = SimplefinClient . new ( username , password )
39- end
31+ if username . blank? || password . blank?
32+ Rails . logger . error "[#{ linkage . id } ] Missing credentials for SimpleFIN"
33+ Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
34+ linkage . update ( sync_status : :error )
35+ maybe_client . close
36+ return
37+ else
38+ simplefin_client = SimplefinClient . new ( username , password )
39+ end
4040
41- lookback_days = Setting . find_by ( key : 'lookback_days' ) &.value . to_i
42- lookback_days = 7 if lookback_days . zero? # Fallback to 7 if the value is nil or non-numeric
43- lookback_date = ( Time . now - ( lookback_days * 24 * 60 * 60 ) )
44- start_date = lookback_date . to_i
41+ lookback_days = Setting . find_by ( key : 'lookback_days' ) &.value . to_i
42+ lookback_days = 7 if lookback_days . zero? # Fallback to 7 if the value is nil or non-numeric
43+ lookback_date = ( Time . now - ( lookback_days * 24 * 60 * 60 ) )
44+ start_date = lookback_date . to_i
4545
46- Rails . logger . info "[#{ linkage . id } ] Searching for Maybe transactions since #{ lookback_date . strftime ( "%m/%d/%Y" ) } "
47- transactions_in_maybe = maybe_client . get_simplefin_transactions ( maybe_account . identifier , start_date )
48- Rails . logger . info "[#{ linkage . id } ] Retrieved #{ transactions_in_maybe . length } Maybe transactions"
49- simplefin_response = simplefin_client . get_transactions ( simplefin_account . identifier , start_date )
50- Rails . logger . info "[#{ linkage . id } ] Searching for SimpleFIN transactions since #{ lookback_date . strftime ( "%m/%d/%Y" ) } "
46+ Rails . logger . info "[#{ linkage . id } ] Searching for Maybe transactions since #{ lookback_date . strftime ( "%m/%d/%Y" ) } "
47+ transactions_in_maybe = maybe_client . get_simplefin_transactions ( maybe_account . identifier , start_date )
48+ Rails . logger . info "[#{ linkage . id } ] Retrieved #{ transactions_in_maybe . length } Maybe transactions"
49+ Rails . logger . info "[#{ linkage . id } ] Searching for SimpleFIN transactions since #{ lookback_date . strftime ( "%m/%d/%Y" ) } "
50+ simplefin_response = simplefin_client . get_transactions ( simplefin_account . identifier , start_date )
51+
52+ if simplefin_response [ :success ]
53+ simplefin_account_with_balance = simplefin_response [ :response ] . dig ( "accounts" ) . first
54+ simplefin_transactions = simplefin_response [ :response ] . dig ( "accounts" ) . first &.dig ( "transactions" ) || [ ]
55+ Rails . logger . info "[#{ linkage . id } ] Retrieved #{ simplefin_transactions . length } SimpleFIN transactions"
56+
57+ simplefin_transactions . each do |simplefin_transaction |
58+ transaction_id = simplefin_transaction . dig ( "id" )
5159
52- if simplefin_response [ :success ]
53- simplefin_account_with_balance = simplefin_response [ :response ] . dig ( "accounts" ) . first
54- simplefin_transactions = simplefin_response [ :response ] . dig ( "accounts" ) . first &.dig ( "transactions" ) || [ ]
55- Rails . logger . info "[#{ linkage . id } ] Retrieved #{ simplefin_transactions . length } SimpleFIN transactions"
56-
57- simplefin_transactions . each do |simplefin_transaction |
58- transaction_id = simplefin_transaction . dig ( "id" )
59-
60- # If this transaction hasn't been synced yet, create a new transaction in Maybe
61- unless transactions_in_maybe . any? { |t | t [ "plaid_id" ] == transaction_id }
60+ # If this transaction hasn't been synced yet, create a new transaction in Maybe
61+ unless transactions_in_maybe . any? { |t | t [ "plaid_id" ] == transaction_id }
6262
63- if maybe_account . accountable_type == "Investment" # Investment
64- description = simplefin_transaction . dig ( "description" )
65- if description . match? ( /(CONTRIBUTIONS|INTEREST PAYMENT|AUTO CLEARING HOUSE FUND)/i ) && simplefin_transaction . dig ( "amount" ) . to_f > 0 #todo: make these regex settings
66- Rails . logger . info "[#{ linkage . id } ] Adding transaction with plaid_id='#{ transaction_id } '"
67- maybe_client . new_transaction ( maybe_account . identifier , simplefin_transaction , simplefin_account . currency )
68- elsif description . match? ( /(RECORDKEEPING|MANAGEMENT|WRAP) FEE/i ) && simplefin_transaction . dig ( "amount" ) . to_f < 0 #todo: make these regex settings
63+ if maybe_account . accountable_type == "Investment" # Investment
64+ description = simplefin_transaction . dig ( "description" )
65+ if description . match? ( /(CONTRIBUTIONS|INTEREST PAYMENT|AUTO CLEARING HOUSE FUND)/i ) && simplefin_transaction . dig ( "amount" ) . to_f > 0 #todo: make these regex settings
66+ Rails . logger . info "[#{ linkage . id } ] Adding transaction with plaid_id='#{ transaction_id } '"
67+ maybe_client . new_transaction ( maybe_account . identifier , simplefin_transaction , simplefin_account . currency )
68+ elsif description . match? ( /(RECORDKEEPING|MANAGEMENT|WRAP) FEE/i ) && simplefin_transaction . dig ( "amount" ) . to_f < 0 #todo: make these regex settings
69+ Rails . logger . info "[#{ linkage . id } ] Adding transaction with plaid_id='#{ transaction_id } '"
70+ maybe_client . new_transaction ( maybe_account . identifier , simplefin_transaction , simplefin_account . currency )
71+ end
72+ else # CreditCard,Despository,Loan
6973 Rails . logger . info "[#{ linkage . id } ] Adding transaction with plaid_id='#{ transaction_id } '"
7074 maybe_client . new_transaction ( maybe_account . identifier , simplefin_transaction , simplefin_account . currency )
7175 end
72- else # CreditCard,Despository,Loan
73- Rails . logger . info "[#{ linkage . id } ] Adding transaction with plaid_id='#{ transaction_id } '"
74- maybe_client . new_transaction ( maybe_account . identifier , simplefin_transaction , simplefin_account . currency )
7576 end
7677 end
78+ if maybe_account . accountable_type == "Investment" # Investment
79+ Rails . logger . info "[#{ linkage . id } ] Updating Balance for '#{ Time . at ( simplefin_account_with_balance . dig ( "balance-date" ) ) . strftime ( "%m/%d/%Y" ) } '"
80+ maybe_client . upsert_account_valuation ( maybe_account . identifier , simplefin_account_with_balance )
81+ end
82+ else
83+ Rails . logger . error "[#{ linkage . id } ] Failed to retrieve data from SimpleFIN"
84+ Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
85+ linkage . update ( sync_status : :error )
7786 end
78- if maybe_account . accountable_type == "Investment" # Investment
79- Rails . logger . info "[#{ linkage . id } ] Updating Balance for '#{ Time . at ( simplefin_account_with_balance . dig ( "balance-date" ) ) . strftime ( "%m/%d/%Y" ) } '"
80- maybe_client . upsert_account_valuation ( maybe_account . identifier , simplefin_account_with_balance )
81- end
82- else
83- Rails . logger . error "[#{ linkage . id } ] Failed to retrieve data from SimpleFIN"
87+ Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Complete'"
88+ linkage . update ( sync_status : :complete , last_sync : Time . current )
89+ maybe_client . close
90+ rescue Net ::ReadTimeout => e
91+ Rails . logger . error "[#{ linkage . id } ] Failed to retrieve data from SimpleFIN: #{ e . message } "
8492 Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Error'"
85- linkage . update ( sync_status : :error , last_sync : Time . current )
93+ linkage . update ( sync_status : :error )
94+ maybe_client . close
8695 end
87- Rails . logger . info "[#{ linkage . id } ] Setting Linkage Status to 'Complete'"
88- linkage . update ( sync_status : :complete , last_sync : Time . current )
89- maybe_client . close
9096 end
9197end
9298
0 commit comments