Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(insights): respect recurring_transactions_disabled in subscription_audit (#2831)

Guillem Arias Fauste59852ca0f3f1
diff --git a/app/models/insight/generators/cash_flow_warning_generator.rb b/app/models/insight/generators/cash_flow_warning_generator.rb
index 36ed14bf20..ad1bcdc8a2 100644
--- a/app/models/insight/generators/cash_flow_warning_generator.rb
+++ b/app/models/insight/generators/cash_flow_warning_generator.rb
@@ -78,6 +78,17 @@ class Insight::Generators::CashFlowWarningGenerator < Insight::Generator
# Projected occurrences of known recurring transactions within the horizon.
# Transfers are internal moves, and cross-currency amounts can't be applied
# to a family-currency balance without a rate lookup, so both are skipped.
+ #
+ # Deliberately NOT gated on family.recurring_transactions_disabled?, unlike
+ # SubscriptionAuditGenerator. That generator's entire subject is "your
+ # recurring transactions" — with detection off, there's nothing left to
+ # report. Here, recurring transactions are one input refining a cash-flow
+ # projection whose subject (will your balance dip low in the next 30 days)
+ # stays meaningful either way. Disabling the setting only stops the
+ # identification job from finding NEW recurring transactions
+ # (IdentifyRecurringTransactionsJob) — it doesn't clear out ones already
+ # identified, so this keeps using that last-known set rather than silently
+ # degrading to the flatter median-only baseline.
def upcoming_recurring_entries
family.recurring_transactions
.expected_soon
diff --git a/app/models/insight/generators/subscription_audit_generator.rb b/app/models/insight/generators/subscription_audit_generator.rb
index dbbae89525..3a3d00708a 100644
--- a/app/models/insight/generators/subscription_audit_generator.rb
+++ b/app/models/insight/generators/subscription_audit_generator.rb
@@ -8,6 +8,8 @@ class Insight::Generators::SubscriptionAuditGenerator < Insight::Generator
MAX_INSIGHTS = 3
def generate
+ return [] if family.recurring_transactions_disabled?
+
overdue_recurring.map do |recurring|
name = recurring.merchant&.name.presence || recurring.name
amount = Money.new(recurring.amount, recurring.currency).format
diff --git a/test/models/insight/generators/subscription_audit_generator_test.rb b/test/models/insight/generators/subscription_audit_generator_test.rb
new file mode 100644
index 0000000000..373b69e3ac
--- /dev/null
+++ b/test/models/insight/generators/subscription_audit_generator_test.rb
@@ -0,0 +1,24 @@
+require "test_helper"
+
+class Insight::Generators::SubscriptionAuditGeneratorTest < ActiveSupport::TestCase
+ setup do
+ @family = families(:dylan_family)
+ @overdue = recurring_transactions(:netflix_subscription)
+ @overdue.update!(next_expected_date: 50.days.ago.to_date)
+ end
+
+ test "generates an insight for an overdue recurring transaction" do
+ insights = Insight::Generators::SubscriptionAuditGenerator.new(@family).generate
+
+ assert_equal 1, insights.size
+ assert_equal "subscription_audit:#{@overdue.id}", insights.first.dedup_key
+ end
+
+ test "returns nothing when recurring transaction detection is disabled" do
+ @family.update!(recurring_transactions_disabled: true)
+
+ insights = Insight::Generators::SubscriptionAuditGenerator.new(@family).generate
+
+ assert_empty insights
+ end
+end