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
209 lines
6.3 KiB
Ruby
209 lines
6.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class RedbarkItem < ApplicationRecord
|
|
include Syncable, Provided, Unlinking, Encryptable
|
|
|
|
enum :status, { good: "good", requires_update: "requires_update" }, default: :good
|
|
|
|
# Encrypt sensitive credentials and raw payloads if ActiveRecord encryption is configured
|
|
if encryption_ready?
|
|
encrypts :api_key, deterministic: true
|
|
encrypts :raw_payload
|
|
encrypts :raw_institution_payload
|
|
end
|
|
|
|
validates :name, presence: true
|
|
# Validate on every save, not just create - an update must never blank the key
|
|
validates :api_key, presence: true
|
|
|
|
belongs_to :family
|
|
has_one_attached :logo, dependent: :purge_later
|
|
|
|
has_many :redbark_accounts, dependent: :destroy
|
|
has_many :accounts, through: :redbark_accounts
|
|
|
|
scope :active, -> { where(scheduled_for_deletion: false) }
|
|
scope :syncable, -> { active }
|
|
scope :ordered, -> { order(created_at: :desc) }
|
|
scope :needs_update, -> { where(status: :requires_update) }
|
|
|
|
def syncer
|
|
RedbarkItem::Syncer.new(self)
|
|
end
|
|
|
|
# Deliberately not transactional - the job must enqueue after the flag commits
|
|
def destroy_later
|
|
update!(scheduled_for_deletion: true)
|
|
DestroyJob.perform_later(self)
|
|
end
|
|
|
|
|
|
# Import data from provider API
|
|
def import_latest_redbark_data(sync: nil)
|
|
provider = redbark_provider
|
|
unless provider
|
|
Rails.logger.error "RedbarkItem #{id} - Cannot import: provider is not configured"
|
|
raise StandardError, I18n.t("redbark_items.errors.provider_not_configured")
|
|
end
|
|
|
|
RedbarkItem::Importer.new(self, redbark_provider: provider, sync: sync).import
|
|
rescue => e
|
|
DebugLogEntry.capture(
|
|
category: "provider_sync",
|
|
level: "error",
|
|
message: "Redbark import failed",
|
|
source: self.class.name,
|
|
provider_key: "redbark",
|
|
family: family,
|
|
metadata: { redbark_item_id: id, error_class: e.class.name, error: e.message }
|
|
)
|
|
Rails.logger.error "RedbarkItem #{id} - Failed to import data: #{e.message}"
|
|
raise
|
|
end
|
|
|
|
# Process linked accounts after data import
|
|
def process_accounts
|
|
return [] if redbark_accounts.empty?
|
|
|
|
results = []
|
|
linked_redbark_accounts.includes(account_provider: :account).each do |redbark_account|
|
|
begin
|
|
result = RedbarkAccount::Processor.new(redbark_account).process
|
|
results << { redbark_account_id: redbark_account.id, success: true, result: result }
|
|
rescue => e
|
|
DebugLogEntry.capture(
|
|
category: "provider_sync",
|
|
level: "error",
|
|
message: "Failed to process Redbark account",
|
|
source: self.class.name,
|
|
provider_key: "redbark",
|
|
family: family,
|
|
metadata: { redbark_account_id: redbark_account.id, error_class: e.class.name, error: e.message }
|
|
)
|
|
results << { redbark_account_id: redbark_account.id, success: false, error: e.message }
|
|
end
|
|
end
|
|
|
|
results
|
|
end
|
|
|
|
# Schedule sync jobs for all linked accounts
|
|
def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
|
|
return [] if accounts.empty?
|
|
|
|
results = []
|
|
accounts.visible.each do |account|
|
|
begin
|
|
account.sync_later(
|
|
parent_sync: parent_sync,
|
|
window_start_date: window_start_date,
|
|
window_end_date: window_end_date
|
|
)
|
|
results << { account_id: account.id, success: true }
|
|
rescue => e
|
|
DebugLogEntry.capture(
|
|
category: "provider_sync",
|
|
level: "error",
|
|
message: "Redbark account sync scheduling failed",
|
|
source: self.class.name,
|
|
provider_key: "redbark",
|
|
family: family,
|
|
metadata: { redbark_item_id: id, account_id: account.id, error_class: e.class.name, error: e.message }
|
|
)
|
|
Rails.logger.error "RedbarkItem #{id} - Failed to schedule sync for account #{account.id}: #{e.message}"
|
|
results << { account_id: account.id, success: false, error: e.message }
|
|
end
|
|
end
|
|
|
|
results
|
|
end
|
|
|
|
def upsert_redbark_snapshot!(accounts_snapshot)
|
|
assign_attributes(
|
|
raw_payload: accounts_snapshot
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
def has_completed_initial_setup?
|
|
accounts.any?
|
|
end
|
|
|
|
# Linked accounts (have AccountProvider association)
|
|
def linked_redbark_accounts
|
|
redbark_accounts.joins(:account_provider)
|
|
end
|
|
|
|
# Unlinked accounts still awaiting setup (no AccountProvider link, not skipped by the user)
|
|
def unlinked_redbark_accounts
|
|
redbark_accounts.needs_setup
|
|
end
|
|
|
|
def sync_status_summary
|
|
if account_counts[:total] == 0
|
|
I18n.t("redbark_items.sync_status.no_accounts")
|
|
elsif account_counts[:needs_setup] == 0
|
|
I18n.t("redbark_items.sync_status.synced", count: account_counts[:linked])
|
|
else
|
|
I18n.t("redbark_items.sync_status.synced_with_setup", linked: account_counts[:linked], unlinked: account_counts[:needs_setup])
|
|
end
|
|
end
|
|
|
|
def linked_accounts_count
|
|
account_counts[:linked]
|
|
end
|
|
|
|
def unlinked_accounts_count
|
|
account_counts[:needs_setup]
|
|
end
|
|
|
|
def total_accounts_count
|
|
account_counts[:total]
|
|
end
|
|
|
|
def institution_display_name
|
|
institution_name.presence || institution_domain.presence || name
|
|
end
|
|
|
|
def connected_institutions
|
|
redbark_accounts.includes(:account)
|
|
.where.not(institution_metadata: nil)
|
|
.map { |acc| acc.institution_metadata }
|
|
.uniq { |inst| inst["name"] || inst["institution_name"] }
|
|
end
|
|
|
|
def institution_summary
|
|
institutions = connected_institutions
|
|
case institutions.count
|
|
when 0
|
|
I18n.t("redbark_items.institution_summary.none")
|
|
else
|
|
I18n.t("redbark_items.institution_summary.count", count: institutions.count)
|
|
end
|
|
end
|
|
|
|
def credentials_configured?
|
|
api_key.present?
|
|
end
|
|
|
|
private
|
|
|
|
# One grouped query instead of separate COUNTs for each of the
|
|
# linked/unlinked/total figures the accounts index partial reads.
|
|
def account_counts
|
|
@account_counts ||= begin
|
|
rows = redbark_accounts
|
|
.left_joins(:account_provider)
|
|
.group(:ignored, Arel.sql("account_providers.id IS NULL"))
|
|
.count
|
|
|
|
{
|
|
total: rows.values.sum,
|
|
linked: rows.sum { |(_ignored, unlinked), count| unlinked ? 0 : count },
|
|
needs_setup: rows.sum { |(ignored, unlinked), count| unlinked && !ignored ? count : 0 }
|
|
}
|
|
end
|
|
end
|
|
end
|