The personal finance app for everyone (by everyone)
Ruby
fix: disable Mark as Recurring button when a manual recurring transaction already exists (#3103)
GFR9d225a21e6b8
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex f6256d9d48..5d3ef24ac8 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -5,6 +5,11 @@ class TransactionsController < ApplicationControllerbefore_action :set_entry_for_tags, only: :update_tagsbefore_action :store_params!, only: :index+ def show+ super+ assign_mark_recurring_state+ end+def newprefill_params_from_duplicate!super@@ -159,6 +164,8 @@ class TransactionsController < ApplicationController# Reload to ensure fresh state for turbo stream rendering@entry.reload+ assign_mark_recurring_state+respond_to do |format|format.html { redirect_back_or_to account_path(@entry.account), notice: t(".updated") }format.turbo_stream do@@ -179,6 +186,11 @@ class TransactionsController < ApplicationControllerpartial: "transactions/notes",locals: { entry: @entry, can_annotate: can_annotate_entry? }) if params[:entry]&.key?(:notes) && notes_changed),+ (turbo_stream.replace(+ dom_id(@entry, :mark_recurring),+ partial: "transactions/mark_recurring",+ locals: { entry: @entry }+ ) if can_edit_entry? && [email protected]_child?),turbo_stream.replace(dom_id(@entry),partial: "entries/entry",@@ -189,6 +201,7 @@ class TransactionsController < ApplicationControllerendendelse+ assign_mark_recurring_staterender :show, status: :unprocessable_entityendend@@ -354,17 +367,9 @@ class TransactionsController < ApplicationControllerreturn unless require_account_permission!(transaction.entry.account)# Check if a recurring transaction already exists for this pattern.- # Amount is included so two distinct recurring payments with the same- # payee but different amounts aren't treated as duplicates (matches the- # DB uniqueness scope and RecurringTransaction::Identifier's grouping key).- existing = Current.family.recurring_transactions.find_by(- account_id: transaction.entry.account_id,- merchant_id: transaction.merchant_id,- name: transaction.merchant_id.present? ? nil : transaction.entry.name,- amount: transaction.entry.amount,- currency: transaction.entry.currency,- manual: true- )+ # The UI disables the button ahead of time using the same lookup, but this+ # guard remains as the authoritative check (e.g. stale page, direct POST).+ existing = transaction.existing_manual_recurring_transactionif existingflash[:alert] = t("recurring_transactions.already_exists")@@ -433,6 +438,23 @@ class TransactionsController < ApplicationControllerendprivate+ # The "Mark as Recurring" block is only ever rendered under these same+ # conditions (see transactions/show.html.erb), so skip the extra query+ # entirely when it won't be used — this runs on every show/failed-update+ # render, including read-only viewers and split-child transactions.+ def assign_mark_recurring_state+ return unless can_edit_entry? && [email protected]_child?++ existing = @entry.transaction.existing_manual_recurring_transaction++ @mark_recurring_href = mark_as_recurring_transaction_path(@entry.transaction)+ @mark_recurring_subtitle_class = existing ? "text-subdued" : "text-secondary"+ @mark_recurring_subtitle = existing ? t("recurring_transactions.already_exists") : t("transactions.show.mark_recurring_subtitle")+ @mark_recurring_disabled = existing.present?+ @mark_recurring_title = existing ? t("recurring_transactions.already_exists") : nil+ @mark_recurring_button_class = existing ? "disabled:opacity-50" : nil+ end+def accessible_transactionsCurrent.family.transactions.joins(entry: :account)diff --git a/app/models/transaction.rb b/app/models/transaction.rbindex 0148616f66..aa6bf88c0f 100644--- a/app/models/transaction.rb+++ b/app/models/transaction.rb@@ -188,6 +188,20 @@ class Transaction < ApplicationRecordpotential_posted_match_data.present? && !potential_duplicate_dismissed?end+ # Manual recurring transactions are unique per (family, account, merchant/name, amount, currency)+ # — see the partial unique indexes on recurring_transactions. Used to guard "mark as recurring"+ # so the UI can disable the action ahead of time instead of failing after a POST.+ def existing_manual_recurring_transaction+ entry.account.family.recurring_transactions.find_by(+ account_id: entry.account_id,+ merchant_id: merchant_id,+ name: merchant_id.present? ? nil : entry.name,+ amount: entry.amount,+ currency: entry.currency,+ manual: true+ )+ end+def potential_duplicate_entryreturn nil unless has_potential_duplicate?Entry.find_by(id: potential_posted_match_data["entry_id"])diff --git a/app/views/transactions/_mark_recurring.html.erb b/app/views/transactions/_mark_recurring.html.erbnew file mode 100644index 0000000000..46e6843120--- /dev/null+++ b/app/views/transactions/_mark_recurring.html.erb@@ -0,0 +1,20 @@+<%# locals: (entry:) %>+<div class="flex items-center justify-between gap-2 p-3" id="<%= dom_id(entry, :mark_recurring) %>">+ <div class="text-sm space-y-1">+ <h4 class="text-primary"><%= t("transactions.show.mark_recurring_title") %></h4>+ <p class="<%= @mark_recurring_subtitle_class %>">+ <%= @mark_recurring_subtitle %>+ </p>+ </div>+ <%= render DS::Button.new(+ text: t("transactions.show.mark_recurring"),+ variant: "outline",+ icon: "repeat",+ href: @mark_recurring_href,+ method: :post,+ frame: "_top",+ disabled: @mark_recurring_disabled,+ title: @mark_recurring_title,+ class: @mark_recurring_button_class+ ) %>+</div>diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erbindex 9b200f59ec..01ce13855d 100644--- a/app/views/transactions/show.html.erb+++ b/app/views/transactions/show.html.erb@@ -398,20 +398,7 @@</div><% end %><!-- Mark as Recurring Form -->- <div class="flex items-center justify-between gap-2 p-3">- <div class="text-sm space-y-1">- <h4 class="text-primary"><%= t(".mark_recurring_title") %></h4>- <p class="text-secondary"><%= t(".mark_recurring_subtitle") %></p>- </div>- <%= render DS::Button.new(- text: t(".mark_recurring"),- variant: "outline",- icon: "repeat",- href: mark_as_recurring_transaction_path(@entry.transaction),- method: :post,- frame: "_top"- ) %>- </div>+ <%= render "transactions/mark_recurring", entry: @entry %><% end %><%# Delete Transaction Form - hidden for split children %><% unless @entry.split_child? %>diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 5c5bce520a..845cba2601 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -74,6 +74,78 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTestassert_enqueued_with(job: SyncJob)end+ test "re-renders show with mark-recurring state when update fails validation" do+ family = families(:empty)+ sign_in users(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new+ merchant = family.merchants.create! name: "Test Merchant"+ entry = create_transaction(account: account, amount: 100, merchant: merchant)++ family.recurring_transactions.create!(+ account: account,+ merchant: merchant,+ amount: entry.amount,+ currency: entry.currency,+ expected_day_of_month: entry.date.day,+ last_occurrence_date: entry.date,+ next_expected_date: 1.month.from_now,+ status: "active",+ manual: true,+ occurrence_count: 1+ )++ patch transaction_url(entry), params: {+ entry: {+ name: "",+ date: entry.date,+ currency: entry.currency,+ amount: entry.amount.abs,+ nature: "outflow",+ entryable_type: entry.entryable_type,+ entryable_attributes: { id: entry.entryable_id }+ }+ }++ assert_response :unprocessable_entity+ assert_includes response.body, "A manual recurring transaction already exists for this pattern"+ assert_select "button[disabled]", text: /Mark as Recurring/+ end++ test "turbo_stream update refreshes mark-recurring state when it newly matches" do+ family = families(:empty)+ sign_in users(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new+ merchant = family.merchants.create! name: "Test Merchant"+ entry = create_transaction(account: account, amount: 100, name: "Other Name")++ family.recurring_transactions.create!(+ account: account,+ merchant: merchant,+ amount: entry.amount,+ currency: entry.currency,+ expected_day_of_month: entry.date.day,+ last_occurrence_date: entry.date,+ next_expected_date: 1.month.from_now,+ status: "active",+ manual: true,+ occurrence_count: 1+ )++ patch transaction_url(entry), params: {+ entry: {+ date: entry.date,+ currency: entry.currency,+ amount: entry.amount.abs,+ nature: "outflow",+ entryable_type: entry.entryable_type,+ entryable_attributes: { id: entry.entryable_id, merchant_id: merchant.id }+ }+ }, as: :turbo_stream++ assert_response :success+ assert_select "turbo-stream[target='#{dom_id(entry, :mark_recurring)}'] button[disabled]", text: /Mark as Recurring/+ end+test "transaction count represents filtered total" dofamily = families(:empty)sign_in users(:empty)diff --git a/test/models/transaction_test.rb b/test/models/transaction_test.rbindex 560d1040f8..ea4efcff7e 100644--- a/test/models/transaction_test.rb+++ b/test/models/transaction_test.rb@@ -1,6 +1,94 @@require "test_helper"class TransactionTest < ActiveSupport::TestCase+ include EntriesTestHelper++ test "existing_manual_recurring_transaction finds a match by merchant" do+ family = families(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new+ merchant = family.merchants.create! name: "Test Merchant"+ entry = create_transaction(account: account, amount: 100, merchant: merchant)+ transaction = entry.entryable++ recurring = family.recurring_transactions.create!(+ account: account,+ merchant: merchant,+ amount: entry.amount,+ currency: entry.currency,+ expected_day_of_month: entry.date.day,+ last_occurrence_date: entry.date,+ next_expected_date: 1.month.from_now,+ status: "active",+ manual: true,+ occurrence_count: 1+ )++ assert_equal recurring, transaction.existing_manual_recurring_transaction+ end++ test "existing_manual_recurring_transaction finds a match by name when no merchant" do+ family = families(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new+ entry = create_transaction(account: account, amount: 50, name: "Netflix")+ transaction = entry.entryable++ recurring = family.recurring_transactions.create!(+ account: account,+ name: "Netflix",+ amount: entry.amount,+ currency: entry.currency,+ expected_day_of_month: entry.date.day,+ last_occurrence_date: entry.date,+ next_expected_date: 1.month.from_now,+ status: "active",+ manual: true,+ occurrence_count: 1+ )++ assert_equal recurring, transaction.existing_manual_recurring_transaction+ end++ test "existing_manual_recurring_transaction returns nil when no manual recurring transaction matches" do+ family = families(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new+ other_account = family.accounts.create! name: "Other", balance: 0, currency: "USD", accountable: Depository.new+ merchant = family.merchants.create! name: "Test Merchant"+ other_merchant = family.merchants.create! name: "Other Merchant"+ entry = create_transaction(account: account, amount: 100, merchant: merchant)+ transaction = entry.entryable++ base_attrs = {+ expected_day_of_month: entry.date.day,+ last_occurrence_date: entry.date,+ next_expected_date: 1.month.from_now,+ status: "active",+ occurrence_count: 1+ }++ # Differs by account+ family.recurring_transactions.create!(base_attrs.merge(+ account: other_account, merchant: merchant, amount: entry.amount, currency: entry.currency, manual: true+ ))+ # Differs by merchant+ family.recurring_transactions.create!(base_attrs.merge(+ account: account, merchant: other_merchant, amount: entry.amount, currency: entry.currency, manual: true+ ))+ # Differs by amount+ family.recurring_transactions.create!(base_attrs.merge(+ account: account, merchant: merchant, amount: entry.amount + 1, currency: entry.currency, manual: true+ ))+ # Differs by currency+ family.recurring_transactions.create!(base_attrs.merge(+ account: account, merchant: merchant, amount: entry.amount, currency: "EUR", manual: true+ ))+ # Differs by manual flag+ family.recurring_transactions.create!(base_attrs.merge(+ account: account, merchant: merchant, amount: entry.amount, currency: entry.currency, manual: false+ ))++ assert_nil transaction.existing_manual_recurring_transaction+ end+test "pending? is true when extra.simplefin.pending is truthy" dotransaction = Transaction.new(extra: { "simplefin" => { "pending" => true } })