Files
sure/app/models/akahu_account/processor.rb
Brad 1b8b21760b feat(provider): Akahu integration (#1921)
* First pass of Akahu

* fix up sync all

* conflicts

* fix db migration issue? - fix auto selection of akahu account type

* Address Akahu PR feedback

* Complete provider metadata

* Fix PR 1921 CI tests

* PR feedback

* PR feedback

* post merge

---------

Co-authored-by: failing <failing@users.noreply.github.com>
Co-authored-by: Juan José Mata <jjmata@jjmata.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-06-02 21:44:57 +02:00

75 lines
2.3 KiB
Ruby

class AkahuAccount::Processor
include CurrencyNormalizable
SanitizedProcessingError = Class.new(StandardError)
attr_reader :akahu_account
def initialize(akahu_account)
@akahu_account = akahu_account
end
def process
unless akahu_account.current_account.present?
Rails.logger.info "AkahuAccount::Processor - No linked account for akahu_account #{akahu_account.id}, skipping processing"
return
end
process_account!
process_transactions
rescue StandardError => e
Rails.logger.error "AkahuAccount::Processor - Failed to process account akahu_account_id=#{akahu_account.id} error_class=#{e.class.name}"
report_exception(e, "account")
raise
end
private
def process_account!
account = akahu_account.current_account
balance = akahu_account.current_balance || 0
balance = balance.abs if account.accountable_type.in?(%w[CreditCard Loan])
cash_balance = account.accountable_type == "Investment" ? 0 : balance
currency = parse_currency(akahu_account.currency) || account.currency || "NZD"
account.update!(
balance: balance,
cash_balance: cash_balance,
currency: currency
)
end
def process_transactions
AkahuAccount::Transactions::Processor.new(akahu_account).process
rescue => e
report_exception(e, "transactions")
Rails.logger.error "AkahuAccount::Processor - Failed to process transactions akahu_account_id=#{akahu_account.id} error_class=#{e.class.name}"
{ success: false, failed: 1, errors: [ { error: I18n.t("akahu_item.errors.account_processing_failed") } ] }
end
def report_exception(error, context)
safe_error = SanitizedProcessingError.new("Akahu account processing failed")
Sentry.capture_exception(safe_error) do |scope|
scope.set_tags(
akahu_account_id: akahu_account.id,
context: context,
error_class: error.class.name
)
scope.set_context(
"akahu_account_processor",
{
akahu_account_id: akahu_account.id,
context: context,
error_class: error.class.name
}
)
end
end
def log_invalid_currency(currency_value)
Rails.logger.warn("Invalid currency code '#{currency_value}' for Akahu account #{akahu_account.id}, falling back to account currency")
end
end