Files
sure/app/models/category_import.rb
Carlos Adames b56dbdb9eb Feat: /import endpoint & drag-n-drop imports (#501)
* Implement API v1 Imports controller

- Add Api::V1::ImportsController with index, show, and create actions
- Add Jbuilder views for index and show
- Add integration tests
- Implement row generation logic in create action
- Update routes

* Validate import account belongs to family

- Add validation to Import model to ensure account belongs to the same family
- Add regression test case in Api::V1::ImportsControllerTest

* updating docs to be more detailed

* Rescue StandardError instead of bare rescue in ImportsController

* Optimize Imports API and fix documentation

- Implement rows_count counter cache for Imports
- Preload rows in Api::V1::ImportsController#show
- Update documentation to show correct OAuth scopes

* Fix formatting in ImportsControllerTest

* Permit all import parameters and fix unknown attribute error

* Restore API routes for auth, chats, and messages

* removing pr summary

* Fix trailing whitespace and configured? test failure

- Update Import#configured? to use rows_count for performance and consistency
- Mock rows_count in TransactionImportTest
- Fix trailing whitespace in migration

* Harden security and fix mass assignment in ImportsController

- Handle type and account_id explicitly in create action
- Rename import_params to import_config_params for clarity
- Validate type against Import::TYPES

* Fix MintImport rows_count update and migration whitespace

- Update MintImport#generate_rows_from_csv to update rows_count counter cache
- Fix trailing whitespace and final newline in AddRowsCountToImports migration

* Implement full-screen Drag and Drop CSV import on Transactions page

- Add DragAndDropImport Stimulus controller listening on document
- Add full-screen overlay with icon and text to Transactions index
- Update ImportsController to handle direct file uploads via create action
- Add system test for drag and drop functionality

* Implement Drag and Drop CSV upload on Import Upload page

- Add drag-and-drop-import controller to import/uploads/show
- Add full-screen overlay to import/uploads/show
- Annotate upload form and input with drag-and-drop targets
- Add PR_SUMMARY.md

* removing pr summary

* Add file validation to ImportsController

- Validate file size (max 10MB) and MIME type in create action
- Prevent memory exhaustion and invalid file processing
- Defined MAX_CSV_SIZE and ALLOWED_MIME_TYPES in Import model

* Refactor dragLeave logic with counter pattern to prevent flickering

* Extract shared drag-and-drop overlay partial

- Create app/views/imports/_drag_drop_overlay.html.erb
- Update transactions/index and import/uploads/show to use the partial
- Reduce code duplication in views

* Update Brakeman and harden ImportsController security

- Update brakeman to 7.1.2
- Explicitly handle type assignment in ImportsController#create to avoid mass assignment
- Remove :type from permitted import parameters

* Fix trailing whitespace in DragAndDropImportTest

* Don't commit LLM comments as file

* FIX add api validation

---------

Co-authored-by: Carlos Adames <cj@Carloss-MacBook-Air.local>
Co-authored-by: Juan José Mata <jjmata@jjmata.com>
Co-authored-by: sokie <sokysrm@gmail.com>
2026-01-10 16:39:18 +01:00

120 lines
3.5 KiB
Ruby

class CategoryImport < Import
def import!
transaction do
rows.each do |row|
category_name = row.name.to_s.strip
category = family.categories.find_or_initialize_by(name: category_name)
category.color = row.category_color.presence || category.color || Category::UNCATEGORIZED_COLOR
category.classification = row.category_classification.presence || category.classification || "expense"
category.lucide_icon = row.category_icon.presence || category.lucide_icon || "shapes"
category.parent = nil
category.save!
ensure_placeholder_category(row.category_parent)
end
rows.each do |row|
category = family.categories.find_by!(name: row.name.to_s.strip)
parent = ensure_placeholder_category(row.category_parent)
if parent && parent == category
errors.add(:base, "Category '#{category.name}' cannot be its own parent")
raise ActiveRecord::RecordInvalid.new(self)
end
next if category.parent == parent
category.update!(parent: parent)
end
end
end
def column_keys
%i[name category_color category_parent category_classification category_icon]
end
def required_column_keys
%i[name]
end
def mapping_steps
[]
end
def dry_run
{ categories: rows_count }
end
def csv_template
template = <<-CSV
name*,color,parent_category,classification,lucide_icon
Food & Drink,#f97316,,expense,carrot
Groceries,#407706,Food & Drink,expense,shopping-basket
Salary,#22c55e,,income,briefcase
CSV
CSV.parse(template, headers: true)
end
def generate_rows_from_csv
rows.destroy_all
validate_required_headers!
name_header = header_for("name")
color_header = header_for("color")
parent_header = header_for("parent_category", "parent category")
classification_header = header_for("classification")
icon_header = header_for("lucide_icon", "lucide icon", "icon")
csv_rows.each do |row|
rows.create!(
name: row[name_header].to_s.strip,
category_color: row[color_header].to_s.strip,
category_parent: row[parent_header].to_s.strip,
category_classification: row[classification_header].to_s.strip,
category_icon: row[icon_header].to_s.strip,
currency: default_currency
)
end
end
private
def validate_required_headers!
missing_headers = required_column_keys.map(&:to_s).reject { |key| header_for(key).present? }
return if missing_headers.empty?
errors.add(:base, "Missing required columns: #{missing_headers.join(', ')}")
raise ActiveRecord::RecordInvalid.new(self)
end
def header_for(*candidates)
candidates.each do |candidate|
normalized = normalize_header(candidate)
header = normalized_headers[normalized]
return header if header.present?
end
nil
end
def normalized_headers
@normalized_headers ||= csv_headers.to_h { |header| [ normalize_header(header), header ] }
end
def normalize_header(header)
header.to_s.strip.downcase.gsub(/\*/, "").gsub(/[\s-]+/, "_")
end
def ensure_placeholder_category(name)
trimmed_name = name.to_s.strip
return if trimmed_name.blank?
family.categories.find_or_create_by!(name: trimmed_name) do |placeholder|
placeholder.color = Category::UNCATEGORIZED_COLOR
placeholder.classification = "expense"
placeholder.lucide_icon = "shapes"
end
end
end