Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

List account statements by period instead of upload order (#5512)

Artur Kalimullin05c8d6270656
diff --git a/app/controllers/account_statements_controller.rb b/app/controllers/account_statements_controller.rb
index a01ec76377..94b93f1854 100644
--- a/app/controllers/account_statements_controller.rb
+++ b/app/controllers/account_statements_controller.rb
@@ -9,13 +9,12 @@ class AccountStatementsController < ApplicationController
account_statements = Current.family.account_statements
.with_attached_original_file
.includes(:account, :suggested_account)
- .ordered
visible_storage_scope = Current.family.account_statements
.where(account_id: nil)
.or(Current.family.account_statements.where(account_id: accessible_account_ids))
- linked_statement_scope = account_statements.with_account.where(account_id: accessible_account_ids)
+ linked_statement_scope = account_statements.with_account.where(account_id: accessible_account_ids).by_period
- @unmatched_pagy, @unmatched_statements = pagy(account_statements.inbox, limit: safe_per_page, page_param: :unmatched_page)
+ @unmatched_pagy, @unmatched_statements = pagy(account_statements.inbox.ordered, limit: safe_per_page, page_param: :unmatched_page)
@linked_pagy, @linked_statements = pagy(linked_statement_scope, limit: safe_per_page, page_param: :linked_page)
@total_storage_bytes = visible_storage_scope.sum(:byte_size)
@accounts = Current.user.accessible_accounts.visible.alphabetically
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index 95d92c9694..578572ff2f 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -601,7 +601,7 @@ class AccountsController < ApplicationController
return unless statement_tab_active?
@statement_coverage = AccountStatement::Coverage.for_year(@account, params[:statement_year])
- @account_statements = @account.account_statements.with_attached_original_file.ordered.to_a
+ @account_statements = @account.account_statements.with_attached_original_file.by_period.to_a
@statement_reconciliation_statuses = AccountStatement.reconciliation_statuses_for(@account_statements, account: @account)
permission = @account.permission_for(Current.user)
@can_manage_statements = AccountStatement.statement_manager?(Current.user) &&
diff --git a/app/models/account_statement.rb b/app/models/account_statement.rb
index decb6d8617..a0ac4acc19 100644
--- a/app/models/account_statement.rb
+++ b/app/models/account_statement.rb
@@ -66,6 +66,18 @@ class AccountStatement < ApplicationRecord
validate :original_file_constraints, if: -> { original_file.attached? }
scope :ordered, -> { order(created_at: :desc) }
+ # Statements with a known period first, newest period first. A period
+ # counts as known only with both ends, as account_statement_period shows
+ # it; the rest follow in upload order, which also breaks ties.
+ scope :by_period, -> {
+ known = "account_statements.period_start_on IS NOT NULL AND account_statements.period_end_on IS NOT NULL"
+ order(
+ Arel.sql("CASE WHEN #{known} THEN 0 ELSE 1 END"),
+ Arel.sql("CASE WHEN #{known} THEN account_statements.period_end_on END DESC"),
+ Arel.sql("CASE WHEN #{known} THEN account_statements.period_start_on END DESC"),
+ created_at: :desc
+ )
+ }
scope :with_account, -> { where.not(account_id: nil) }
scope :unmatched, -> { where(account_id: nil).where(review_status: "unmatched") }
# Rejecting a suggestion leaves the statement accountless, so it stays in the inbox where it can be found and deleted.
diff --git a/test/controllers/account_statements_controller_test.rb b/test/controllers/account_statements_controller_test.rb
index 4f1f3e4d6b..3450b2b3e9 100644
--- a/test/controllers/account_statements_controller_test.rb
+++ b/test/controllers/account_statements_controller_test.rb
@@ -500,4 +500,32 @@ class AccountStatementsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to account_url(@account, tab: "statements")
assert_equal I18n.t("account_statements.destroy.failure"), flash[:alert]
end
+
+ test "statement vault lists linked statements by period and the inbox by upload" do
+ february = statement_for(account: @account, month: Date.new(2024, 2, 1), name: "february")
+ april = statement_for(account: @account, month: Date.new(2024, 4, 1), name: "april")
+ march = statement_for(account: @account, month: Date.new(2024, 3, 1), name: "march")
+ older_upload = statement_for(account: nil, month: Date.new(2024, 4, 1), name: "older_upload")
+ newer_upload = statement_for(account: nil, month: Date.new(2024, 2, 1), name: "newer_upload")
+ older_upload.update_columns(created_at: 2.days.ago)
+ newer_upload.update_columns(created_at: 1.day.ago)
+
+ get account_statements_url
+
+ assert_response :success
+ assert_equal [ newer_upload, older_upload, april, march, february ].map { |statement| dom_id(statement) },
+ css_select("tr[id^='account_statement_']").map { |row| row["id"] }
+ end
+
+ private
+
+ def statement_for(account:, month:, name:)
+ statement = AccountStatement.create_from_upload!(
+ family: @account.family,
+ account: account,
+ file: uploaded_file(filename: "#{name}.csv", content_type: "text/csv", content: "date,amount\n#{month},1\n#{name}\n")
+ )
+ statement.update!(period_start_on: month, period_end_on: month.end_of_month)
+ statement
+ end
end
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index 16d4ad1ce1..7fae2ca5c2 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -1209,6 +1209,24 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_select "turbo-frame##{dom_id(@account, :container)}", count: 0
end
+ test "statements tab lists statements by period" do
+ statements = [ Date.new(2024, 2, 1), Date.new(2024, 4, 1), Date.new(2024, 3, 1) ].map do |month|
+ statement = AccountStatement.create_from_upload!(
+ family: @account.family,
+ account: @account,
+ file: uploaded_file(filename: "statement_#{month}.csv", content_type: "text/csv", content: "date,amount\n#{month},1\n")
+ )
+ statement.update!(period_start_on: month, period_end_on: month.end_of_month)
+ statement
+ end
+
+ get account_url(@account, tab: "statements")
+
+ assert_response :success
+ assert_equal statements.sort_by(&:period_end_on).reverse.map { |statement| dom_id(statement) },
+ css_select("tr[id^='account_statement_']").map { |row| row["id"] }
+ end
+
test "statements tab filters historical coverage by year" do
account = Account.create!(
family: @user.family,
diff --git a/test/models/account_statement_test.rb b/test/models/account_statement_test.rb
index 9f29ac7435..45dd1fc012 100644
--- a/test/models/account_statement_test.rb
+++ b/test/models/account_statement_test.rb
@@ -1133,6 +1133,30 @@ class AccountStatementTest < ActiveSupport::TestCase
assert_equal "covered", coverage.months.first.status
end
+ test "by_period lists the newest period first and statements without a period last" do
+ march = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 3, 1), period_end_on: Date.new(2024, 3, 31), content: "march")
+ unread = create_statement_with_period(account: @account, period_start_on: nil, period_end_on: nil, content: "unread")
+ january = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 1, 1), period_end_on: Date.new(2024, 1, 31), content: "january")
+ quarter = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 1, 1), period_end_on: Date.new(2024, 3, 31), content: "quarter")
+ january_again = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 1, 1), period_end_on: Date.new(2024, 1, 31), content: "january_again")
+ january.update_columns(created_at: 2.days.ago)
+ january_again.update_columns(created_at: 1.day.ago)
+
+ assert_equal [ march, quarter, january_again, january, unread ], @account.account_statements.by_period.to_a
+ end
+
+ test "by_period lists a statement with only one end of its period with the unknown periods, by upload" do
+ march = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 3, 1), period_end_on: Date.new(2024, 3, 31), content: "march")
+ unread = create_statement_with_period(account: @account, period_start_on: nil, period_end_on: nil, content: "unread")
+ end_only = create_statement_with_period(account: @account, period_start_on: nil, period_end_on: Date.new(2024, 4, 30), content: "end_only")
+ start_only = create_statement_with_period(account: @account, period_start_on: Date.new(2024, 5, 1), period_end_on: nil, content: "start_only")
+ unread.update_columns(created_at: 3.hours.ago)
+ end_only.update_columns(created_at: 2.hours.ago)
+ start_only.update_columns(created_at: 1.hour.ago)
+
+ assert_equal [ march, start_only, end_only, unread ], @account.account_statements.by_period.to_a
+ end
+
private
def upload_card_statement(account:, text: CARD_STATEMENT_TEXT)