mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Merge branch 'main' into add-config-import-csv-skip-first-x-rows
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
@@ -2,6 +2,9 @@ class Import < ApplicationRecord
|
||||
MaxRowCountExceededError = Class.new(StandardError)
|
||||
MappingError = Class.new(StandardError)
|
||||
|
||||
MAX_CSV_SIZE = 10.megabytes
|
||||
ALLOWED_MIME_TYPES = %w[text/csv text/plain application/vnd.ms-excel application/csv].freeze
|
||||
|
||||
TYPES = %w[TransactionImport TradeImport AccountImport MintImport CategoryImport RuleImport].freeze
|
||||
SIGNAGE_CONVENTIONS = %w[inflows_positive inflows_negative]
|
||||
SEPARATORS = [ [ "Comma (,)", "," ], [ "Semicolon (;)", ";" ] ].freeze
|
||||
@@ -37,6 +40,7 @@ class Import < ApplicationRecord
|
||||
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }, allow_nil: true
|
||||
validates :number_format, presence: true, inclusion: { in: NUMBER_FORMATS.keys }
|
||||
validates :rows_to_skip, numericality: { greater_than_or_equal_to: 0 }
|
||||
validate :account_belongs_to_family
|
||||
|
||||
has_many :rows, dependent: :destroy
|
||||
has_many :mappings, dependent: :destroy
|
||||
@@ -111,7 +115,7 @@ class Import < ApplicationRecord
|
||||
|
||||
def dry_run
|
||||
mappings = {
|
||||
transactions: rows.count,
|
||||
transactions: rows_count,
|
||||
categories: Import::CategoryMapping.for_import(self).creational.count,
|
||||
tags: Import::TagMapping.for_import(self).creational.count
|
||||
}
|
||||
@@ -153,6 +157,7 @@ class Import < ApplicationRecord
|
||||
end
|
||||
|
||||
rows.insert_all!(mapped_rows)
|
||||
update_column(:rows_count, rows.count)
|
||||
end
|
||||
|
||||
def sync_mappings
|
||||
@@ -182,7 +187,7 @@ class Import < ApplicationRecord
|
||||
end
|
||||
|
||||
def configured?
|
||||
uploaded? && rows.any?
|
||||
uploaded? && rows_count > 0
|
||||
end
|
||||
|
||||
def cleaned?
|
||||
@@ -233,7 +238,7 @@ class Import < ApplicationRecord
|
||||
|
||||
private
|
||||
def row_count_exceeded?
|
||||
rows.count > max_row_count
|
||||
rows_count > max_row_count
|
||||
end
|
||||
|
||||
def import!
|
||||
@@ -296,4 +301,11 @@ class Import < ApplicationRecord
|
||||
def set_default_number_format
|
||||
self.number_format ||= "1,234.56" # Default to US/UK format
|
||||
end
|
||||
|
||||
def account_belongs_to_family
|
||||
return if account.nil?
|
||||
return if account.family_id == family_id
|
||||
|
||||
errors.add(:account, "must belong to your family")
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user