mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
Account-level import configuration templates (#1946)
* Account-level import configuration templates * Default import to family's preferred date format
This commit is contained in:
parent
5b2fa3d707
commit
0544089710
15 changed files with 108 additions and 43 deletions
|
@ -2,9 +2,7 @@ class Import::RowsController < ApplicationController
|
|||
before_action :set_import_row
|
||||
|
||||
def update
|
||||
@row.assign_attributes(row_params)
|
||||
@row.save!(validate: false)
|
||||
@row.sync_mappings
|
||||
@row.update_and_sync(row_params)
|
||||
|
||||
redirect_to import_row_path(@row.import, @row)
|
||||
end
|
||||
|
|
|
@ -12,7 +12,7 @@ class Import::UploadsController < ApplicationController
|
|||
@import.assign_attributes(raw_file_str: csv_str, col_sep: upload_params[:col_sep])
|
||||
@import.save!(validate: false)
|
||||
|
||||
redirect_to import_configuration_path(@import), notice: "CSV uploaded successfully."
|
||||
redirect_to import_configuration_path(@import, template_hint: true), notice: "CSV uploaded successfully."
|
||||
else
|
||||
flash.now[:alert] = "Must be valid CSV with headers and at least one row of data"
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class ImportsController < ApplicationController
|
||||
before_action :set_import, only: %i[show publish destroy revert]
|
||||
before_action :set_import, only: %i[show publish destroy revert apply_template]
|
||||
|
||||
def publish
|
||||
@import.publish_later
|
||||
|
@ -19,7 +19,11 @@ class ImportsController < ApplicationController
|
|||
|
||||
def create
|
||||
account = Current.family.accounts.find_by(id: params.dig(:import, :account_id))
|
||||
import = Current.family.imports.create!(type: import_params[:type], account: account)
|
||||
import = Current.family.imports.create!(
|
||||
type: import_params[:type],
|
||||
account: account,
|
||||
date_format: Current.family.date_format,
|
||||
)
|
||||
|
||||
redirect_to import_upload_path(import)
|
||||
end
|
||||
|
@ -37,6 +41,15 @@ class ImportsController < ApplicationController
|
|||
redirect_to imports_path, notice: "Import is reverting in the background."
|
||||
end
|
||||
|
||||
def apply_template
|
||||
if @import.suggested_template
|
||||
@import.apply_template!(@import.suggested_template)
|
||||
redirect_to import_configuration_path(@import), notice: "Template applied."
|
||||
else
|
||||
redirect_to import_configuration_path(@import), alert: "No template found, please manually configure your import."
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@import.destroy
|
||||
|
||||
|
|
|
@ -146,8 +146,20 @@ class Import < ApplicationRecord
|
|||
end
|
||||
|
||||
def sync_mappings
|
||||
mapping_steps.each do |mapping|
|
||||
mapping.sync(self)
|
||||
transaction do
|
||||
mapping_steps.each do |mapping_class|
|
||||
mappables_by_key = mapping_class.mappables_by_key(self)
|
||||
|
||||
updated_mappings = mappables_by_key.map do |key, mappable|
|
||||
mapping = mappings.find_or_initialize_by(key: key, import: self, type: mapping_class.name)
|
||||
mapping.mappable = mappable
|
||||
mapping.create_when_empty = key.present? && mappable.nil?
|
||||
mapping
|
||||
end
|
||||
|
||||
updated_mappings.each { |m| m.save(validate: false) }
|
||||
mapping_class.where.not(id: updated_mappings.map(&:id)).destroy_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -183,6 +195,28 @@ class Import < ApplicationRecord
|
|||
family.accounts.empty? && has_unassigned_account?
|
||||
end
|
||||
|
||||
# Used to optionally pre-fill the configuration for the current import
|
||||
def suggested_template
|
||||
family.imports
|
||||
.complete
|
||||
.where(account: account, type: type)
|
||||
.order(created_at: :desc)
|
||||
.first
|
||||
end
|
||||
|
||||
def apply_template!(import_template)
|
||||
update!(
|
||||
import_template.attributes.slice(
|
||||
"date_col_label", "amount_col_label", "name_col_label",
|
||||
"category_col_label", "tags_col_label", "account_col_label",
|
||||
"qty_col_label", "ticker_col_label", "price_col_label",
|
||||
"entity_type_col_label", "notes_col_label", "currency_col_label",
|
||||
"date_format", "signage_convention", "number_format",
|
||||
"exchange_operating_mic_col_label"
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
def import!
|
||||
# no-op, subclasses can implement for customization of algorithm
|
||||
|
|
|
@ -2,8 +2,11 @@ class Import::AccountMapping < Import::Mapping
|
|||
validates :mappable, presence: true, if: :requires_mapping?
|
||||
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:account).uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:account).uniq
|
||||
accounts = import.family.accounts.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| accounts[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ class Import::AccountTypeMapping < Import::Mapping
|
|||
validates :value, presence: true
|
||||
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:entity_type).uniq
|
||||
def mappables_by_key(import)
|
||||
import.rows.map(&:entity_type).uniq.index_with { nil }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
class Import::CategoryMapping < Import::Mapping
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:category).uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:category).uniq
|
||||
categories = import.family.categories.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| categories[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -18,19 +18,8 @@ class Import::Mapping < ApplicationRecord
|
|||
find_by(key: key)&.mappable
|
||||
end
|
||||
|
||||
def sync(import)
|
||||
unique_values = mapping_values(import).uniq
|
||||
|
||||
unique_values.each do |value|
|
||||
mapping = find_or_initialize_by(key: value, import: import, create_when_empty: value.present?)
|
||||
mapping.save(validate: false) if mapping.new_record?
|
||||
end
|
||||
|
||||
where(import: import).where.not(key: unique_values).destroy_all
|
||||
end
|
||||
|
||||
def mapping_values(import)
|
||||
raise NotImplementedError, "Subclass must implement mapping_values"
|
||||
def mappables_by_key(import)
|
||||
raise NotImplementedError, "Subclass must implement mappables_by_key"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -30,11 +30,10 @@ class Import::Row < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def sync_mappings
|
||||
Import::CategoryMapping.sync(import) if import.column_keys.include?(:category)
|
||||
Import::TagMapping.sync(import) if import.column_keys.include?(:tags)
|
||||
Import::AccountMapping.sync(import) if import.column_keys.include?(:account)
|
||||
Import::AccountTypeMapping.sync(import) if import.column_keys.include?(:entity_type)
|
||||
def update_and_sync(params)
|
||||
assign_attributes(params)
|
||||
save!(validate: false)
|
||||
import.sync_mappings
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
class Import::TagMapping < Import::Mapping
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:tags_list).flatten.uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:tags_list).flatten.uniq
|
||||
|
||||
tags = import.family.tags.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| tags[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,14 +4,33 @@
|
|||
|
||||
<%= content_for :previous_path, import_upload_path(@import) %>
|
||||
|
||||
<div>
|
||||
<% if @import.suggested_template.present? && params[:template_hint] == "true" %>
|
||||
<div class="py-12">
|
||||
<div class="shadow-border-xs rounded-lg p-4 max-w-lg mx-auto">
|
||||
<h3 class="text-sm font-medium mb-2 flex items-center gap-2">
|
||||
<span class="text-success">
|
||||
<%= icon "sparkles" %>
|
||||
</span>
|
||||
|
||||
Template configuration found
|
||||
</h3>
|
||||
|
||||
<p class="text-sm text-secondary">We found a configuration from a previous import for this account. Would you like to apply it to this import?</p>
|
||||
|
||||
<div class="mt-4 flex gap-2 items-center">
|
||||
<%= link_to "Manually configure", import_configuration_path(@import), class: "btn btn--outline" %>
|
||||
<%= button_to "Apply template", apply_template_import_path(@import), class: "btn btn--primary", method: :put, data: { turbo_frame: :_top } %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="space-y-4">
|
||||
<div class="text-center space-y-2">
|
||||
<h1 class="text-3xl text-primary font-medium"><%= t(".title") %></h1>
|
||||
<p class="text-secondary text-sm"><%= t(".description") %></p>
|
||||
</div>
|
||||
|
||||
<div class="mx-auto max-w-lg">
|
||||
<div class="mx-auto max-w-lg space-y-3">
|
||||
<%= render partial: permitted_import_configuration_path(@import), locals: { import: @import } %>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -19,4 +38,4 @@
|
|||
<div class="mx-auto max-w-5xl my-12">
|
||||
<%= render "imports/table", headers: @import.csv_headers, rows: @import.csv_sample, caption: "Sample data from your uploaded CSV" %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
<%= styled_form_with model: @import, scope: :import, url: import_upload_path(@import), multipart: true, class: "space-y-2" do |form| %>
|
||||
<%= form.select :col_sep, Import::SEPARATORS, label: true %>
|
||||
|
||||
<% unless @import.type == "MintImport" %>
|
||||
<% if @import.type == "TransactionImport" || @import.type == "TradeImport" %>
|
||||
<%= form.select :account_id, @import.family.accounts.pluck(:name, :id), { label: "Account (optional)", include_blank: "Multi-account import", selected: @import.account_id } %>
|
||||
<% end %>
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
<%= lucide_icon "check", class: "w-3 h-3" %>
|
||||
</div>
|
||||
<% when :alert %>
|
||||
<div class="flex h-full items-center justify-center rounded-full bg-error">
|
||||
<div class="flex h-full items-center justify-center rounded-full bg-destructive">
|
||||
<%= lucide_icon "x", class: "w-3 h-3" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
|
|
@ -68,8 +68,11 @@ Rails.application.routes.draw do
|
|||
resources :transfers, only: %i[new create destroy show update]
|
||||
|
||||
resources :imports, only: %i[index new show create destroy] do
|
||||
post :publish, on: :member
|
||||
put :revert, on: :member
|
||||
member do
|
||||
post :publish
|
||||
put :revert
|
||||
put :apply_template
|
||||
end
|
||||
|
||||
resource :upload, only: %i[show update], module: :import
|
||||
resource :configuration, only: %i[show update], module: :import
|
||||
|
|
|
@ -19,7 +19,7 @@ class Import::UploadsControllerTest < ActionDispatch::IntegrationTest
|
|||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_configuration_url(@import)
|
||||
assert_redirected_to import_configuration_url(@import, template_hint: true)
|
||||
assert_equal "CSV uploaded successfully.", flash[:notice]
|
||||
end
|
||||
|
||||
|
@ -31,7 +31,7 @@ class Import::UploadsControllerTest < ActionDispatch::IntegrationTest
|
|||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_configuration_url(@import)
|
||||
assert_redirected_to import_configuration_url(@import, template_hint: true)
|
||||
assert_equal "CSV uploaded successfully.", flash[:notice]
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue