Files
sure/app/models/recurring_transaction/cleaner.rb
soky srm e290e3d4a1 Recurring transactions (#271)
* Implement recurring transactions support

* Amount fix

* Hide section when any filter is applied

* Add automatic identify feature

Automatic identification runs after:
  - CSV Import completes (TransactionImport, TradeImport, AccountImport, MintImport)
  - Plaid sync completes
  - SimpleFIN sync completes
  - LunchFlow sync completes
- Any new provider that we create.

* Fix linter and tests

* Fix address review

* FIX proper text sizing

* Fix further linter

Use circular distance to handle month-boundary wrapping

* normalize to a circular representation before computing the median

* Better tests validation

* Added some UI info

Fix pattern identification, last recurrent transaction needs to happened within the last 45 days.

* Fix styling

* Revert text subdued look

* Match structure of the other sections

* Styling

* Restore positive amounts styling

* Shorten label for UI styling

---------

Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2025-11-01 09:12:42 +01:00

42 lines
1.1 KiB
Ruby

class RecurringTransaction
class Cleaner
attr_reader :family
def initialize(family)
@family = family
end
# Mark recurring transactions as inactive if they haven't occurred in 2+ months
def cleanup_stale_transactions
two_months_ago = 2.months.ago.to_date
stale_transactions = family.recurring_transactions
.active
.where("last_occurrence_date < ?", two_months_ago)
stale_count = 0
stale_transactions.find_each do |recurring_transaction|
# Double-check if there are any recent matching transactions
recent_matches = recurring_transaction.matching_transactions.select { |entry| entry.date >= two_months_ago }
if recent_matches.empty?
recurring_transaction.mark_inactive!
stale_count += 1
end
end
stale_count
end
# Remove inactive recurring transactions that have been inactive for 6+ months
def remove_old_inactive_transactions
six_months_ago = 6.months.ago
family.recurring_transactions
.inactive
.where("updated_at < ?", six_months_ago)
.destroy_all
end
end
end