Files
sure/app/models/mercury_entry/processor.rb
ghost 6768d03b3c feat(mercury): pending transactions, kind/counterpartyId metadata, and test coverage (#2452)
* feat(mercury): pending transactions, kind/counterpartyId metadata, and test coverage

- Transaction::PENDING_PROVIDERS: add "mercury" so the existing pending
  reconciliation pipeline (pending→posted amount matching in
  ProviderImportAdapter) activates for Mercury entries

- MercuryEntry::Processor: pass extra: to import_transaction with
    extra["mercury"]["pending"]        = true/false (status == "pending")
    extra["mercury"]["kind"]           = ACH / Wire / Card / etc.
    extra["mercury"]["counterparty_id"] = Mercury counterparty UUID
  Pending transactions are now imported with the flag set rather than
  being silently ignored; failed transactions continue to be skipped

- Tests (new files, 30 cases):
  - test/models/mercury_entry/processor_test.rb  — sign convention,
    date fallback, name priority, notes concat, pending flag, kind,
    counterpartyId, failed skip, idempotency, merchant creation,
    no-linked-account guard
  - test/models/mercury_item/importer_test.rb    — account discovery,
    no-duplicate unlinked records, balance update on linked accounts,
    transaction dedup (append-only new ids), sync window (90-day first
    sync, last_synced_at-7d subsequent), 401 marks requires_update
  - test/models/mercury_account/processor_test.rb — balance update,
    CreditCard sign negation, cash_balance parity, no-linked-account
    no-op, transaction processing delegation

* fix(mercury): address review findings — pending SQL, dedup upsert, N+1, nil assertion

- provider_import_adapter: add mercury to all three find_pending_transaction*
  SQL predicates so Mercury pending entries are found and claimed when the
  posted version arrives (exact, fuzzy, and low-confidence paths)

- mercury_item/importer: replace append-only dedup with an upsert-by-id
  that replaces the stored raw payload when status changes from pending to
  non-pending; prevents pending flag from persisting indefinitely when Mercury
  reuses the same transaction ID for the posted version

- kraken_account/ledger_processor: preload all existing kraken ledger
  external_ids into a Set before the loop; replaces per-entry exists? query
  (N+1) with an in-memory Set#include? lookup

- test/models/mercury_account/processor_test: capture return value from
  process and add assert_nil to enforce the nil contract stated in the test name

* fix(mercury): route transaction-count diagnostics through DebugLogEntry
2026-06-30 07:37:14 +02:00

179 lines
6.0 KiB
Ruby

require "digest/md5"
class MercuryEntry::Processor
include CurrencyNormalizable
# mercury_transaction is the raw hash fetched from Mercury API and converted to JSONB
# Transaction structure: { id, amount, bankDescription, counterpartyId, counterpartyName,
# counterpartyNickname, createdAt, dashboardLink, details,
# estimatedDeliveryDate, failedAt, kind, note, postedAt,
# reasonForFailure, status }
def initialize(mercury_transaction, mercury_account:)
@mercury_transaction = mercury_transaction
@mercury_account = mercury_account
end
def process
# Validate that we have a linked account before processing
unless account.present?
Rails.logger.warn "MercuryEntry::Processor - No linked account for mercury_account #{mercury_account.id}, skipping transaction #{external_id}"
return nil
end
# Skip failed transactions
if data[:status] == "failed"
Rails.logger.debug "MercuryEntry::Processor - Skipping failed transaction #{external_id}"
return nil
end
# Wrap import in error handling to catch validation and save errors
begin
import_adapter.import_transaction(
external_id: external_id,
amount: amount,
currency: currency,
date: date,
name: name,
source: "mercury",
merchant: merchant,
notes: notes,
extra: extra
)
rescue ArgumentError => e
# Re-raise validation errors (missing required fields, invalid data)
Rails.logger.error "MercuryEntry::Processor - Validation error for transaction #{external_id}: #{e.message}"
raise
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
# Handle database save errors
Rails.logger.error "MercuryEntry::Processor - Failed to save transaction #{external_id}: #{e.message}"
raise StandardError.new("Failed to import transaction: #{e.message}")
rescue => e
# Catch unexpected errors with full context
Rails.logger.error "MercuryEntry::Processor - Unexpected error processing transaction #{external_id}: #{e.class} - #{e.message}"
Rails.logger.error e.backtrace.join("\n")
raise StandardError.new("Unexpected error importing transaction: #{e.message}")
end
end
private
attr_reader :mercury_transaction, :mercury_account
def import_adapter
@import_adapter ||= Account::ProviderImportAdapter.new(account)
end
def account
@account ||= mercury_account.current_account
end
def data
@data ||= mercury_transaction.with_indifferent_access
end
def external_id
id = data[:id].presence
raise ArgumentError, "Mercury transaction missing required field 'id'" unless id
"mercury_#{id}"
end
def name
# Use counterparty name or bank description
data[:counterpartyNickname].presence ||
data[:counterpartyName].presence ||
data[:bankDescription].presence ||
"Unknown transaction"
end
def notes
# Combine note and details if present
note_parts = []
note_parts << data[:note] if data[:note].present?
note_parts << data[:details] if data[:details].present?
note_parts.any? ? note_parts.join(" - ") : nil
end
def merchant
counterparty_name = data[:counterpartyName].presence
return nil unless counterparty_name.present?
# Create a stable merchant ID from the counterparty name
# Using digest to ensure uniqueness while keeping it deterministic
merchant_name = counterparty_name.to_s.strip
return nil if merchant_name.blank?
merchant_id = Digest::MD5.hexdigest(merchant_name.downcase)
@merchant ||= begin
import_adapter.find_or_create_merchant(
provider_merchant_id: "mercury_merchant_#{merchant_id}",
name: merchant_name,
source: "mercury"
)
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error "MercuryEntry::Processor - Failed to create merchant '#{merchant_name}': #{e.message}"
nil
end
end
def extra
meta = { "pending" => pending? }
meta["kind"] = data[:kind] if data[:kind].present?
meta["counterparty_id"] = data[:counterpartyId] if data[:counterpartyId].present?
{ "mercury" => meta }
end
def pending?
data[:status] == "pending"
end
def amount
parsed_amount = case data[:amount]
when String
BigDecimal(data[:amount])
when Numeric
BigDecimal(data[:amount].to_s)
else
BigDecimal("0")
end
# Mercury uses standard convention where:
# - Negative amounts are money going out (expenses)
# - Positive amounts are money coming in (income)
# Our app uses opposite convention (expenses positive, income negative)
# So we negate the amount to convert from Mercury to our format
-parsed_amount
rescue ArgumentError => e
Rails.logger.error "Failed to parse Mercury transaction amount: #{data[:amount].inspect} - #{e.message}"
raise
end
def currency
# Mercury is US-only, always USD
"USD"
end
def date
# Mercury provides createdAt and postedAt - use postedAt if available, otherwise createdAt
date_value = data[:postedAt].presence || data[:createdAt].presence
case date_value
when String
# Mercury uses ISO 8601 format: "2024-01-15T10:30:00Z"
DateTime.parse(date_value).to_date
when Integer, Float
# Unix timestamp
Time.at(date_value).to_date
when Time, DateTime
date_value.to_date
when Date
date_value
else
Rails.logger.error("Mercury transaction has invalid date value: #{date_value.inspect}")
raise ArgumentError, "Invalid date format: #{date_value.inspect}"
end
rescue ArgumentError, TypeError => e
Rails.logger.error("Failed to parse Mercury transaction date '#{date_value}': #{e.message}")
raise ArgumentError, "Unable to parse transaction date: #{date_value.inspect}"
end
end