Files
sure/app/models/up_item.rb
Jake dc2a565b6a feat(up): add Up Bank (AU) provider integration (#2391)
* 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>
2026-06-20 20:33:09 +02:00

202 lines
6.6 KiB
Ruby

class UpItem < ApplicationRecord
include Syncable, Provided, Unlinking, Encryptable
enum :status, { good: "good", requires_update: "requires_update" }, default: :good
if encryption_ready?
encrypts :access_token, deterministic: true
encrypts :raw_payload
encrypts :raw_institution_payload
end
belongs_to :family
has_one_attached :logo, dependent: :purge_later
has_many :up_accounts, dependent: :destroy
has_many :accounts, through: :up_accounts
validates :name, presence: true
validates :access_token, presence: true, on: :create
scope :active, -> { where(scheduled_for_deletion: false) }
scope :syncable, -> { active }
scope :ordered, -> { order(created_at: :desc) }
scope :needs_update, -> { where(status: :requires_update) }
# Mark the item for deletion and enqueue the background destroy job.
def destroy_later
update!(scheduled_for_deletion: true)
DestroyJob.perform_later(self)
end
# Run the importer to fetch the latest accounts/transactions from Up.
def import_latest_up_data
provider = up_provider
unless provider
DebugLogEntry.capture(
category: "provider_sync_error",
level: "error",
message: "Cannot import: Up provider is not configured",
source: self.class.name,
provider_key: "up",
family: family,
metadata: { up_item_id: id }
)
raise StandardError.new("Up provider is not configured")
end
UpItem::Importer.new(self, up_provider: provider).import
rescue => e
DebugLogEntry.capture(
category: "provider_sync_error",
level: "error",
message: "Failed to import data",
source: self.class.name,
provider_key: "up",
family: family,
metadata: { up_item_id: id, error_class: e.class.name, error_message: e.message }
)
raise
end
# Process each linked, visible Up account, returning a per-account result array.
def process_accounts
return [] if up_accounts.empty?
up_accounts.joins(:account).merge(Account.visible).map do |up_account|
result = UpAccount::Processor.new(up_account).process
if result.is_a?(Hash) && result.with_indifferent_access[:success] == false
{ up_account_id: up_account.id, success: false, error: I18n.t("up_item.errors.account_processing_failed") }
else
{ up_account_id: up_account.id, success: true, result: result }
end
rescue => e
DebugLogEntry.capture(
category: "provider_sync_error",
level: "error",
message: "Failed to process account",
source: self.class.name,
provider_key: "up",
family: family,
account_provider: up_account.account_provider,
metadata: { up_item_id: id, up_account_id: up_account.id, error_class: e.class.name, error_message: e.message }
)
{ up_account_id: up_account.id, success: false, error: I18n.t("up_item.errors.account_processing_failed") }
end
end
# Enqueue a balance sync for each visible linked account, returning per-account results.
def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
return [] if accounts.empty?
accounts.visible.map do |account|
account.sync_later(
parent_sync: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
{ account_id: account.id, success: true }
rescue => e
DebugLogEntry.capture(
category: "provider_sync_error",
level: "error",
message: "Failed to schedule sync for account",
source: self.class.name,
provider_key: "up",
family: family,
account: account,
metadata: { up_item_id: id, account_id: account.id, error_class: e.class.name, error_message: e.message }
)
{ account_id: account.id, success: false, error: I18n.t("up_item.errors.account_sync_schedule_failed") }
end
end
# Persist the latest raw accounts payload for this item.
def upsert_up_snapshot!(accounts_snapshot)
assign_attributes(raw_payload: accounts_snapshot)
save!
end
# True once at least one Up account has been linked to a Sure account.
def has_completed_initial_setup?
accounts.any?
end
# Human-readable summary of linked vs. unlinked account counts.
def sync_status_summary
total_accounts = total_accounts_count
linked_count = linked_accounts_count
unlinked_count = unlinked_accounts_count
if total_accounts.zero?
I18n.t("up_item.sync_status.no_accounts")
elsif unlinked_count.zero?
I18n.t("up_item.sync_status.all_synced", count: linked_count)
else
I18n.t("up_item.sync_status.partial", linked: linked_count, unlinked: unlinked_count)
end
end
# Number of Up accounts linked to a Sure account.
def linked_accounts_count
account_counts[:linked]
end
# Number of unlinked Up accounts still awaiting setup.
def unlinked_accounts_count
account_counts[:unlinked]
end
# Total number of Up accounts under this item.
def total_accounts_count
account_counts[:total]
end
# Best available display name for the connected institution.
def institution_display_name
institution_name.presence || institution_domain.presence || name
end
# Distinct institution metadata across this item's accounts.
def connected_institutions
up_accounts.includes(:account)
.where.not(institution_metadata: nil)
.map(&:institution_metadata)
.uniq { |inst| inst["name"] || inst["domain"] }
end
# Human-readable summary of connected institutions (none/one/count).
def institution_summary
institutions = connected_institutions
case institutions.count
when 0
I18n.t("up_item.institution_summary.none")
when 1
institutions.first["name"].presence || I18n.t("up_item.institution_summary.one")
else
I18n.t("up_item.institution_summary.count", count: institutions.count)
end
end
# True when an access token is present and the item can call the Up API.
def credentials_configured?
access_token.present?
end
private
# Single query for all three account counts, reused across sync_status_summary
# and the settings partial to avoid 3+ separate COUNT queries per rendered item.
def account_counts
@account_counts ||= begin
rows = up_accounts
.left_joins(:account_provider)
.pluck(Arel.sql("account_providers.id IS NOT NULL"), :ignored)
linked = rows.count { |has_provider, _ignored| has_provider }
unlinked = rows.count { |has_provider, ignored| !has_provider && !ignored }
{ linked: linked, unlinked: unlinked, total: rows.size }
end
end
end