1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-10 16:05:22 +02:00

Continue to process holdings even if one is not resolvable

This commit is contained in:
Zach Gollwitzer 2025-06-26 16:43:15 -04:00
parent c5b45d5ccf
commit a378c6ad79
2 changed files with 52 additions and 7 deletions

View file

@ -8,7 +8,7 @@ class PlaidAccount::Investments::HoldingsProcessor
holdings.each do |plaid_holding|
resolved_security_result = security_resolver.resolve(plaid_security_id: plaid_holding["security_id"])
return unless resolved_security_result.security.present?
next unless resolved_security_result.security.present?
security = resolved_security_result.security
holding_date = plaid_holding["institution_price_as_of"] || Date.current
@ -25,13 +25,15 @@ class PlaidAccount::Investments::HoldingsProcessor
amount: plaid_holding["quantity"] * plaid_holding["institution_price"]
)
holding.save!
ActiveRecord::Base.transaction do
holding.save!
# Delete all holdings for this security after the institution price date
account.holdings
.where(security: security)
.where("date > ?", holding_date)
.destroy_all
# Delete all holdings for this security after the institution price date
account.holdings
.where(security: security)
.where("date > ?", holding_date)
.destroy_all
end
end
end

View file

@ -151,4 +151,47 @@ class PlaidAccount::Investments::HoldingsProcessorTest < ActiveSupport::TestCase
assert account.holdings.exists?(security: third_security, date: 1.day.ago.to_date, qty: 75)
assert account.holdings.exists?(security: securities(:aapl), date: 1.day.ago.to_date, qty: 100)
end
test "continues processing other holdings when security resolution fails" do
test_investments_payload = {
securities: [],
holdings: [
{
"security_id" => "fail",
"quantity" => 100,
"institution_price" => 100,
"iso_currency_code" => "USD"
},
{
"security_id" => "success",
"quantity" => 200,
"institution_price" => 200,
"iso_currency_code" => "USD"
}
],
transactions: []
}
@plaid_account.update!(raw_investments_payload: test_investments_payload)
# First security fails to resolve
@security_resolver.expects(:resolve)
.with(plaid_security_id: "fail")
.returns(OpenStruct.new(security: nil))
# Second security succeeds
@security_resolver.expects(:resolve)
.with(plaid_security_id: "success")
.returns(OpenStruct.new(security: securities(:aapl)))
processor = PlaidAccount::Investments::HoldingsProcessor.new(@plaid_account, security_resolver: @security_resolver)
# Should create only 1 holding (the successful one)
assert_difference "Holding.count", 1 do
processor.process
end
# Should have created the successful holding
assert @plaid_account.account.holdings.exists?(security: securities(:aapl), qty: 200)
end
end