The personal finance app for everyone (by everyone)
Ruby
Keep loan terms and credit card details through a backup and restore (#5060)
arnaurodondevb10eba23352c
diff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rbindex ac9c0c3c61..946a61e329 100644--- a/app/models/family/data_importer.rb+++ b/app/models/family/data_importer.rb@@ -38,6 +38,14 @@ class Family::DataImporter"CreditCard" => CreditCard, "Loan" => Loan, "OtherLiability" => OtherLiability}.freeze+ # What the loan and credit card forms edit, restored on import. The loan's+ # rate schedule is restored separately (see assign_liability_details).+ LOAN_DETAIL_ATTRIBUTES = %w[+ interest_rate rate_type term_months start_date down_payment initial_balance+ insurance_rate insurance_rate_type day_count_convention principal_only_transfers+ ].freeze+ CREDIT_CARD_DETAIL_ATTRIBUTES = %w[apr annual_fee minimum_payment available_credit due_day statement_close_day expiration_date].freeze+def self.accountable_class_for(type)ACCOUNTABLE_TYPE_CLASSES[type.to_s]end@@ -285,6 +293,25 @@ class Family::DataImporteraccount.entries.find_by(source: session_entry_source, external_id: external_id)end+ # A missing or null value keeps the column's default, so older exports,+ # which carry none of these, still import.+ def assign_liability_details(accountable, accountable_data)+ attributes = case accountable+ when Loan then LOAN_DETAIL_ATTRIBUTES+ when CreditCard then CREDIT_CARD_DETAIL_ATTRIBUTES+ end+ return unless attributes++ accountable.assign_attributes(accountable_data.slice(*attributes).compact)++ # Through the form's own parser, so a schedule the loan could not read+ # back is rejected instead of stored.+ schedule = accountable_data["variable_rate_schedule"]+ if accountable.is_a?(Loan) && schedule.is_a?(Hash)+ accountable.rate_changes = schedule.map { |date, rate| { "effective_date" => date, "rate" => rate } }+ end+ end+def import_accounts(records)records.each do |record|data = record["data"]@@ -310,6 +337,7 @@ class Family::DataImporter# Build accountableaccountable = accountable_class.newaccountable.subtype = accountable_data["subtype"] if accountable.respond_to?(:subtype=) && accountable_data["subtype"]+ assign_liability_details(accountable, accountable_data)# Copy any other accountable attributessafe_accountable_attrs = %w[subtype locked_attributes]diff --git a/test/models/family/data_importer_test.rb b/test/models/family/data_importer_test.rbindex 3b12cf2219..a106a447cc 100644--- a/test/models/family/data_importer_test.rb+++ b/test/models/family/data_importer_test.rb@@ -31,6 +31,130 @@ class Family::DataImporterTest < ActiveSupport::TestCaseassert_equal "Depository", account.accountable_typeend+ test "round trips loan terms through full export" do+ source_family = Family.create!(name: "Source Loan Family", currency: "EUR", locale: "en", date_format: "%Y-%m-%d")+ source_family.accounts.create!(+ name: "Round Trip Land Loan",+ balance: 18_000,+ currency: "USD",+ accountable: Loan.new(+ subtype: "other",+ interest_rate: 7.25,+ rate_type: "variable",+ term_months: 120,+ start_date: Date.new(2025, 3, 1),+ down_payment: 4_000,+ initial_balance: 20_000,+ insurance_rate: 0.35,+ insurance_rate_type: "level_term",+ day_count_convention: "actual_365",+ principal_only_transfers: true,+ variable_rate_schedule: { "2026-01-01" => "6.5" }+ )+ )++ Family::DataImporter.new(@family, full_export_ndjson(source_family)).import!++ loan = @family.accounts.find_by!(name: "Round Trip Land Loan").accountable+ assert_equal "other", loan.subtype+ assert_equal BigDecimal("7.25"), loan.interest_rate+ assert_equal "variable", loan.rate_type+ assert_equal 120, loan.term_months+ assert_equal Date.new(2025, 3, 1), loan.start_date+ assert_equal BigDecimal("4000"), loan.down_payment+ assert_equal BigDecimal("20000"), loan.initial_balance+ assert_equal BigDecimal("0.35"), loan.insurance_rate+ assert_equal "level_term", loan.insurance_rate_type+ assert_equal "actual_365", loan.day_count_convention+ assert loan.principal_only_transfers+ assert_equal [ [ Date.new(2026, 1, 1), BigDecimal("6.5") ] ], loan.variable_rates+ end++ test "round trips credit card details through full export" do+ source_family = Family.create!(name: "Source Card Family", currency: "EUR", locale: "en", date_format: "%Y-%m-%d")+ source_family.accounts.create!(+ name: "Round Trip Card",+ balance: 350,+ currency: "EUR",+ accountable: CreditCard.new(+ subtype: "credit_card",+ apr: 19.9,+ annual_fee: 45,+ minimum_payment: 25,+ available_credit: 2_650,+ due_day: 5,+ statement_close_day: 20,+ expiration_date: Date.new(2029, 8, 31)+ )+ )++ Family::DataImporter.new(@family, full_export_ndjson(source_family)).import!++ card = @family.accounts.find_by!(name: "Round Trip Card").accountable+ assert_equal BigDecimal("19.9"), card.apr+ assert_equal BigDecimal("45"), card.annual_fee+ assert_equal BigDecimal("25"), card.minimum_payment+ assert_equal BigDecimal("2650"), card.available_credit+ assert_equal 5, card.due_day+ assert_equal 20, card.statement_close_day+ assert_equal Date.new(2029, 8, 31), card.expiration_date+ end++ test "imports loans and credit cards from an export without their details" do+ ndjson = build_ndjson([+ { type: "Account", data: { id: "old-loan", name: "Old Loan", balance: "1000", currency: "USD", subtype: "auto", accountable_type: "Loan", accountable: { subtype: "auto" } } },+ { type: "Account", data: { id: "old-card", name: "Old Card", balance: "100", currency: "USD", accountable_type: "CreditCard", accountable: { subtype: "credit_card" } } }+ ])++ Family::DataImporter.new(@family, ndjson).import!++ loan = @family.accounts.find_by!(name: "Old Loan").accountable+ assert_equal "auto", loan.subtype+ assert_nil loan.interest_rate+ assert_equal Loan::DEFAULT_DAY_COUNT_CONVENTION, loan.day_count_convention+ assert_not loan.principal_only_transfers+ assert_equal({}, loan.variable_rate_schedule)++ card = @family.accounts.find_by!(name: "Old Card").accountable+ assert_nil card.apr+ assert_nil card.due_day+ end++ test "imports a loan whose detail columns are null in the export" do+ ndjson = build_ndjson([+ {+ type: "Account",+ data: {+ id: "old-loan", name: "Null Loan", balance: "1000", currency: "USD", accountable_type: "Loan",+ accountable: { interest_rate: "5.0", day_count_convention: nil, principal_only_transfers: nil, variable_rate_schedule: nil, start_date: nil }+ }+ }+ ])++ Family::DataImporter.new(@family, ndjson).import!++ loan = @family.accounts.find_by!(name: "Null Loan").accountable+ assert_equal BigDecimal("5"), loan.interest_rate+ assert_equal Loan::DEFAULT_DAY_COUNT_CONVENTION, loan.day_count_convention+ assert_not loan.principal_only_transfers+ assert_equal({}, loan.variable_rate_schedule)+ end++ test "rejects a loan rate schedule that cannot be read" do+ ndjson = build_ndjson([+ {+ type: "Account",+ data: {+ id: "old-loan", name: "Bad Schedule Loan", balance: "1000", currency: "USD", accountable_type: "Loan",+ accountable: { rate_type: "variable", interest_rate: "5", variable_rate_schedule: { "not a date" => "6" } }+ }+ }+ ])++ assert_raises(ActiveRecord::RecordInvalid) { Family::DataImporter.new(@family, ndjson).import! }+ assert_not @family.accounts.exists?(name: "Bad Schedule Loan")+ end+test "imports into existing accounts given as account targets" doaccount = @family.accounts.create!(name: "Existing", balance: 0, currency: "USD", accountable: Depository.new)ndjson = build_ndjson([@@ -3301,6 +3425,12 @@ class Family::DataImporterTest < ActiveSupport::TestCaserecords.map(&:to_json).join("\n")end+ def full_export_ndjson(family)+ ndjson = nil+ Zip::File.open_buffer(Family::DataExporter.new(family).generate_export) { |zip| ndjson = zip.read("all.ndjson") }+ ndjson+ end+# A bill, one non-monthly rule, a settled occurrence, and the payment that# settled it, pointing at a transaction arriving in the same export.def bills_ndjson_records