mirror of
https://github.com/we-promise/sure.git
synced 2026-07-12 12:55:20 +00:00
* feat(up): add Up Bank (AU) provider integration Adds Up Bank as a per-family, token-based bank sync provider, modelled on the existing Akahu integration. Up uses a JSON:API REST API with a personal access token (Bearer), cursor pagination via links.next, and returns both HELD (pending) and SETTLED transactions from one endpoint. New: - Provider::Up client (JSON:API unwrap, links.next pagination, retries, typed errors, /util/ping) + Provider::UpAdapter (Factory-registered, Depository + Loan). - UpItem / UpAccount models with Provided, Unlinking, Syncer, SyncCompleteEvent, Importer, Processor, Transactions::Processor, and UpEntry::Processor (amount sign flip, HELD->pending, foreignAmount FX, merchant from description, stale-pending pruning). - Family::UpConnectable, UpItemsController, routes, settings panel + connect flow views, accounts index wiring, initializer, en locale, and model tests. Core wiring: - "up" added to Transaction::PENDING_PROVIDERS, the three pending-match SQL blocks in Account::ProviderImportAdapter, Provider::Metadata::REGISTRY, ProviderMerchant/DataEnrichment source enums, ProviderConnectionStatus, settings provider panels, and financial data reset. Migration create_up_items_and_accounts must be run before use. No external API endpoints added (no OpenAPI changes). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(up): dump up tables to schema and make since filter TZ-safe The feature commit added the up_items/up_accounts migration but never re-dumped db/schema.rb, leaving the schema version and tables stale. Add the two table definitions and foreign keys and bump the schema version so a fresh DB load matches the migration. Also format a bare Date `since` as UTC midnight instead of the server's local zone, so `filter[since]` is deterministic regardless of where the app runs (previously shifted by the local UTC offset). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(up): address code review feedback Behavior/correctness: - Persist skipped accounts via a new up_accounts.ignored flag and a needs_setup scope, so skipped accounts stop resurfacing as "needs setup" on every sync. Linking clears the flag. - destroy now checks unlink_all! per-account results and aborts deletion (alert) if any unlink failed, instead of swallowing failures. - render_provider_panel_error redirect uses :see_other (was an invalid 4xx redirect status). - Up provider adapter falls back to item institution name/url when institution_metadata is absent (early return previously blocked it). Resilience/security: - fetch_all_resources guards against an API repeating the same links.next cursor (Set#add?), preventing infinite pagination. - HTTP client validates absolute URLs (from links.next) against Up's HTTPS host before sending the bearer token, preventing credential leakage to untrusted hosts. Diagnostics: - Route provider sync/import failures through DebugLogEntry.capture (controller, UpItem, syncer, unlinking) with family/account context. Low-level HTTP client and currency-normalization warnings keep Rails.logger to match existing provider conventions. Data integrity: - up_accounts.name and currency are NOT NULL (align with model presence validations); account_id stays nullable (allow_nil uniqueness). Forms: - select_existing_account radio is required; controller guards a blank/ unknown up_account_id with a friendly alert instead of RecordNotFound. Tests: - Add UpAccount needs_setup scope test, pagination loop guard test, untrusted-host rejection test; tighten filter[since] assertion to the exact UTC timestamp. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(up): address second-round review feedback - Capture sync/import failures via DebugLogEntry so swallowed errors in account/transaction fetching and transaction processing surface in /settings/debug instead of only Rails.logger. - Gate UP_DEBUG_RAW raw payload dump to local envs to avoid leaking PII (merchant names, amounts, account IDs) in managed/production logs. - Collapse linked/unlinked/total account counts into one memoized query instead of 3 separate COUNTs per rendered item. - Rename "Set Up Up Accounts" locale title to "Link Up Accounts". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * docs(up): add method docstrings and align failed_result keys Add docstrings to all Up provider source files (controller, models, providers, concerns) to satisfy the 80% docstring coverage threshold. Third-round review: failed_result now mirrors import's result shape (accounts_updated/created/failed, transactions_imported/failed) instead of the stale accounts_imported key, so failure results stay consistent. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
200 lines
6.6 KiB
Ruby
200 lines
6.6 KiB
Ruby
require "digest/md5"
|
|
|
|
class UpEntry::Processor
|
|
include CurrencyNormalizable
|
|
|
|
# Stable external id for a transaction: its Up id when present, else a content
|
|
# hash so pending entries lacking an id stay deduplicated across syncs.
|
|
def self.canonical_external_id(up_transaction)
|
|
data = up_transaction.with_indifferent_access
|
|
id = data[:id].presence
|
|
return "up_#{id}" if id.present?
|
|
|
|
"up_pending_#{content_hash_for(data)}"
|
|
end
|
|
|
|
# Up marks unsettled transactions with status "HELD"; settled ones are "SETTLED".
|
|
def self.pending?(up_transaction)
|
|
data = up_transaction.with_indifferent_access
|
|
data[:status].to_s.upcase == "HELD"
|
|
end
|
|
|
|
# MD5 of account/date/amount/description, used to identify id-less pendings.
|
|
def self.content_hash_for(data)
|
|
amount = data[:amount].is_a?(Hash) ? data[:amount].with_indifferent_access : {}
|
|
attributes = [
|
|
data[:account_id],
|
|
data[:createdAt],
|
|
amount[:value],
|
|
data[:description]
|
|
].compact.join("|")
|
|
|
|
Digest::MD5.hexdigest(attributes)
|
|
end
|
|
|
|
# Build a processor for a single raw Up transaction tied to +up_account+.
|
|
def initialize(up_transaction, up_account:)
|
|
@up_transaction = up_transaction
|
|
@up_account = up_account
|
|
end
|
|
|
|
# Import the transaction into the linked Sure account via the import adapter.
|
|
# Returns nil when the account isn't linked; re-raises on validation/save errors.
|
|
def process
|
|
unless account.present?
|
|
Rails.logger.warn "UpEntry::Processor - No linked account for up_account #{up_account.id}, skipping transaction #{external_id}"
|
|
return nil
|
|
end
|
|
|
|
import_adapter.import_transaction(
|
|
external_id: external_id,
|
|
amount: amount,
|
|
currency: currency,
|
|
date: date,
|
|
name: name,
|
|
source: "up",
|
|
merchant: merchant,
|
|
notes: notes,
|
|
extra: extra_metadata
|
|
)
|
|
rescue ArgumentError => e
|
|
Rails.logger.error "UpEntry::Processor - Validation error for transaction #{external_id}: #{e.message}"
|
|
raise
|
|
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
|
|
Rails.logger.error "UpEntry::Processor - Failed to save transaction #{external_id}: #{e.message}"
|
|
raise StandardError.new("Failed to import transaction: #{e.message}")
|
|
rescue => e
|
|
Rails.logger.error "UpEntry::Processor - Unexpected error processing transaction #{external_id}: #{e.class} - #{e.message}"
|
|
Rails.logger.error e.backtrace.join("\n")
|
|
raise StandardError.new("Unexpected error importing transaction: #{e.message}")
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :up_transaction, :up_account
|
|
|
|
# Memoized adapter that writes provider transactions into the Sure account.
|
|
def import_adapter
|
|
@import_adapter ||= Account::ProviderImportAdapter.new(account)
|
|
end
|
|
|
|
# The linked Sure account for this transaction, if any.
|
|
def account
|
|
@account ||= up_account.current_account
|
|
end
|
|
|
|
# The raw transaction as an indifferent-access hash.
|
|
def data
|
|
@data ||= up_transaction.with_indifferent_access
|
|
end
|
|
|
|
# Canonical external id for this transaction (see .canonical_external_id).
|
|
def external_id
|
|
@external_id ||= self.class.canonical_external_id(data)
|
|
end
|
|
|
|
# Display name: the Up description, or a generic fallback.
|
|
def name
|
|
data[:description].presence || I18n.t("transactions.unknown_name")
|
|
end
|
|
|
|
# Optional user-entered message attached to the transaction.
|
|
def notes
|
|
data[:message].presence
|
|
end
|
|
|
|
# Find or create the merchant for this transaction's description, or nil.
|
|
def merchant
|
|
merchant_name = data[:description].to_s.strip.presence
|
|
return nil unless merchant_name
|
|
|
|
provider_merchant_id = "up_merchant_#{Digest::MD5.hexdigest(merchant_name.downcase)}"
|
|
|
|
@merchant ||= import_adapter.find_or_create_merchant(
|
|
provider_merchant_id: provider_merchant_id,
|
|
name: merchant_name,
|
|
source: "up"
|
|
)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
Rails.logger.error "UpEntry::Processor - Failed to create merchant '#{merchant_name}': #{e.message}"
|
|
nil
|
|
end
|
|
|
|
# Up amounts use banking convention: negative is money out, positive is money in.
|
|
# Sure stores expenses as positive and income as negative, so the sign is flipped.
|
|
def amount
|
|
raw_value = amount_data[:value]
|
|
parsed_amount = case raw_value
|
|
when String
|
|
BigDecimal(raw_value)
|
|
when Numeric
|
|
BigDecimal(raw_value.to_s)
|
|
else
|
|
BigDecimal("0")
|
|
end
|
|
|
|
-parsed_amount
|
|
rescue ArgumentError => e
|
|
Rails.logger.error "Failed to parse Up transaction amount: #{e.class}"
|
|
raise ArgumentError, "Invalid transaction amount"
|
|
end
|
|
|
|
# Transaction currency, falling back to the account/default currency.
|
|
def currency
|
|
parse_currency(amount_data[:currencyCode]) || up_account.currency || account&.currency || "AUD"
|
|
end
|
|
|
|
# Settlement date (or creation date for pendings) as a Date.
|
|
def date
|
|
value = data[:settledAt].presence || data[:createdAt].presence
|
|
case value
|
|
when String
|
|
Time.parse(value).to_date
|
|
when Time, DateTime
|
|
value.to_date
|
|
when Date
|
|
value
|
|
else
|
|
Rails.logger.error("Up transaction has no usable date value")
|
|
raise ArgumentError, "Invalid date format"
|
|
end
|
|
rescue ArgumentError, TypeError => e
|
|
Rails.logger.error("Failed to parse Up transaction date: #{e.class}")
|
|
raise ArgumentError, "Unable to parse transaction date"
|
|
end
|
|
|
|
# Provider metadata persisted on Transaction#extra (pending/status/fx/etc.).
|
|
def extra_metadata
|
|
{
|
|
"up" => {
|
|
"pending" => pending?,
|
|
"status" => data[:status],
|
|
"category_id" => data[:category_id],
|
|
"raw_text" => data[:rawText],
|
|
"fx_from" => foreign_amount_data[:currencyCode],
|
|
"fx_amount" => foreign_amount_data[:value]
|
|
}.compact
|
|
}
|
|
end
|
|
|
|
# Whether this transaction is still HELD (unsettled) on Up.
|
|
def pending?
|
|
self.class.pending?(data)
|
|
end
|
|
|
|
# The native amount object ({ value:, currencyCode: }) as a hash.
|
|
def amount_data
|
|
@amount_data ||= data[:amount].is_a?(Hash) ? data[:amount].with_indifferent_access : {}
|
|
end
|
|
|
|
# The foreign-currency amount object for FX transactions, or empty hash.
|
|
def foreign_amount_data
|
|
@foreign_amount_data ||= data[:foreignAmount].is_a?(Hash) ? data[:foreignAmount].with_indifferent_access : {}
|
|
end
|
|
|
|
# CurrencyNormalizable hook: warn when an Up currency code is unrecognized.
|
|
def log_invalid_currency(currency_value)
|
|
Rails.logger.warn("Invalid currency code '#{currency_value}' in Up transaction #{external_id}, falling back to account currency")
|
|
end
|
|
end
|