mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 06:55:21 +02:00
fix: Various account issues with balances
This commit is contained in:
parent
175d3dc753
commit
8d22d46420
4 changed files with 45 additions and 18 deletions
|
@ -13,7 +13,6 @@ class Provider::Registry
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_provider(name)
|
def get_provider(name)
|
||||||
puts "NAME: #{name}"
|
|
||||||
send(name)
|
send(name)
|
||||||
rescue NoMethodError
|
rescue NoMethodError
|
||||||
raise Error.new("Provider '#{name}' not found in registry")
|
raise Error.new("Provider '#{name}' not found in registry")
|
||||||
|
|
|
@ -84,19 +84,27 @@ class Provider::SimpleFin
|
||||||
# For more spec information, see: https://www.simplefin.org/protocol.html#setup-token
|
# For more spec information, see: https://www.simplefin.org/protocol.html#setup-token
|
||||||
#
|
#
|
||||||
# @param [str] accountable_type The name of the account type we're looking for.
|
# @param [str] accountable_type The name of the account type we're looking for.
|
||||||
# @param [int?] trans_start_date A linux epoch of the start date to get transactions of.
|
# @param [int?] trans_start_date A linux epoch of the start date to get transactions of. Default is based on the config
|
||||||
# @param [int?] trans_end_date A linux epoch of the end date to get transactions between.
|
# @param [int?] trans_end_date A linux epoch of the end date to get transactions between. Default is now.
|
||||||
# @param [Boolean] trans_pending If we should include pending transactions. Default is true.
|
# @param [Boolean] trans_pending If we should include pending transactions. Default is false as I don't think maybe supports pending.
|
||||||
def get_available_accounts(accountable_type, trans_start_date = nil, trans_end_date = nil, trans_pending = true)
|
def get_available_accounts(accountable_type, trans_start_date = nil, trans_end_date = nil, trans_pending = false)
|
||||||
check_rate_limit
|
check_rate_limit
|
||||||
endpoint = "/accounts?pending=#{trans_pending}"
|
endpoint = "/accounts?pending=#{trans_pending}"
|
||||||
|
|
||||||
|
if trans_end_date == nil
|
||||||
|
trans_end_date = Time.now.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
if trans_start_date == nil
|
||||||
|
trans_start_date = Provider::SimpleFin.provider_config.look_back_days.days.ago.to_i
|
||||||
|
end
|
||||||
|
|
||||||
# Add any parameters we care about
|
# Add any parameters we care about
|
||||||
if trans_start_date
|
if trans_start_date
|
||||||
endpoint += "&trans_start_date=#{trans_start_date}"
|
endpoint += "&start-date=#{trans_start_date}"
|
||||||
end
|
end
|
||||||
if trans_end_date
|
if trans_end_date
|
||||||
endpoint += "&trans_end_date=#{trans_end_date}"
|
endpoint += "&end-date=#{trans_end_date}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# account_info = send_request_to_sf(endpoint)
|
# account_info = send_request_to_sf(endpoint)
|
||||||
|
|
|
@ -19,31 +19,41 @@ class SimpleFinAccount < ApplicationRecord
|
||||||
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
# Gets what balance we should use as our account balance
|
||||||
|
def get_adjusted_balance(sf_account_data)
|
||||||
|
balance_from_sf = sf_account_data["balance"].to_d
|
||||||
|
account_type = sf_account_data["type"]
|
||||||
|
# Adjust balance: liabilities (CreditCard, Loan) should be negative
|
||||||
|
if [ "CreditCard", "Loan" ].include?(account_type)
|
||||||
|
balance_from_sf * -1
|
||||||
|
else
|
||||||
|
balance_from_sf
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def find_or_create_from_simple_fin_data!(sf_account_data, sfc)
|
def find_or_create_from_simple_fin_data!(sf_account_data, sfc)
|
||||||
sfc.simple_fin_accounts.find_or_create_by!(external_id: sf_account_data["id"]) do |sfa|
|
sfc.simple_fin_accounts.find_or_create_by!(external_id: sf_account_data["id"]) do |sfa|
|
||||||
sfa.current_balance = sf_account_data["balance"].to_d
|
balance = get_adjusted_balance(sf_account_data)
|
||||||
sfa.available_balance = sf_account_data["balance"].to_d
|
sfa.current_balance = balance
|
||||||
|
sfa.available_balance = balance
|
||||||
sfa.currency = sf_account_data["currency"]
|
sfa.currency = sf_account_data["currency"]
|
||||||
|
|
||||||
new_account = sfc.family.accounts.new(
|
new_account = sfc.family.accounts.new(
|
||||||
name: sf_account_data["name"],
|
name: sf_account_data["name"],
|
||||||
balance: sf_account_data["balance"].to_d,
|
balance: 0,
|
||||||
currency: sf_account_data["currency"],
|
currency: sf_account_data["currency"],
|
||||||
accountable: TYPE_MAPPING[sf_account_data["type"]].new,
|
accountable: TYPE_MAPPING[sf_account_data["type"]].new,
|
||||||
subtype: sf_account_data["subtype"],
|
subtype: sf_account_data["subtype"],
|
||||||
simple_fin_account: sfa, # Explicitly associate back
|
simple_fin_account: sfa, # Explicitly associate back
|
||||||
last_synced_at: Time.current, # Mark as synced upon creation
|
last_synced_at: Time.current, # Mark as synced upon creation
|
||||||
# Set cash_balance similar to how Account.create_and_sync might
|
# Set cash_balance similar to how Account.create_and_sync might
|
||||||
cash_balance: sf_account_data["balance"].to_d
|
cash_balance: 0
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add initial valuations
|
|
||||||
current_balance_amount = new_account.balance
|
|
||||||
|
|
||||||
new_account.entries.build(
|
new_account.entries.build(
|
||||||
name: "Current Balance",
|
name: "Current Balance",
|
||||||
date: Date.current,
|
date: Date.current,
|
||||||
amount: current_balance_amount,
|
amount: balance,
|
||||||
currency: new_account.currency,
|
currency: new_account.currency,
|
||||||
entryable: Valuation.new
|
entryable: Valuation.new
|
||||||
)
|
)
|
||||||
|
@ -72,13 +82,14 @@ class SimpleFinAccount < ApplicationRecord
|
||||||
# Syncs all account data for the given sf_account_data parameter
|
# Syncs all account data for the given sf_account_data parameter
|
||||||
def sync_account_data!(sf_account_data)
|
def sync_account_data!(sf_account_data)
|
||||||
accountable_attributes = { id: self.account.accountable_id }
|
accountable_attributes = { id: self.account.accountable_id }
|
||||||
|
balance = SimpleFinAccount.get_adjusted_balance(sf_account_data)
|
||||||
self.update!(
|
self.update!(
|
||||||
current_balance: sf_account_data["balance"].to_d,
|
current_balance: balance,
|
||||||
available_balance: sf_account_data["available-balance"]&.to_d,
|
available_balance: sf_account_data["available-balance"]&.to_d,
|
||||||
currency: sf_account_data["currency"],
|
currency: sf_account_data["currency"],
|
||||||
account_attributes: {
|
account_attributes: {
|
||||||
id: self.account.id,
|
id: self.account.id,
|
||||||
balance: sf_account_data["balance"].to_d,
|
balance: balance,
|
||||||
last_synced_at: Time.current,
|
last_synced_at: Time.current,
|
||||||
accountable_attributes: accountable_attributes
|
accountable_attributes: accountable_attributes
|
||||||
}
|
}
|
||||||
|
@ -159,8 +170,17 @@ class SimpleFinAccount < ApplicationRecord
|
||||||
entry.entryable = Transaction.new
|
entry.entryable = Transaction.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
entry.entryable.simple_fin_category = transaction_data.dig("extra", "category") if entry.entryable.respond_to?(:simple_fin_category=)
|
entry.entryable.simple_fin_category = transaction_data.dig("extra", "category") if entry.entryable.respond_to?(:simple_fin_category=)
|
||||||
|
|
||||||
|
# Auto associate a category to our transaction
|
||||||
|
if entry.entryable.simple_fin_category.present?
|
||||||
|
category_name = entry.entryable.simple_fin_category
|
||||||
|
category = self.account.family.categories.find_or_create_by!(name: category_name)
|
||||||
|
entry.entryable.category = category
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if entry.changed? || entry.entryable.changed? # Check if entryable also changed
|
if entry.changed? || entry.entryable.changed? # Check if entryable also changed
|
||||||
entry.save!
|
entry.save!
|
||||||
else
|
else
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<div class="bg-container p-5 shadow-border-xs rounded-xl" data-controller="focus-record" data-focus-record-id-value="<%= @focused_record ? dom_id(@focused_record) : nil %>">
|
<div class="bg-container p-5 shadow-border-xs rounded-xl" data-controller="focus-record" data-focus-record-id-value="<%= @focused_record ? dom_id(@focused_record) : nil %>">
|
||||||
<div class="flex items-center justify-between mb-4" data-testid="activity-menu">
|
<div class="flex items-center justify-between mb-4" data-testid="activity-menu">
|
||||||
<%= tag.h2 t(".title"), class: "font-medium text-lg" %>
|
<%= tag.h2 t(".title"), class: "font-medium text-lg" %>
|
||||||
<% unless @account.plaid_account_id.present? %>
|
<% unless (@account.plaid_account_id.present? || @account.simple_fin_account.present?) %>
|
||||||
<%= render MenuComponent.new(variant: "button") do |menu| %>
|
<%= render MenuComponent.new(variant: "button") do |menu| %>
|
||||||
<% menu.with_button(text: "New", variant: "secondary", icon: "plus") %>
|
<% menu.with_button(text: "New", variant: "secondary", icon: "plus") %>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue