mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 21:29:38 +02:00
Ignore empty categories while importing (#789)
* Ignore empty categories while importing * Review fixes
This commit is contained in:
parent
ac27a1c87f
commit
77f166a5f8
5 changed files with 23 additions and 8 deletions
|
@ -11,6 +11,8 @@ class Import < ApplicationRecord
|
||||||
|
|
||||||
scope :ordered, -> { order(created_at: :desc) }
|
scope :ordered, -> { order(created_at: :desc) }
|
||||||
|
|
||||||
|
FALLBACK_TRANSACTION_NAME = "Imported transaction"
|
||||||
|
|
||||||
def publish_later
|
def publish_later
|
||||||
ImportJob.perform_later(self)
|
ImportJob.perform_later(self)
|
||||||
end
|
end
|
||||||
|
@ -110,9 +112,9 @@ class Import < ApplicationRecord
|
||||||
transactions = []
|
transactions = []
|
||||||
|
|
||||||
csv.table.each do |row|
|
csv.table.each do |row|
|
||||||
category = account.family.transaction_categories.find_or_initialize_by(name: row["category"])
|
category = account.family.transaction_categories.find_or_initialize_by(name: row["category"]) if row["category"].present?
|
||||||
txn = account.transactions.build \
|
txn = account.transactions.build \
|
||||||
name: row["name"].presence || "Imported transaction",
|
name: row["name"].presence || FALLBACK_TRANSACTION_NAME,
|
||||||
date: Date.iso8601(row["date"]),
|
date: Date.iso8601(row["date"]),
|
||||||
category: category,
|
category: category,
|
||||||
amount: BigDecimal(row["amount"]) * -1, # User inputs amounts with opposite signage of our internal representation
|
amount: BigDecimal(row["amount"]) * -1, # User inputs amounts with opposite signage of our internal representation
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<%# locals: (category:) %>
|
<%# locals: (category:) %>
|
||||||
<% category ||= null_category %>
|
<% category ||= null_category %>
|
||||||
|
|
||||||
<span class="border text-sm font-medium px-2.5 py-1 rounded-full cursor-pointer content-center"
|
<span class="border text-sm font-medium px-2.5 py-1 rounded-full content-center"
|
||||||
style="
|
style="
|
||||||
background-color: color-mix(in srgb, <%= category.color %> 5%, white);
|
background-color: color-mix(in srgb, <%= category.color %> 5%, white);
|
||||||
border-color: color-mix(in srgb, <%= category.color %> 10%, white);
|
border-color: color-mix(in srgb, <%= category.color %> 10%, white);
|
||||||
|
|
|
@ -37,7 +37,7 @@ class Import::CsvTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "csv with additional columns and empty values" do
|
test "csv with additional columns and empty values" do
|
||||||
csv = Import::Csv.new valid_csv_with_extra_column
|
csv = Import::Csv.new valid_csv_with_missing_data
|
||||||
assert csv.valid?
|
assert csv.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ class ImportTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "publishes a valid import" do
|
test "publishes a valid import" do
|
||||||
assert_difference "Transaction.count", 2 do
|
assert_difference -> { Transaction::Category.count } => 2, -> { Transaction.count } => 2 do
|
||||||
@loaded_import.publish
|
@loaded_import.publish
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -48,6 +48,19 @@ class ImportTest < ActiveSupport::TestCase
|
||||||
assert @loaded_import.complete?
|
assert @loaded_import.complete?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "publishes a valid import with missing data" do
|
||||||
|
@empty_import.update! raw_csv_str: valid_csv_with_missing_data
|
||||||
|
assert_difference -> { Transaction::Category.count } => 1, -> { Transaction.count } => 2 do
|
||||||
|
@empty_import.publish
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_not_nil Transaction.find_sole_by(name: Import::FALLBACK_TRANSACTION_NAME)
|
||||||
|
|
||||||
|
@empty_import.reload
|
||||||
|
|
||||||
|
assert @empty_import.complete?
|
||||||
|
end
|
||||||
|
|
||||||
test "failed publish results in error status" do
|
test "failed publish results in error status" do
|
||||||
@empty_import.update! raw_csv_str: valid_csv_with_invalid_values
|
@empty_import.update! raw_csv_str: valid_csv_with_invalid_values
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,11 @@ module ImportTestHelper
|
||||||
ROWS
|
ROWS
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_csv_with_extra_column
|
def valid_csv_with_missing_data
|
||||||
<<-ROWS
|
<<-ROWS
|
||||||
date,name,category,"optional id",amount
|
date,name,category,"optional id",amount
|
||||||
2024-01-01,Starbucks drink,Food,1234,20
|
2024-01-01,Drink,Food,1234,200
|
||||||
2024-01-02,Amazon stuff,Shopping,,200
|
2024-01-02,,,,100
|
||||||
ROWS
|
ROWS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue