Files
sure/app/models/recurring_transaction/identifier.rb
LPW 61eb611529 Simplefin enhancements v2 (#267)
* SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint

# Conflicts:
#	db/schema.rb

# Conflicts:
#	app/controllers/simplefin_items_controller.rb

* fix testing

* fix linting

* xfix linting x2

* Review PR #267 on we-promise/sure (SimpleFin enhancements v2). Address all 15 actionable CodeRabbit comments: Add UUID validations in rakes (e.g., simplefin_unlink), swap Ruby pattern matching/loops for efficient DB queries (e.g., where LOWER(name) LIKE ?), generate docstrings for low-coverage areas (31%), consolidate routes for simplefin_items, move view logic to helpers (e.g., format_transaction_extra), strengthen tests with exact assertions/fixtures for dedup/relink failures. Also, check for overlaps with merged #262 (merchants fix): Ensure merchant creation in simplefin_entry/processor.rb aligns with new payee-based flow and MD5 IDs; add tests for edge cases like empty payees or over-merging pendings. Prioritize security (PII redaction in logs, no hardcoded secrets).

* SimpleFin: address CodeRabbit comments (batch 1)

- Consolidate simplefin_items routes under a single resources block; keep URLs stable
- Replace inline JS with Stimulus auto-relink controller; auto-load relink modal via global modal frame
- Improve a11y in relink modal by wrapping rows in labels
- Harden unlink rake: default dry_run=true, UUID validation, redact PII in outputs, clearer errors
- Backfill rake: default dry_run=true, UUID validation; groundwork for per-SFA counters
- Fix-was-merged rake: default dry_run=true, UUID validation; clearer outputs
- Idempotent transfer auto-match (find_or_create_by! + RecordNotUnique rescue)
- Extract SimpleFin error tooltip assembly into helper and use it in view

RuboCop: maintain 2-space indentation, spaces inside array brackets, spaces after commas, and no redundant returns

* Linter noise

* removed filed commited by mistake.

* manual relink flow and tighten composite matching

* enforce manual relink UI; fix adapter keywords; guarantee extra.simplefin hash

* refactor(simplefin): extract relink service; enforce manual relink UI; tighten composite match; migration 7.2

* add provider date parser; refactor rake; move view queries; partial resilience

* run balances-only import in background job. make update flow enqueue balances-only job

* persists across all update redirects and initialize
used_manual_ids to prevent NameError in relink candidate computation.

* SimpleFin: metadata + merge fixes; holdings (incl. crypto) + Day Change; Sync Summary; ops rakes; lint

* Fixed failed test after rebase.

* scan_ruby fix

* Calming the rabbit:
Fix AccountProvider linking when accounts change
Drop the legacy unique index instead of duplicating it
Fix dynamic constant assignment
Use fixtures consistently; avoid rescue for control flow.
Replace bare rescue with explicit exception class.
Move business logic out of the view.
Critical: Transaction boundary excludes recompute phase, risking data loss.
Inconsistency between documentation and implementation for zero-error case.
Refactor to use the compute_unlinked_count helper for consistency.
Fix cleanup task default: it deletes by default.
Move sync stats computation to controller to avoid N+1 queries.
Consolidate duplicate sync query.
Clarify the intent of setting flash notice on the error path.
Fix Date/Time comparison in should_be_inactive?.
Move stats retrieval logic to controller.
Remove duplicate Sync summary section.
Remove the unnecessary sleep statement; use Capybara's built-in waiting.
Add label wrappers for accessibility and consistency.

* FIX SimpleFIN new account modal

Now new account properly loads as a Modal, instead of new page.
Fixes also form showing dashboard instead of settings page.

* Remove SimpleFin legacy UI components, migrate schema, and refine linking behavior.

# Conflicts:
#	app/helpers/settings_helper.rb

* Extract SimpleFin-related logic to `prepare_show_context` helper and refactor for consistency. Adjust conditional checks and ensure controller variables are properly initialized.

* Remove unused SimpleFin maps from prepare_show_context; select IDs to avoid N+1
Replace Tailwind bg-green-500 with semantic bg-success in _simplefin_panel/_provider_form
Add f.label :setup_token in simplefin_items/new for a11y
Remove duplicate require in AccountsControllerSimplefinCtaTest

* Remove unnecessary blank lines

* Reduce unnecessary changes

This reduces the diff against main

* Simplefin Account Setup: Display in modal

This fixes an issue with the `X` dismiss button in the top right corner

* Removed unnecessary comment.

* removed unnecessary function.

* fixed broken links

* Removed unnecessary file

* changed to database query

* set to use UTC and gaurd against null

* set dry_run=true

* Fixed comment

* Changed to use a database-level query

* matched test name to test behavior.

* Eliminate code duplication and Time.zone dependency

* make final summary surface failures

* lint fix

* Revised timezone comment. better handle missing selectors.

* sanitized LIKE wildcards

* Fixed SimpleFin import to avoid “Currency can’t be blank” validation failures when providers return an empty currency string.

* Added helper methods for admin and self-hosted checks

* Specify exception types in rescue clauses.

* Refined logic to determine transaction dates for credit accounts.

* Refined stats calculation for `total_accounts` to track the maximum unique accounts per run instead of accumulating totals.

* Moved `unlink_all!` logic to `SimplefinItem::Unlinking` concern and deprecated `SimplefinItem::Unlinker`. Updated related references.

* Refined legacy unlinking logic, improved `current_holdings` formatting, and added ENV-based overrides for self-hosted checks.

* Enhanced `unlink_all!` with explicit error handling, improved transaction safety, and refined ENV-based self-hosted checks. Adjusted exception types and cleaned up private method handling.

* Improved currency assignment logic by adding fallback to `current_account` and `family` currencies.

* Enhanced error tracking during SimpleFin account imports by adding categorized error buckets, limiting stored errors to the last 5, and improving `stats` calculations.

* typo fix

* Didn't realize rabbit was still mad...
Refactored SimpleFin error handling and CTA logic: centralized duplicate detection and relink visibility into controller, improved task counters, adjusted redirect notices, and fixed form indexing.

* Dang rabbit never stops... Centralized SimpleFin maps logic into `MapsHelper` concern and integrated it into relevant controllers and rake tasks. Optimized queries, reduced redundancy, and improved unlinked counts and manual account checks with batch processing. Adjusted task arguments for clarity.

* Persistent rabbit. Optimized SimpleFin maps logic by implementing batch queries for manual account and unlinked count checks, reducing N+1 issues. Improved clarity of rake task argument descriptions and error messages for better usability.

* Lost a commit somehow, resolved here. Refactored transaction extra details logic by introducing `build_transaction_extra_details` helper to improve clarity, reusability, and reduce view complexity. Enhanced rake tasks with strict dry-run validation and better error handling. Updated schema to allow nullable `merchant_id` and added conditional unique indexes for recurring transactions.

* Refactored sensitive data redaction in `simplefin_unlink` task for recursive handling, optimized SQL sanitization in `simplefin_holdings_backfill`, improved error handling in `transactions_helper`, and streamlined day change calculation logic in `Holding` model.

* Lint fix

* Removed per PR comments.

* Also removing per PR comment.

* git commit -m "SimpleFIN polish: preserve #manual-accounts wrapper, unify \"manual\" scope, and correct unlinked counts
- Preserve #manual-accounts wrapper: switch non-empty updates to turbo_stream.update and background broadcast_update_to; keep empty-path replace to render <div id=\"manual-accounts\"></div>
- Unify definition of manual accounts via Account.visible_manual (visible + legacy-nil + no AccountProvider); reuse in controllers, jobs, and helper
- Correct setup/unlinked counts: SimplefinItem::Syncer#finalize_setup_counts and maps now consider AccountProvider links (legacy account AND provider must be absent)
Deleted:
- app/models/simplefin_item/relink_service.rb
- app/controllers/concerns/simplefin_items/relink_helpers.rb
- app/javascript/controllers/auto_relink_controller.js
- app/views/simplefin_items/_relink_modal.html.erb
- app/views/simplefin_items/manual_relink.html.erb
- app/views/simplefin_items/relink.html.erb
- test/services/simplefin_item/relink_service_test.rb
Refs: PR #318 unified link/unlink; PR #267 SimpleFIN; follow-up to fix wrapper ID loss and counting drift."

* Extend unlinked account check to include "Investment" type

* set SimpleFIN item for `balances`, remove redundant unpacking, and improve holdings task error

* SimpleFIN: add `errors` action + modal; do not reintroduce legacy relink actions; removed dead helper

* FIX simpleFIN linking

* Add delay back, tests benefit from it

* Put cache back in

* Remove empty `rake` task

* Small spelling fixes.

---------

Signed-off-by: soky srm <sokysrm@gmail.com>
Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: sokie <sokysrm@gmail.com>
Co-authored-by: Dylan Corrales <deathcamel58@gmail.com>
2025-11-17 21:51:37 +01:00

267 lines
10 KiB
Ruby

class RecurringTransaction
class Identifier
attr_reader :family
def initialize(family)
@family = family
end
# Identify and create/update recurring transactions for the family
def identify_recurring_patterns
three_months_ago = 3.months.ago.to_date
# Get all transactions from the last 3 months
entries_with_transactions = family.entries
.where(entryable_type: "Transaction")
.where("entries.date >= ?", three_months_ago)
.includes(:entryable)
.to_a
# Group by merchant (if present) or name, along with amount (preserve sign) and currency
grouped_transactions = entries_with_transactions
.select { |entry| entry.entryable.is_a?(Transaction) }
.group_by do |entry|
transaction = entry.entryable
# Use merchant_id if present, otherwise use entry name
identifier = transaction.merchant_id.present? ? [ :merchant, transaction.merchant_id ] : [ :name, entry.name ]
[ identifier, entry.amount.round(2), entry.currency ]
end
recurring_patterns = []
grouped_transactions.each do |(identifier, amount, currency), entries|
next if entries.size < 3 # Must have at least 3 occurrences
# Check if the last occurrence was within the last 45 days
last_occurrence = entries.max_by(&:date)
next if last_occurrence.date < 45.days.ago.to_date
# Check if transactions occur on similar days (within 5 days of each other)
days_of_month = entries.map { |e| e.date.day }.sort
# Calculate if days cluster together (standard deviation check)
if days_cluster_together?(days_of_month)
expected_day = calculate_expected_day(days_of_month)
# Unpack identifier - either [:merchant, id] or [:name, name_string]
identifier_type, identifier_value = identifier
pattern = {
amount: amount,
currency: currency,
expected_day_of_month: expected_day,
last_occurrence_date: last_occurrence.date,
occurrence_count: entries.size,
entries: entries
}
if identifier_type == :merchant
pattern[:merchant_id] = identifier_value
else
pattern[:name] = identifier_value
end
recurring_patterns << pattern
end
end
# Create or update RecurringTransaction records
recurring_patterns.each do |pattern|
# Build find conditions based on whether it's merchant-based or name-based
find_conditions = {
amount: pattern[:amount],
currency: pattern[:currency]
}
if pattern[:merchant_id].present?
find_conditions[:merchant_id] = pattern[:merchant_id]
find_conditions[:name] = nil
else
find_conditions[:name] = pattern[:name]
find_conditions[:merchant_id] = nil
end
recurring_transaction = family.recurring_transactions.find_or_initialize_by(find_conditions)
# Handle manual recurring transactions specially
if recurring_transaction.persisted? && recurring_transaction.manual?
# Update variance for manual recurring transactions
update_manual_recurring_variance(recurring_transaction, pattern)
next
end
# Set the name or merchant_id on new records
if recurring_transaction.new_record?
if pattern[:merchant_id].present?
recurring_transaction.merchant_id = pattern[:merchant_id]
else
recurring_transaction.name = pattern[:name]
end
# New auto-detected recurring transactions are not manual
recurring_transaction.manual = false
end
recurring_transaction.assign_attributes(
expected_day_of_month: pattern[:expected_day_of_month],
last_occurrence_date: pattern[:last_occurrence_date],
next_expected_date: calculate_next_expected_date(pattern[:last_occurrence_date], pattern[:expected_day_of_month]),
occurrence_count: pattern[:occurrence_count],
status: recurring_transaction.new_record? ? "active" : recurring_transaction.status
)
recurring_transaction.save!
end
# Also check for manual recurring transactions that might need variance updates
update_manual_recurring_transactions(three_months_ago)
recurring_patterns.size
end
# Update variance for existing manual recurring transactions
def update_manual_recurring_transactions(since_date)
family.recurring_transactions.where(manual: true, status: "active").find_each do |recurring|
# Find matching transactions in the recent period
matching_entries = RecurringTransaction.find_matching_transaction_entries(
family: family,
merchant_id: recurring.merchant_id,
name: recurring.name,
currency: recurring.currency,
expected_day: recurring.expected_day_of_month,
lookback_months: 6
)
next if matching_entries.empty?
# Extract amounts and dates from all matching entries
matching_amounts = matching_entries.map(&:amount)
last_entry = matching_entries.max_by(&:date)
# Recalculate variance from all occurrences (including identical amounts)
recurring.update!(
expected_amount_min: matching_amounts.min,
expected_amount_max: matching_amounts.max,
expected_amount_avg: matching_amounts.sum / matching_amounts.size,
occurrence_count: matching_amounts.size,
last_occurrence_date: last_entry.date,
next_expected_date: calculate_next_expected_date(last_entry.date, recurring.expected_day_of_month)
)
end
end
# Update variance for a manual recurring transaction when pattern is found
def update_manual_recurring_variance(recurring_transaction, pattern)
# Check if this transaction's date is more recent
if pattern[:last_occurrence_date] > recurring_transaction.last_occurrence_date
# Find all matching transactions to recalculate variance
matching_entries = RecurringTransaction.find_matching_transaction_entries(
family: family,
merchant_id: recurring_transaction.merchant_id,
name: recurring_transaction.name,
currency: recurring_transaction.currency,
expected_day: recurring_transaction.expected_day_of_month,
lookback_months: 6
)
# Update if we have any matching transactions
if matching_entries.any?
matching_amounts = matching_entries.map(&:amount)
recurring_transaction.update!(
expected_amount_min: matching_amounts.min,
expected_amount_max: matching_amounts.max,
expected_amount_avg: matching_amounts.sum / matching_amounts.size,
occurrence_count: matching_amounts.size,
last_occurrence_date: pattern[:last_occurrence_date],
next_expected_date: calculate_next_expected_date(pattern[:last_occurrence_date], recurring_transaction.expected_day_of_month)
)
end
end
end
private
# Check if days cluster together (within ~5 days variance)
# Uses circular distance to handle month-boundary wrapping (e.g., 28, 29, 30, 31, 1, 2)
def days_cluster_together?(days)
return false if days.empty?
# Calculate median as reference point
median = calculate_expected_day(days)
# Calculate circular distances from median
circular_distances = days.map { |day| circular_distance(day, median) }
# Calculate standard deviation of circular distances
mean_distance = circular_distances.sum.to_f / circular_distances.size
variance = circular_distances.map { |dist| (dist - mean_distance)**2 }.sum / circular_distances.size
std_dev = Math.sqrt(variance)
# Allow up to 5 days standard deviation
std_dev <= 5
end
# Calculate circular distance between two days on a 31-day circle
# Examples:
# circular_distance(1, 31) = 2 (wraps around: 31 -> 1 is 1 day forward)
# circular_distance(28, 2) = 5 (wraps: 28, 29, 30, 31, 1, 2)
def circular_distance(day1, day2)
linear_distance = (day1 - day2).abs
wrap_distance = 31 - linear_distance
[ linear_distance, wrap_distance ].min
end
# Calculate the expected day based on the most common day
# Uses circular rotation to handle month-wrapping sequences (e.g., [29, 30, 31, 1, 2])
def calculate_expected_day(days)
return days.first if days.size == 1
# Convert to 0-indexed (0-30 instead of 1-31) for modular arithmetic
days_0 = days.map { |d| d - 1 }
# Find the rotation (pivot) that minimizes span, making the cluster contiguous
# This handles month-wrapping sequences like [29, 30, 31, 1, 2]
best_pivot = 0
min_span = Float::INFINITY
(0..30).each do |pivot|
rotated = days_0.map { |d| (d - pivot) % 31 }
span = rotated.max - rotated.min
if span < min_span
min_span = span
best_pivot = pivot
end
end
# Rotate days using best pivot to create contiguous array
rotated_days = days_0.map { |d| (d - best_pivot) % 31 }.sort
# Calculate median on rotated, contiguous array
mid = rotated_days.size / 2
rotated_median = if rotated_days.size.odd?
rotated_days[mid]
else
# For even count, average and round
((rotated_days[mid - 1] + rotated_days[mid]) / 2.0).round
end
# Map median back to original day space (unrotate) and convert to 1-indexed
original_day = (rotated_median + best_pivot) % 31 + 1
original_day
end
# Calculate next expected date
def calculate_next_expected_date(last_date, expected_day)
next_month = last_date.next_month
begin
Date.new(next_month.year, next_month.month, expected_day)
rescue ArgumentError
# If day doesn't exist in month, use last day of month
next_month.end_of_month
end
end
end
end