mirror of
https://github.com/we-promise/sure.git
synced 2026-07-21 01:05:28 +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>
142 lines
5.2 KiB
Ruby
142 lines
5.2 KiB
Ruby
class UpItem::Syncer
|
|
include SyncStats::Collector
|
|
|
|
SafeSyncError = Class.new(StandardError)
|
|
|
|
# Error carrying the structured per-stage sync errors for health reporting.
|
|
class SyncError < StandardError
|
|
attr_reader :sync_errors
|
|
|
|
# Build the error with a +message+ and the collected +sync_errors+.
|
|
def initialize(message, sync_errors:)
|
|
super(message)
|
|
@sync_errors = sync_errors
|
|
end
|
|
end
|
|
|
|
attr_reader :up_item
|
|
|
|
# Build a syncer for the given +up_item+.
|
|
def initialize(up_item)
|
|
@up_item = up_item
|
|
end
|
|
|
|
# Run the full sync: import, account setup detection, transaction processing,
|
|
# balance sync scheduling, and stats/health collection. Raises on failures.
|
|
def perform_sync(sync)
|
|
sync.update!(status_text: "Importing accounts from Up...") if sync.respond_to?(:status_text)
|
|
import_result = up_item.import_latest_up_data
|
|
raise_if_failed_result!(import_result, stage: "Up import")
|
|
|
|
sync.update!(status_text: "Checking account configuration...") if sync.respond_to?(:status_text)
|
|
collect_setup_stats(sync, provider_accounts: up_item.up_accounts)
|
|
|
|
linked_accounts = up_item.up_accounts.joins(:account_provider)
|
|
unlinked_accounts = up_item.up_accounts.needs_setup
|
|
|
|
if unlinked_accounts.any?
|
|
up_item.update!(pending_account_setup: true)
|
|
sync.update!(status_text: "#{unlinked_accounts.count} accounts need setup...") if sync.respond_to?(:status_text)
|
|
else
|
|
up_item.update!(pending_account_setup: false)
|
|
end
|
|
|
|
if linked_accounts.any?
|
|
sync.update!(status_text: "Processing transactions...") if sync.respond_to?(:status_text)
|
|
mark_import_started(sync)
|
|
process_results = up_item.process_accounts
|
|
raise_if_failed_results!(process_results, stage: "Up account processing")
|
|
|
|
sync.update!(status_text: "Calculating balances...") if sync.respond_to?(:status_text)
|
|
schedule_results = up_item.schedule_account_syncs(
|
|
parent_sync: sync,
|
|
window_start_date: sync.window_start_date,
|
|
window_end_date: sync.window_end_date
|
|
)
|
|
raise_if_failed_results!(schedule_results, stage: "Up account sync scheduling")
|
|
|
|
account_ids = linked_accounts.includes(:account_provider).filter_map { |ua| ua.current_account&.id }
|
|
collect_transaction_stats(sync, account_ids: account_ids, source: "up")
|
|
else
|
|
Rails.logger.info "UpItem::Syncer - No linked accounts to process"
|
|
end
|
|
|
|
collect_health_stats(sync, errors: nil)
|
|
rescue SyncError => e
|
|
collect_health_stats(sync, errors: e.sync_errors)
|
|
raise
|
|
rescue => e
|
|
safe_message = I18n.t("up_item.errors.sync_failed")
|
|
DebugLogEntry.capture(
|
|
category: "provider_sync_error",
|
|
level: "error",
|
|
message: "Unexpected sync error",
|
|
source: self.class.name,
|
|
provider_key: "up",
|
|
family: up_item.family,
|
|
metadata: { up_item_id: up_item.id, error_class: e.class.name, error_message: e.message }
|
|
)
|
|
collect_health_stats(sync, errors: [ { message: safe_message, category: "sync_error" } ])
|
|
raise SafeSyncError.new(safe_message), cause: nil
|
|
end
|
|
|
|
# Post-sync hook (no work required for Up).
|
|
def perform_post_sync
|
|
# no-op
|
|
end
|
|
|
|
private
|
|
|
|
# Raise a SyncError if a single result hash indicates failure.
|
|
def raise_if_failed_result!(result, stage:)
|
|
return unless failed_result?(result)
|
|
|
|
errors = errors_from_result(result, stage: stage)
|
|
raise SyncError.new(error_message(stage, errors), sync_errors: errors)
|
|
end
|
|
|
|
# Raise a SyncError if any result in the collection indicates failure.
|
|
def raise_if_failed_results!(results, stage:)
|
|
errors = Array(results).filter_map do |result|
|
|
next unless failed_result?(result)
|
|
|
|
errors_from_result(result, stage: stage).first
|
|
end
|
|
|
|
return if errors.empty?
|
|
|
|
raise SyncError.new(error_message(stage, errors), sync_errors: errors)
|
|
end
|
|
|
|
# True when +result+ is a hash explicitly flagged success: false.
|
|
def failed_result?(result)
|
|
result.is_a?(Hash) && result.with_indifferent_access[:success] == false
|
|
end
|
|
|
|
# Normalize a failed result into an array of { message:, category: } errors.
|
|
def errors_from_result(result, stage:)
|
|
data = result.with_indifferent_access
|
|
messages = []
|
|
messages << data[:error] if data[:error].present?
|
|
messages << "#{data[:accounts_failed]} accounts failed" if data[:accounts_failed].to_i.positive?
|
|
messages << "#{data[:transactions_failed]} transactions failed" if data[:transactions_failed].to_i.positive?
|
|
messages.concat(Array(data[:errors]).map { |error| error_message_value(error) }.compact)
|
|
messages << "#{stage} failed" if messages.empty?
|
|
|
|
messages.map { |message| { message: "#{stage}: #{message}", category: "sync_error" } }
|
|
end
|
|
|
|
# Join the error messages into a single stage summary string.
|
|
def error_message(stage, errors)
|
|
messages = errors.map { |error| error[:message] || error["message"] }.compact
|
|
messages.presence&.join(", ") || "#{stage} failed"
|
|
end
|
|
|
|
# Extract a human-readable message from a heterogeneous error value.
|
|
def error_message_value(error)
|
|
return error[:message].presence || error["message"].presence || error[:error].presence || error["error"].presence if error.is_a?(Hash)
|
|
|
|
error.to_s.presence
|
|
end
|
|
end
|