mirror of
https://github.com/we-promise/sure.git
synced 2026-08-05 16:42:18 +00:00
* add redbark provider integration - per family api key provider, built like the lunchflow integration - syncs accounts, balances and transactions from api.redbark.com - account setup flow, settings panel, locales and routes - tests and fixtures * harden redbark integration based on prior provider pr feedback - use DebugLogEntry.capture for sync/import/unlink failures - retry 429s and 5xxs with backoff, raise on page cap instead of truncating - keep raw response bodies out of logs and errors - not null constraints on account columns, migration base 7.2 - persist ignored flag for skipped accounts so they stop nagging setup - validate api key on every save, re-arm status on key rotation - destroy aborts if unlink fails, atomic account create and link - require_admin on mutating actions, see_other on error redirects - single grouped query for item account counts - i18n default connection name, blank password field value - controller and provider tests * fix issues found in second review sweep - add missing syncable scope, without it every family sync raises - kick off a sync on connection create and on key rotation - setup dialog fetches accounts inline for fresh connections and shows api errors - skip balance write when no balance has been fetched yet, never anchor a false zero - exclude stale and non banking accounts from the batched balances call, per account fallback if the batch is rejected - detect the server row ceiling and empty pages instead of silently truncating history - user sync start date only governs the initial backfill, incremental after that - fetch connections before the per account loop so auth errors propagate once - drop untemplated index/show/new/edit routes and dead preload/link_accounts actions - stable dom id on the settings panel so repeat turbo replaces keep working * skip brokerage connections, found in live testing - the transactions endpoint 400s for brokerage connections, they belong to /v1/trades - only import accounts from banking and documents connections - guard transaction fetches for any legacy linked non banking account * address review feedback - treat the truncation header as a pagination signal: split the date window and refetch instead of failing the account - prune stale pending rows from the snapshot so settled pendings cant come back as duplicates - block linking a sure account that already has another provider feed - count setup failures separately from skips and surface an error instead of "all skipped" - add not nulls on redbark_items name and api key - enqueue the destroy job after the flag commits, not inside the transaction - swap bg-gray-400 for bg-surface-inset, drop amounts from info logs, remove i18n default fallbacks - tests for window splitting, pending pruning and encrypted payload round trip * fix issues from convention review - benign skips (unlinked account, blank id, unparseable rows) no longer count as failures, tracked separately so a clean batch reports success - currency parsing goes through extract_currency so hash shaped payloads resolve instead of falling to the default - merchant ids use truncated sha256 instead of md5 - debug log entries for import failures and account sync scheduling failures * bound the raw transactions snapshot to the fetch window - trim raw_transactions_payload to the current fetch window on merge, same as brex - keep rows without a parseable date, drop settled pendings as before - surface skipped rows in the aggregate debug log entry with imported/skipped counts
83 lines
3.0 KiB
Ruby
83 lines
3.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class RedbarkItem::Syncer
|
|
include SyncStats::Collector
|
|
|
|
attr_reader :redbark_item
|
|
|
|
def initialize(redbark_item)
|
|
@redbark_item = redbark_item
|
|
end
|
|
|
|
def perform_sync(sync)
|
|
Rails.logger.info "RedbarkItem::Syncer - Starting sync for item #{redbark_item.id}"
|
|
|
|
# Phase 1: Import data from provider API
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.importing")) if sync.respond_to?(:status_text)
|
|
import_stats = redbark_item.import_latest_redbark_data(sync: sync)
|
|
|
|
# Phase 2: Collect setup statistics
|
|
finalize_setup_counts(sync)
|
|
|
|
# Phase 3: Process data for linked accounts
|
|
linked_redbark_accounts = redbark_item.linked_redbark_accounts.includes(account_provider: :account)
|
|
if linked_redbark_accounts.any?
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.processing")) if sync.respond_to?(:status_text)
|
|
mark_import_started(sync)
|
|
redbark_item.process_accounts
|
|
|
|
# Phase 4: Schedule balance calculations
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.calculating")) if sync.respond_to?(:status_text)
|
|
redbark_item.schedule_account_syncs(
|
|
parent_sync: sync,
|
|
window_start_date: sync.window_start_date,
|
|
window_end_date: sync.window_end_date
|
|
)
|
|
|
|
# Phase 5: Collect statistics
|
|
account_ids = linked_redbark_accounts.filter_map { |pa| pa.current_account&.id }
|
|
collect_transaction_stats(sync, account_ids: account_ids, source: "redbark")
|
|
end
|
|
|
|
# Mark sync health, surfacing per-account import errors instead of
|
|
# unconditionally reporting a clean run
|
|
import_errors = import_stats.is_a?(Hash) ? import_stats["errors"] : nil
|
|
collect_health_stats(sync, errors: import_errors.presence)
|
|
rescue Provider::Redbark::AuthenticationError => e
|
|
redbark_item.update!(status: :requires_update)
|
|
collect_health_stats(sync, errors: [ { message: e.message, category: "auth_error" } ])
|
|
raise
|
|
rescue => e
|
|
collect_health_stats(sync, errors: [ { message: e.message, category: "sync_error" } ])
|
|
raise
|
|
end
|
|
|
|
# Public: called by Sync after finalization
|
|
def perform_post_sync
|
|
# Override for post-sync cleanup if needed
|
|
end
|
|
|
|
private
|
|
|
|
def mark_import_started(sync)
|
|
# Mark that we're now processing imported data
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.importing_data")) if sync.respond_to?(:status_text)
|
|
end
|
|
|
|
def finalize_setup_counts(sync)
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.checking_setup")) if sync.respond_to?(:status_text)
|
|
|
|
unlinked_count = redbark_item.unlinked_accounts_count
|
|
|
|
if unlinked_count > 0
|
|
redbark_item.update!(pending_account_setup: true)
|
|
sync.update!(status_text: I18n.t("redbark_items.sync.status.needs_setup", count: unlinked_count)) if sync.respond_to?(:status_text)
|
|
else
|
|
redbark_item.update!(pending_account_setup: false)
|
|
end
|
|
|
|
# Collect setup stats
|
|
collect_setup_stats(sync, provider_accounts: redbark_item.redbark_accounts)
|
|
end
|
|
end
|