mirror of
https://github.com/we-promise/sure.git
synced 2026-05-24 21:14:56 +00:00
* feat(api): add import preflight validation * fix(api): harden import preflight validation
108 lines
2.9 KiB
Ruby
108 lines
2.9 KiB
Ruby
class MintImport < Import
|
|
after_create :set_mappings
|
|
|
|
DEFAULT_COLUMN_MAPPINGS = {
|
|
signage_convention: "inflows_positive",
|
|
date_col_label: "Date",
|
|
date_format: "%m/%d/%Y",
|
|
name_col_label: "Description",
|
|
amount_col_label: "Amount",
|
|
currency_col_label: "Currency",
|
|
account_col_label: "Account Name",
|
|
category_col_label: "Category",
|
|
tags_col_label: "Labels",
|
|
notes_col_label: "Notes",
|
|
entity_type_col_label: "Transaction Type"
|
|
}.freeze
|
|
|
|
def self.default_column_mappings
|
|
DEFAULT_COLUMN_MAPPINGS
|
|
end
|
|
|
|
def generate_rows_from_csv
|
|
rows.destroy_all
|
|
|
|
mapped_rows = csv_rows.map.with_index(1) do |row, index|
|
|
{
|
|
source_row_number: index,
|
|
account: row[account_col_label].to_s,
|
|
date: row[date_col_label].to_s,
|
|
amount: signed_csv_amount(row).to_s,
|
|
currency: (row[currency_col_label] || default_currency).to_s,
|
|
name: (row[name_col_label] || default_row_name).to_s,
|
|
category: row[category_col_label].to_s,
|
|
tags: row[tags_col_label].to_s,
|
|
notes: row[notes_col_label].to_s
|
|
}
|
|
end
|
|
|
|
rows.insert_all!(mapped_rows)
|
|
update_column(:rows_count, rows.count)
|
|
end
|
|
|
|
def import!
|
|
transaction do
|
|
mappings.each(&:create_mappable!)
|
|
|
|
rows.each do |row|
|
|
account = mappings.accounts.mappable_for(row.account)
|
|
category = mappings.categories.mappable_for(row.category)
|
|
tags = row.tags_list.map { |tag| mappings.tags.mappable_for(tag) }.compact
|
|
|
|
# Use account's currency when no currency column was mapped in CSV, with family currency as fallback
|
|
effective_currency = currency_col_label.present? ? row.currency : (account.currency.presence || family.currency)
|
|
|
|
entry = account.entries.build \
|
|
date: row.date_iso,
|
|
amount: row.signed_amount,
|
|
name: row.name,
|
|
currency: effective_currency,
|
|
notes: row.notes,
|
|
entryable: Transaction.new(category: category, tags: tags),
|
|
import: self
|
|
|
|
entry.save!
|
|
end
|
|
end
|
|
end
|
|
|
|
def mapping_steps
|
|
[ Import::CategoryMapping, Import::TagMapping, Import::AccountMapping ]
|
|
end
|
|
|
|
def required_column_keys
|
|
%i[date amount]
|
|
end
|
|
|
|
def column_keys
|
|
%i[date amount name currency category tags account notes]
|
|
end
|
|
|
|
def csv_template
|
|
template = <<-CSV
|
|
Date,Amount,Account Name,Description,Category,Labels,Currency,Notes,Transaction Type
|
|
01/01/2024,-8.55,Checking,Starbucks,Food & Drink,Coffee|Breakfast,USD,Morning coffee,debit
|
|
04/15/2024,2000,Savings,Paycheck,Income,,USD,Bi-weekly salary,credit
|
|
CSV
|
|
|
|
CSV.parse(template, headers: true)
|
|
end
|
|
|
|
def signed_csv_amount(csv_row)
|
|
amount = csv_row[amount_col_label]
|
|
type = csv_row["Transaction Type"]
|
|
|
|
if type == "credit"
|
|
amount.to_d
|
|
else
|
|
amount.to_d * -1
|
|
end
|
|
end
|
|
|
|
private
|
|
def set_mappings
|
|
assign_attributes(self.class.default_column_mappings)
|
|
save!
|
|
end
|
|
end
|