Files
sure/app/models/account_statement/coverage.rb
ghost e59235fdc5 feat(statements): add account statement vault (#1753)
* feat(statements): add account statement vault

Add web-only statement uploads, account linking, duplicate detection, and per-account coverage/reconciliation checks without mutating transactions. Extend ActiveStorage authorization and targeted tests for family/account scoping.

* fix(statements): return deleted account statements to inbox

Preserve linked statement records when an account is deleted by moving them back to the unmatched inbox, then expand coverage for upload validation, sanitized parser metadata, unavailable reconciliation, and missing-month coverage.

* fix(statements): harden vault upload review flows

Address review and security findings in the statement vault by preserving sanitized parser metadata, failing closed on orphaned statement blobs, avoiding account_id mass assignment permits, and adding regression coverage for link/delete edge cases.

* fix(statements): harden vault upload and access controls

* fix(statements): address vault hardening review

* fix(statements): address vault review feedback

Prioritize SHA-256 duplicate detection while preserving MD5 fallback for legacy rows.

Remove free-form account notes from statement matching, document direct account-destroy unlinking, and add year-selectable historical coverage with muted out-of-range months.

* fix(statements): harden vault review follow-ups

Clarify legacy MD5 checksum use, whitelist statement balance helper dispatch, and preserve sanitized parser metadata.

Hide statement management controls from read-only viewers while keeping server-side authorization unchanged.

* fix(statements): repair settings system coverage

Allow the changelog provider lookup in the self-hosting settings system test, include Statement Vault in settings navigation coverage, and align the feature title casing. Update the devcontainer so ActiveStorage and parallel system tests can run in the documented environment.

* fix(statements): move vault beside accounts

Place Statement Vault with account settings instead of between Imports and Exports. Keep settings footer ordering and system navigation coverage aligned, including the non-admin visibility guard.

* fix(statements): address vault review cleanup

Resolve CodeRabbit review feedback for statement upload validation, duplicate race handling, account statement matching semantics, metadata detection, ActiveStorage authorization tests, and small UI/style cleanups.

* fix(statements): address vault cleanup review

* fix(statements): deduplicate vault style helpers

* fix(statements): close vault review follow-ups

* fix(statements): refresh schema after upstream rebase

* fix(statements): process vault uploads sequentially

* fix(statements): close vault review follow-ups

* fix(statements): scope vault index to accessible accounts

* fix(statements): harden statement vault readiness

Squash the statement vault migration hardening into the feature migration, tighten Active Storage authorization edge cases, bound CSV metadata detection, and add real PDF fixture coverage for stored statements.

Validation: targeted statement/auth/controller/provider tests, full Rails suite, system tests, RuboCop, Biome, Brakeman, Zeitwerk, importmap audit, npm audit, ERB lint, CodeRabbit, and Codex Security all passed locally.

* fix(statements): close vault review follow-ups

Move statement unlinking to after account destroy commit, keep Kraken account creation on the shared crypto helper, and add statement metadata length limits with DB checks.

Validation: fresh devcontainer with fresh DB via db:prepare, focused account/statement/Kraken/Binance tests, RuboCop, Brakeman, Zeitwerk, git diff --check, CodeRabbit, and Codex Security passed before commit.

* fix(statements): address vault scan follow-ups

Move statement tab data setup out of the ERB partial, harden reconciliation labels and coverage initialization, and tighten statement schema constraints.

Validation: CodeRabbit and Codex Security reviewed the current PR diff; Rails focused tests, full Rails tests, system tests, RuboCop, Brakeman, Zeitwerk, ERB lint, npm lint, importmap audit, npm audit, and git diff --check passed.

* fix(statements): defer vault tab loading

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-05-13 21:05:11 +02:00

195 lines
6.4 KiB
Ruby

# frozen_string_literal: true
class AccountStatement::Coverage
Month = Struct.new(:date, :status, :statements, :ambiguous_statements, keyword_init: true) do
def expected?
status != "not_expected"
end
def covered?
status == "covered"
end
def missing?
status == "missing"
end
def duplicate?
status == "duplicate"
end
def ambiguous?
status == "ambiguous"
end
def mismatched?
status == "mismatched"
end
def not_expected?
status == "not_expected"
end
end
attr_reader :account, :start_month, :end_month, :expected_start_month, :expected_end_month, :selected_year, :available_years
class << self
def for_year(account, year)
expected_end_month = default_expected_end_month
expected_start_month = default_expected_start_month(account, fallback_end_month: expected_end_month)
available_years = years_between(expected_start_month, expected_end_month)
selected_year = resolve_year_value(year, available_years)
new(
account,
start_month: Date.new(selected_year, 1, 1),
end_month: Date.new(selected_year, 12, 1),
expected_start_month: expected_start_month,
expected_end_month: expected_end_month,
selected_year: selected_year,
available_years: available_years
)
end
def years_for(account)
expected_end_month = default_expected_end_month
expected_start_month = default_expected_start_month(account, fallback_end_month: expected_end_month)
years_between(expected_start_month, expected_end_month)
end
def resolve_year(account, year)
resolve_year_value(year, years_for(account))
end
def default_expected_end_month
Date.current.prev_month.beginning_of_month
end
def default_expected_start_month(account, fallback_end_month: default_expected_end_month)
candidates = [
account.entries.minimum(:date),
account.balances.minimum(:date),
account.account_statements.where.not(period_start_on: nil).minimum(:period_start_on),
account.family.account_statements.unmatched.where(suggested_account: account).where.not(period_start_on: nil).minimum(:period_start_on)
].compact
start_month = (candidates.min || fallback_end_month.advance(months: -11)).to_date.beginning_of_month
start_month > fallback_end_month ? fallback_end_month : start_month
end
private
def years_between(start_month, end_month)
(start_month.year..end_month.year).to_a.reverse
end
def resolve_year_value(year, available_years)
requested_year = year.to_i if year.present?
available_years.include?(requested_year) ? requested_year : available_years.first
end
end
def initialize(account, start_month: nil, end_month: nil, expected_start_month: nil, expected_end_month: nil, selected_year: nil, available_years: nil)
raise ArgumentError, "account is required" if account.nil?
@account = account
@expected_end_month = (expected_end_month || end_month || self.class.default_expected_end_month).to_date.beginning_of_month
resolved_expected_start_month = (expected_start_month || start_month || self.class.default_expected_start_month(account, fallback_end_month: @expected_end_month)).to_date.beginning_of_month
@expected_start_month = resolved_expected_start_month > @expected_end_month ? @expected_end_month : resolved_expected_start_month
@start_month = (start_month || @expected_start_month).to_date.beginning_of_month
@end_month = (end_month || @expected_end_month).to_date.beginning_of_month
@selected_year = selected_year
@available_years = available_years || self.class.years_for(account)
end
def months
@months ||= begin
current = start_month
result = []
while current <= end_month
result << build_month(current)
current = current.next_month
end
result
end
end
def summary_counts
months.group_by(&:status).transform_values(&:count)
end
private
def build_month(month)
return Month.new(date: month, status: "not_expected", statements: [], ambiguous_statements: []) unless expected_month?(month)
linked_statements = statements_covering(linked_statement_scope, month)
ambiguous_statements = statements_covering(ambiguous_statement_scope, month)
status = if linked_statements.size > 1
"duplicate"
elsif linked_statements.any? { |statement| statement.reconciliation_mismatched?(balance_lookup: balance_lookup) }
"mismatched"
elsif linked_statements.one?
"covered"
elsif ambiguous_statements.any?
"ambiguous"
else
"missing"
end
Month.new(date: month, status: status, statements: linked_statements, ambiguous_statements: ambiguous_statements)
end
def expected_month?(month)
month >= expected_start_month && month <= expected_end_month
end
def linked_statement_scope
@linked_statement_scope ||= account.account_statements
.where("period_start_on <= ? AND period_end_on >= ?", end_month.end_of_month, start_month)
.ordered
.to_a
end
def ambiguous_statement_scope
@ambiguous_statement_scope ||= account.family.account_statements
.unmatched
.where(suggested_account: account)
.where("period_start_on <= ? AND period_end_on >= ?", end_month.end_of_month, start_month)
.ordered
.to_a
end
def statements_covering(statements, month)
month_start = month.to_date.beginning_of_month
month_end = month_start.end_of_month
statements.select do |statement|
statement.period_start_on.present? &&
statement.period_end_on.present? &&
statement.period_start_on <= month_end &&
statement.period_end_on >= month_start
end
end
def balance_lookup
@balance_lookup ||= begin
currencies = linked_statement_scope.map(&:statement_currency).compact.uniq
dates = linked_statement_scope.flat_map { |statement| [ statement.period_start_on, statement.period_end_on ] }.compact.uniq
balances = if currencies.any? && dates.any?
account.balances.where(currency: currencies, date: dates).to_a
else
[]
end
by_key = balances.index_by { |balance| [ balance.date, balance.currency ] }
->(date, currency) { by_key[[ date, currency ]] }
end
end
end