mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Feature: Add the ability to "revert" a CSV import (#1814)
* Allow reverting imports * Fix tests * Add currency column to all imports * Don't auto-enrich demo account
This commit is contained in:
parent
60925bd16c
commit
536c82f2aa
17 changed files with 125 additions and 6 deletions
|
@ -6,7 +6,14 @@ class Import < ApplicationRecord
|
|||
|
||||
scope :ordered, -> { order(created_at: :desc) }
|
||||
|
||||
enum :status, { pending: "pending", complete: "complete", importing: "importing", failed: "failed" }, validate: true
|
||||
enum :status, {
|
||||
pending: "pending",
|
||||
complete: "complete",
|
||||
importing: "importing",
|
||||
reverting: "reverting",
|
||||
revert_failed: "revert_failed",
|
||||
failed: "failed"
|
||||
}, validate: true, default: "pending"
|
||||
|
||||
validates :type, inclusion: { in: TYPES }
|
||||
validates :col_sep, inclusion: { in: [ ",", ";" ] }
|
||||
|
@ -35,6 +42,27 @@ class Import < ApplicationRecord
|
|||
update! status: :failed, error: error.message
|
||||
end
|
||||
|
||||
def revert_later
|
||||
raise "Import is not revertable" unless revertable?
|
||||
|
||||
update! status: :reverting
|
||||
|
||||
RevertImportJob.perform_later(self)
|
||||
end
|
||||
|
||||
def revert
|
||||
Import.transaction do
|
||||
accounts.destroy_all
|
||||
entries.destroy_all
|
||||
end
|
||||
|
||||
family.sync
|
||||
|
||||
update! status: :pending
|
||||
rescue => error
|
||||
update! status: :revert_failed, error: error.message
|
||||
end
|
||||
|
||||
def csv_rows
|
||||
@csv_rows ||= parsed_csv
|
||||
end
|
||||
|
@ -113,6 +141,10 @@ class Import < ApplicationRecord
|
|||
cleaned? && mappings.all?(&:valid?)
|
||||
end
|
||||
|
||||
def revertable?
|
||||
complete? || revert_failed?
|
||||
end
|
||||
|
||||
def has_unassigned_account?
|
||||
mappings.accounts.where(key: "").any?
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue