diff --git a/app/models/account.rb b/app/models/account.rb index 4984fb89..a610171d 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -73,6 +73,12 @@ class Account < ApplicationRecord end end + def set_name(name) + if enrichable?(:name) + self.name = name + end + end + def institution_domain url_string = plaid_account&.plaid_item&.institution_url return nil unless url_string.present? diff --git a/app/models/plaid_account/processor.rb b/app/models/plaid_account/processor.rb new file mode 100644 index 00000000..31c8b8d3 --- /dev/null +++ b/app/models/plaid_account/processor.rb @@ -0,0 +1,22 @@ +class PlaidAccount::Processor + attr_reader :plaid_account + + def initialize(plaid_account) + @plaid_account = plaid_account + end + + def process + PlaidAccount.transaction do + account = family.accounts.find_or_initialize_by( + plaid_account_id: plaid_account.id + ) + + account.set_name(plaid_account.name) + end + end + + private + def family + plaid_account.plaid_item.family + end +end \ No newline at end of file diff --git a/app/models/plaid_item/processor.rb b/app/models/plaid_item/processor.rb deleted file mode 100644 index a236c3df..00000000 --- a/app/models/plaid_item/processor.rb +++ /dev/null @@ -1,8 +0,0 @@ -class PlaidItem::Processor - def initialize(plaid_item) - @plaid_item = plaid_item - end - - def process_data - end -end diff --git a/app/models/plaid_item/syncer.rb b/app/models/plaid_item/syncer.rb index f66a42cf..92148a94 100644 --- a/app/models/plaid_item/syncer.rb +++ b/app/models/plaid_item/syncer.rb @@ -10,7 +10,9 @@ class PlaidItem::Syncer fetch_and_import_item_data # Processes the raw Plaid data and updates internal domain objects - process_item_data + plaid_item.plaid_accounts.each do |plaid_account| + PlaidAccount::Processor.new(plaid_account).process + end # All data is synced, so we can now run an account sync to calculate historical balances and more plaid_item.reload.accounts.each do |account| @@ -35,10 +37,6 @@ class PlaidItem::Syncer PlaidItem::Importer.new(plaid_item, plaid_provider: plaid_provider).import end - def process_item_data - PlaidItem::Processor.new(plaid_item).process_data - end - def safe_fetch_plaid_data(method) begin plaid.send(method, plaid_item)