mirror of
https://github.com/we-promise/sure.git
synced 2026-05-30 07:49:01 +00:00
* feat(sync): add Brex provider schema Adds Brex item and account tables with per-family credentials, scoped upstream account uniqueness, encrypted token storage, and sanitized provider payload columns. * feat(sync): add Brex provider core Adds Brex item/account models, provider client and adapter support, family connection helpers, and provider enum registration for read-only Brex cash and card data. * feat(sync): add Brex import pipeline Adds Brex account discovery, linked-account sync, cash/card balance processors, transaction import, sanitized metadata handling, and idempotent provider entry processing. * feat(sync): add Brex connection flows Adds Mercury-style Brex connection management, explicit item-scoped account selection and linking, settings provider UI, account index visibility, localized copy, and per-item cache handling. * test(sync): cover Brex provider workflows Adds targeted coverage for Brex provider requests, adapter config, item/account guards, importer behavior, entry processing, and Mercury-style controller flows. * fix(sync): align Brex API edge cases Tightens Brex account fetching against the official card-account response shape, sends transaction start filters as RFC3339 date-times, and keeps provider error bodies out of user-facing messages while expanding provider client guard coverage. * fix(sync): harden Brex provider integration Restrict Brex API base URLs to official hosts, tighten account-selection UI behavior, and add tests for invalid credentials, cache scoping, and provider setup edge cases. * test(sync): avoid Brex secret-shaped fixtures * refactor(sync): extract Brex account flows * fix(sync): address Brex provider review feedback * fix(sync): address Brex review follow-ups Move remaining Brex review cleanup into focused model behavior, tighten link/setup edge cases, localize summaries, and add regression coverage from CodeRabbit feedback. Also records the security-review pass as no-findings after diff-scoped inspection and Brakeman validation. * refactor(sync): split Brex account flow controllers Route Brex account selection and setup actions through small namespaced controllers while keeping existing URLs and helpers stable. Business flow remains in BrexItem::AccountFlow; the main Brex item controller now only handles connection CRUD, provider-panel rendering, destroy, and sync. * fix(sync): address Brex CodeRabbit review * fix(sync): address Brex follow-up review * fix(sync): address Brex review follow-ups * fix(sync): address Brex sync review findings * fix(sync): polish Brex review copy and errors * fix(sync): register Brex provider health * fix(sync): polish Brex bank sync presentation * fix(sync): address Brex review follow-ups * fix(sync): tighten Brex setup params * test(api): stabilize usage rate-limit window * fix(sync): polish Brex setup flow nits * fix(sync): harden Brex setup params * fix(sync): finalize Brex review cleanup --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
84 lines
3.2 KiB
Ruby
84 lines
3.2 KiB
Ruby
class BrexAccount::Transactions::Processor
|
|
attr_reader :brex_account
|
|
|
|
def initialize(brex_account)
|
|
@brex_account = brex_account
|
|
end
|
|
|
|
def process
|
|
unless brex_account.raw_transactions_payload.present?
|
|
Rails.logger.info "BrexAccount::Transactions::Processor - No transactions in raw_transactions_payload for brex_account #{brex_account.id}"
|
|
return { success: true, total: 0, imported: 0, skipped: 0, failed: 0, errors: [], skipped_transactions: [] }
|
|
end
|
|
|
|
total_count = brex_account.raw_transactions_payload.count
|
|
Rails.logger.info "BrexAccount::Transactions::Processor - Processing #{total_count} transactions for brex_account #{brex_account.id}"
|
|
|
|
imported_count = 0
|
|
failed_count = 0
|
|
skipped_count = 0
|
|
errors = []
|
|
skipped = []
|
|
|
|
# Each entry is processed inside a transaction, but to avoid locking up the DB when
|
|
# there are hundreds or thousands of transactions, we process them individually.
|
|
brex_account.raw_transactions_payload.each_with_index do |transaction_data, index|
|
|
begin
|
|
result = BrexEntry::Processor.new(
|
|
transaction_data,
|
|
brex_account: brex_account
|
|
).process
|
|
|
|
if result == :skipped
|
|
skipped_count += 1
|
|
skipped << { index: index, transaction_id: transaction_id_for(transaction_data), reason: "No linked account" }
|
|
elsif result.nil?
|
|
failed_count += 1
|
|
errors << { index: index, transaction_id: transaction_id_for(transaction_data), error: "No transaction imported" }
|
|
else
|
|
imported_count += 1
|
|
end
|
|
rescue ArgumentError => e
|
|
# Validation error - log and continue
|
|
failed_count += 1
|
|
transaction_id = transaction_id_for(transaction_data)
|
|
error_message = "Validation error: #{e.message}"
|
|
Rails.logger.error "BrexAccount::Transactions::Processor - #{error_message} (transaction #{transaction_id})"
|
|
errors << { index: index, transaction_id: transaction_id, error: error_message }
|
|
rescue => e
|
|
# Unexpected error - log with full context and continue
|
|
failed_count += 1
|
|
transaction_id = transaction_id_for(transaction_data)
|
|
error_message = "#{e.class}: #{e.message}"
|
|
Rails.logger.error "BrexAccount::Transactions::Processor - Error processing transaction #{transaction_id}: #{error_message}"
|
|
Rails.logger.error Array(e.backtrace).first(10).join("\n")
|
|
errors << { index: index, transaction_id: transaction_id, error: error_message }
|
|
end
|
|
end
|
|
|
|
result = {
|
|
success: failed_count == 0,
|
|
total: total_count,
|
|
imported: imported_count,
|
|
skipped: skipped_count,
|
|
failed: failed_count,
|
|
errors: errors,
|
|
skipped_transactions: skipped
|
|
}
|
|
|
|
if failed_count > 0
|
|
Rails.logger.warn "BrexAccount::Transactions::Processor - Completed with #{failed_count} failures out of #{total_count} transactions"
|
|
else
|
|
Rails.logger.info "BrexAccount::Transactions::Processor - Successfully processed #{imported_count} transactions"
|
|
end
|
|
|
|
result
|
|
end
|
|
|
|
private
|
|
|
|
def transaction_id_for(transaction_data)
|
|
transaction_data&.dig(:id) || transaction_data&.dig("id") || "unknown"
|
|
end
|
|
end
|