The personal finance app for everyone (by everyone)
Ruby
Finish a committed Monarch import on retry instead of reimporting (#4832)
jezmor7c76032b8924
diff --git a/app/controllers/concerns/import_guest_guardable.rb b/app/controllers/concerns/import_guest_guardable.rbindex bb119a607b..17b7efc3a3 100644--- a/app/controllers/concerns/import_guest_guardable.rb+++ b/app/controllers/concerns/import_guest_guardable.rb@@ -1,10 +1,13 @@# The import wizard's steps change an import before it is published. Guests-# may only change their own kind of import, and only before publishing.+# may only change their own kind of import, and only before publishing. No one+# may change an import whose configuration is locked.module ImportGuestGuardableextend ActiveSupport::Concernprivatedef require_import_editable!- require_non_guest! unless @import.editable_by?(Current.user)+ return require_non_guest! unless @import.editable_by?(Current.user)++ redirect_to import_path(@import), alert: t("imports.configuration_locked") if @import.configuration_locked?endenddiff --git a/app/models/import.rb b/app/models/import.rbindex c86af9a330..c423475fc7 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -482,6 +482,12 @@ class Import < ApplicationRecordcomplete? || revert_failed?end+ # Whether the wizard's upload and configuration steps are closed even to+ # someone who may edit the import. Subclasses that need this override it.+ def configuration_locked?+ false+ end+def family_config?FAMILY_CONFIG_TYPES.include?(type)enddiff --git a/app/models/monarch_import.rb b/app/models/monarch_import.rbindex 334610f3ef..c9004897e8 100644--- a/app/models/monarch_import.rb+++ b/app/models/monarch_import.rb@@ -149,6 +149,12 @@ class MonarchImport < Importsuper || changed_existing_accounts?end+ # Once data has committed, a retry only finishes publishing, so changed+ # exports or choices would be ignored. Undoing the import unlocks them.+ def configuration_locked?+ data_committed?+ end+# A lost job may have died after its transaction committed. Then the import# is done, as Import.clean decides, and failing it would let a retry import# everything again.@@ -231,6 +237,11 @@ class MonarchImport < Importenddef import!+ # A retry after the data committed, when a later publishing step failed,+ # only has to finish publishing. Importing again would replace the saved+ # values that undoing the import restores.+ return if data_committed?+result = conversiontransaction dodiff --git a/config/locales/views/imports/en.yml b/config/locales/views/imports/en.ymlindex 4c840761f1..c3fd0c27fd 100644--- a/config/locales/views/imports/en.yml+++ b/config/locales/views/imports/en.yml@@ -298,6 +298,7 @@ en:hint_html: Upload the <strong>all.ndjson</strong> file from your data export ZIPndjson_invalid: Must be valid NDJSON with at least one recordimports:+ configuration_locked: "This import's data is already in Sure, so its files and choices can't change. Try again to finish it, or delete it to start over."mapping_labels:account_type: "Account Type"account: "Account"diff --git a/test/controllers/import/monarch_import_flow_test.rb b/test/controllers/import/monarch_import_flow_test.rbindex e0febc6252..1298294252 100644--- a/test/controllers/import/monarch_import_flow_test.rb+++ b/test/controllers/import/monarch_import_flow_test.rb@@ -113,6 +113,23 @@ class Import::MonarchImportFlowTest < ActionDispatch::IntegrationTestassert_nil checking.reload.targetend+ test "an import whose data committed before failing can't change its files or choices" do+ upload_exports+ Family.any_instance.stubs(:sync_later).raises(StandardError, "queue down")+ @import.publish+ assert @import.reload.failed?+ checking = @import.account_mappings.find_by!(key: "Checking")++ patch import_configuration_url(@import), params: { accounts: { checking.id => { type: "Depository", include: "0" } } }+ assert_redirected_to import_url(@import)+ assert_equal I18n.t("imports.configuration_locked"), flash[:alert]+ assert checking.reload.include++ patch import_upload_url(@import), params: { import: { balances_file: csv_upload("monarch_balances.csv") } }+ assert_redirected_to import_url(@import)+ assert_equal 3, @import.account_mappings.count+ end+test "confirm shows the summary and publishes" doupload_exportsget import_url(@import)diff --git a/test/models/monarch_import_test.rb b/test/models/monarch_import_test.rbindex 0d4de4cd64..325827ec4c 100644--- a/test/models/monarch_import_test.rb+++ b/test/models/monarch_import_test.rb@@ -85,6 +85,31 @@ class MonarchImportTest < ActiveSupport::TestCaseassert_equal [ Date.new(2024, 1, 3), 500 ], [ opening.reload.date, opening.amount ]end+ test "retrying an import whose data committed before publishing failed keeps what undo restores" do+ account = @family.accounts.create!(name: "My Checking", balance: 500, currency: "USD", accountable: Depository.new)+ opening = account.entries.create!(date: Date.new(2024, 1, 3), amount: 500, currency: "EUR", name: "Opening balance",+ entryable: Valuation.new(kind: "opening_anchor"))+ ExchangeRate.create!(date: Date.new(2024, 1, 3), from_currency: "EUR", to_currency: "USD", rate: 1)+ account.disable!+ disabled_at = account.reload.disabled_at+ mapping("Checking").update!(mappable: account, choices: { "closed" => false })+ Family.any_instance.stubs(:sync_later).raises(StandardError, "queue down").then.returns(nil)++ @import.reload.publish+ assert @import.reload.failed?+ assert @import.data_committed?+ entry_ids = @import.entries.pluck(:id).sort++ publish!+ assert_equal entry_ids, @import.entries.pluck(:id).sort+ assert account.reload.active?++ @import.revert+ assert_equal [ Date.new(2024, 1, 3), 500, "EUR" ], [ opening.reload.date, opening.amount, opening.currency ]+ assert account.reload.disabled?+ assert_in_delta disabled_at, account.disabled_at, 1.second+ end+test "an opening balance in another currency moves in the account's and comes back in its own" doaccount = @family.accounts.create!(name: "My Checking", balance: 500, currency: "USD", accountable: Depository.new)opening = account.entries.create!(date: Date.new(2024, 1, 3), amount: 500, currency: "EUR", name: "Opening balance",