Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Add a merchant page with its transactions across accounts (#4537)

Dov Benyomin Sohacheski9fb2c2535161
diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb
new file mode 100644
index 0000000000..21ec10fea2
--- /dev/null
+++ b/app/controllers/merchants_controller.rb
@@ -0,0 +1,54 @@
+class MerchantsController < ApplicationController
+ def show
+ @merchant = find_merchant
+
+ @breadcrumbs = [
+ [ t("breadcrumbs.home"), root_path ],
+ [ t("breadcrumbs.merchants"), family_merchants_path ],
+ [ @merchant.name, nil ]
+ ]
+
+ @accessible_account_ids = Current.user.accessible_accounts.pluck(:id)
+ @search = merchant_search
+ @expense_totals = merchant_search(types: [ "expense" ]).totals
+ @average_expense_money = @expense_totals.expense_money / @expense_totals.count if @expense_totals.count.positive?
+ @first_date, @last_date = @search.transactions_scope.pick(Arel.sql("MIN(entries.date)"), Arel.sql("MAX(entries.date)"))
+
+ @pagy, @transactions = pagy(
+ @search.transactions_scope.reverse_chronological.includes(
+ { entry: :account }, :category, :merchant, :tags,
+ { transfer_as_outflow: { inflow_transaction: { entry: :account } } },
+ { transfer_as_inflow: { inflow_transaction: { entry: :account }, outflow_transaction: [ :category, { entry: :account } ] } }
+ ),
+ limit: safe_per_page(50)
+ )
+ Transaction::ActivitySecurityPreloader.new(@transactions).preload
+
+ @split_parents = load_split_parents
+ @split_parent_entry_ids = Entry.split_parent_ids_among(@transactions.map { |t| t.entry.id })
+ @projected_recurring = []
+ @q = {}
+ end
+
+ private
+ def find_merchant
+ Current.family.available_merchants_for(Current.user).find(params[:id])
+ end
+
+ def merchant_search(**filters)
+ Transaction::Search.new(
+ Current.family,
+ filters: { merchants: [ @merchant.name ], **filters },
+ accessible_account_ids: @accessible_account_ids
+ )
+ end
+
+ def load_split_parents
+ return {} unless Current.user.show_split_grouped?
+
+ parent_ids = @transactions.filter_map { |t| t.entry.parent_entry_id }.uniq
+ return {} if parent_ids.empty?
+
+ Entry.where(id: parent_ids).includes(:account, entryable: [ :category, :merchant ]).index_by(&:id)
+ end
+end
diff --git a/app/views/family_merchants/_family_merchant.html.erb b/app/views/family_merchants/_family_merchant.html.erb
index bea7b683a7..658442ffb7 100644
--- a/app/views/family_merchants/_family_merchant.html.erb
+++ b/app/views/family_merchants/_family_merchant.html.erb
@@ -11,9 +11,7 @@
<%= render partial: "shared/color_avatar", locals: { name: family_merchant.name, color: family_merchant.color } %>
<% end %>
- <p class="font-medium truncate">
- <%= family_merchant.name %>
- </p>
+ <%= link_to family_merchant.name, merchant_path(family_merchant), class: "font-medium truncate hover:underline" %>
</div>
</td>
<% unless Current.user&.guest? %>
diff --git a/app/views/family_merchants/_provider_merchant.html.erb b/app/views/family_merchants/_provider_merchant.html.erb
index 53773ce58d..e5826b8f58 100644
--- a/app/views/family_merchants/_provider_merchant.html.erb
+++ b/app/views/family_merchants/_provider_merchant.html.erb
@@ -13,9 +13,7 @@
</div>
<% end %>
- <p class="font-medium truncate">
- <%= provider_merchant.name %>
- </p>
+ <%= link_to provider_merchant.name, merchant_path(provider_merchant), class: "font-medium truncate hover:underline" %>
</div>
</td>
<td class="py-3 px-4 text-sm text-secondary align-middle">
diff --git a/app/views/merchants/show.html.erb b/app/views/merchants/show.html.erb
new file mode 100644
index 0000000000..3698aeebbf
--- /dev/null
+++ b/app/views/merchants/show.html.erb
@@ -0,0 +1,41 @@
+<% totals = @search.totals %>
+
+<div class="space-y-4 pb-6 lg:pb-12 flex flex-col">
+ <header class="flex items-center justify-between gap-4 text-primary">
+ <div class="flex items-center gap-3 min-w-0">
+ <% if @merchant.logo_url.present? %>
+ <%= image_tag Setting.transform_brand_fetch_url(@merchant.logo_url), class: "w-9 h-9 rounded-full border border-secondary", alt: "" %>
+ <% else %>
+ <%= render DS::FilledIcon.new(variant: :text, text: @merchant.name, hex_color: @merchant.color, size: "lg", rounded: true) %>
+ <% end %>
+ <h1 class="text-xl font-medium truncate"><%= @merchant.name %></h1>
+ </div>
+
+ <% unless Current.user.guest? %>
+ <%= render DS::Link.new(
+ text: t(".edit"),
+ icon: "pencil",
+ variant: "outline",
+ href: edit_family_merchant_path(@merchant),
+ frame: :modal
+ ) %>
+ <% end %>
+ </header>
+
+ <%= render DS::Card.new(class: "gap-3") do %>
+ <h2 class="text-base font-medium text-primary"><%= t(".summary") %></h2>
+ <dl class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-x-10 gap-y-3">
+ <%= render DS::DetailRow.new(label: t(".total_transactions"), value: totals.count.to_s, align: :center, value_class: "privacy-sensitive") %>
+ <%= render DS::DetailRow.new(label: t(".total_spending"), value: format_money(@expense_totals.expense_money), align: :center, value_class: "privacy-sensitive") %>
+ <% if @average_expense_money %>
+ <%= render DS::DetailRow.new(label: t(".average_transaction"), value: format_money(@average_expense_money), align: :center, value_class: "privacy-sensitive") %>
+ <% end %>
+ <% if @first_date %>
+ <%= render DS::DetailRow.new(label: t(".first_transaction"), value: l(@first_date, format: :long), align: :center) %>
+ <%= render DS::DetailRow.new(label: t(".last_transaction"), value: l(@last_date, format: :long), align: :center) %>
+ <% end %>
+ </dl>
+ <% end %>
+
+ <%= render "transactions/list", transactions: @transactions, projected_recurring: @projected_recurring, q: @q, pagy: @pagy, toolbar: false %>
+</div>
diff --git a/app/views/transactions/_list.html.erb b/app/views/transactions/_list.html.erb
index e7aed3e134..0ca0f64db0 100644
--- a/app/views/transactions/_list.html.erb
+++ b/app/views/transactions/_list.html.erb
@@ -1,20 +1,31 @@
-<%# locals: (transactions:, projected_recurring:, q:, pagy:) %>
+<%# locals: (transactions:, projected_recurring:, q:, pagy:, toolbar: true) %>
<div id="transactions"
- data-controller="bulk-select checkbox-toggle drag-and-drop-import"
+ data-controller="bulk-select checkbox-toggle<%= " drag-and-drop-import" if toolbar %>"
data-bulk-select-singular-label-value="<%= t(".transaction") %>"
data-bulk-select-plural-label-value="<%= t(".transactions") %>"
data-bulk-select-selected-label-value="<%= t("transactions.selection_bar.selected") %>"
data-bulk-select-edit-label-value="<%= t("transactions.selection_bar.edit") %>"
class="flex flex-col bg-container rounded-xl shadow-border-xs px-3 py-4 lg:p-4 relative group">
- <%= form_with url: imports_path, method: :post, class: "hidden", data: { drag_and_drop_import_target: "form", turbo: false } do |f| %>
- <%= f.hidden_field "import[type]", value: "TransactionImport" %>
- <%= f.file_field "import[import_file]", class: "hidden", data: { drag_and_drop_import_target: "input" }, accept: ".csv" %>
- <% end %>
+ <% if toolbar %>
+ <%= form_with url: imports_path, method: :post, class: "hidden", data: { drag_and_drop_import_target: "form", turbo: false } do |f| %>
+ <%= f.hidden_field "import[type]", value: "TransactionImport" %>
+ <%= f.file_field "import[import_file]", class: "hidden", data: { drag_and_drop_import_target: "input" }, accept: ".csv" %>
+ <% end %>
- <%= render "imports/drag_drop_overlay", title: t(".drag_drop_title"), subtitle: t(".drag_drop_subtitle") %>
+ <%= render "imports/drag_drop_overlay", title: t(".drag_drop_title"), subtitle: t(".drag_drop_subtitle") %>
- <%= render "transactions/searches/search" %>
+ <%= render "transactions/searches/search" %>
+ <% else %>
+ <div class="flex justify-end mb-4 lg:hidden">
+ <%= render DS::Button.new(
+ variant: :outline,
+ icon: "list-todo",
+ aria: { label: t("transactions.searches.form.toggle_selection_checkboxes") },
+ data: { action: "click->checkbox-toggle#toggle", checkbox_toggle_target: "toggleButton" }
+ ) %>
+ </div>
+ <% end %>
<div id="entry-selection-bar" data-bulk-select-target="selectionBar" class="flex justify-center hidden">
<%= render "transactions/selection_bar" %>
diff --git a/config/locales/views/merchants/en.yml b/config/locales/views/merchants/en.yml
index 8d52abf570..d96182d94f 100644
--- a/config/locales/views/merchants/en.yml
+++ b/config/locales/views/merchants/en.yml
@@ -83,3 +83,12 @@ en:
update:
success: Merchant updated successfully
converted_success: Merchant converted and updated successfully
+ merchants:
+ show:
+ average_transaction: Average transaction
+ edit: Edit merchant
+ first_transaction: First transaction
+ last_transaction: Last transaction
+ summary: Summary
+ total_spending: Total spending
+ total_transactions: Total transactions
diff --git a/config/routes.rb b/config/routes.rb
index 0281b5fbfc..96fe6db0a2 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -504,6 +504,8 @@ Rails.application.routes.draw do
end
end
+ resources :merchants, only: :show
+
resources :family_merchants, only: %i[index new create edit update destroy] do
collection do
get :merge
diff --git a/test/controllers/merchants_controller_test.rb b/test/controllers/merchants_controller_test.rb
new file mode 100644
index 0000000000..92472aa798
--- /dev/null
+++ b/test/controllers/merchants_controller_test.rb
@@ -0,0 +1,75 @@
+require "test_helper"
+
+class MerchantsControllerTest < ActionDispatch::IntegrationTest
+ include EntriesTestHelper
+
+ setup do
+ sign_in @user = users(:family_admin)
+ @merchant = merchants(:netflix)
+ end
+
+ test "lists the merchant's transactions from every account" do
+ create_transaction(account: accounts(:depository), name: "Netflix checking", merchant: @merchant)
+ create_transaction(account: accounts(:credit_card), name: "Netflix card", merchant: @merchant)
+ create_transaction(account: accounts(:depository), name: "Amazon order", merchant: merchants(:amazon))
+
+ get merchant_url(@merchant)
+
+ assert_response :success
+ assert_match "Netflix checking", response.body
+ assert_match "Netflix card", response.body
+ assert_no_match "Amazon order", response.body
+ end
+
+ test "shows a provider merchant assigned to the family's transactions" do
+ provider = ProviderMerchant.create!(name: "Coffee Co", source: "plaid")
+ create_transaction(account: accounts(:depository), name: "Morning coffee", merchant: provider)
+
+ get merchant_url(provider)
+
+ assert_response :success
+ assert_match "Morning coffee", response.body
+ end
+
+ test "leaves out transactions from accounts the user cannot access" do
+ create_transaction(account: accounts(:investment), name: "Private Netflix", merchant: @merchant)
+ create_transaction(account: accounts(:depository), name: "Shared Netflix", merchant: @merchant)
+
+ sign_in users(:family_member)
+ get merchant_url(@merchant)
+
+ assert_response :success
+ assert_match "Shared Netflix", response.body
+ assert_no_match "Private Netflix", response.body
+ end
+
+ test "averages spending over purchases only" do
+ create_transaction(account: accounts(:depository), name: "Netflix plan", merchant: @merchant, amount: 100)
+ create_transaction(account: accounts(:depository), name: "Netflix refund", merchant: @merchant, amount: -100)
+
+ get merchant_url(@merchant)
+
+ assert_response :success
+ assert_select "dt", text: "Average transaction" do |dt|
+ assert_equal "$100.00", dt.first.next_element.text.strip
+ end
+ end
+
+ test "does not show a provider merchant used only on accounts the user cannot access" do
+ provider = ProviderMerchant.create!(name: "Private Co", source: "plaid")
+ create_transaction(account: accounts(:investment), name: "Private purchase", merchant: provider)
+
+ sign_in users(:family_member)
+ get merchant_url(provider)
+
+ assert_response :not_found
+ end
+
+ test "does not show another family's merchant" do
+ other = families(:empty).merchants.create!(name: "Elsewhere")
+
+ get merchant_url(other)
+
+ assert_response :not_found
+ end
+end