mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
fix: locale-dependent category duplication bug (#956)
* fix: locale-dependent category duplication bug * fix: use family locale for investment contributions category to prevent duplicates and handle legacy data * Remove v* tag trigger from flutter-build to fix double-runs publish.yml already calls flutter-build via workflow_call on v* tags, so the direct push trigger was causing duplicate workflow runs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Refactor mobile release asset flow * fix: category uniqueness and workflow issues * fix: fix test issue * fix: solve test issue * fix: resolve legacy problem * fix: solve lint test issue * fix: revert unrelated changes --------- Co-authored-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -127,6 +127,14 @@ class Category < ApplicationRecord
|
||||
I18n.t(INVESTMENT_CONTRIBUTIONS_NAME_KEY)
|
||||
end
|
||||
|
||||
# Returns all possible investment contributions names across all supported locales
|
||||
# Used to detect investment contributions category regardless of locale
|
||||
def all_investment_contributions_names
|
||||
LanguagesHelper::SUPPORTED_LOCALES.map do |locale|
|
||||
I18n.t(INVESTMENT_CONTRIBUTIONS_NAME_KEY, locale: locale)
|
||||
end.uniq
|
||||
end
|
||||
|
||||
private
|
||||
def default_categories
|
||||
[
|
||||
|
||||
@@ -123,14 +123,45 @@ class Family < ApplicationRecord
|
||||
|
||||
# Returns the Investment Contributions category for this family, creating it if it doesn't exist.
|
||||
# This is used for auto-categorizing transfers to investment accounts.
|
||||
# Always uses the family's locale to ensure consistent category naming across all users.
|
||||
def investment_contributions_category
|
||||
categories.find_or_create_by!(name: Category.investment_contributions_name) do |cat|
|
||||
cat.color = "#0d9488"
|
||||
cat.classification = "expense"
|
||||
cat.lucide_icon = "trending-up"
|
||||
# Find ALL legacy categories (created under old request-locale behavior)
|
||||
legacy = categories.where(name: Category.all_investment_contributions_names).order(:created_at).to_a
|
||||
|
||||
if legacy.any?
|
||||
keeper = legacy.first
|
||||
duplicates = legacy[1..]
|
||||
|
||||
# Reassign transactions and subcategories from duplicates to keeper
|
||||
if duplicates.any?
|
||||
duplicate_ids = duplicates.map(&:id)
|
||||
categories.where(parent_id: duplicate_ids).update_all(parent_id: keeper.id)
|
||||
Transaction.where(category_id: duplicate_ids).update_all(category_id: keeper.id)
|
||||
BudgetCategory.where(category_id: duplicate_ids).update_all(category_id: keeper.id)
|
||||
categories.where(id: duplicate_ids).delete_all
|
||||
end
|
||||
|
||||
# Rename keeper to family's locale name if needed
|
||||
I18n.with_locale(locale) do
|
||||
correct_name = Category.investment_contributions_name
|
||||
keeper.update!(name: correct_name) unless keeper.name == correct_name
|
||||
end
|
||||
return keeper
|
||||
end
|
||||
|
||||
# Create new category using family's locale
|
||||
I18n.with_locale(locale) do
|
||||
categories.find_or_create_by!(name: Category.investment_contributions_name) do |cat|
|
||||
cat.color = "#0d9488"
|
||||
cat.classification = "expense"
|
||||
cat.lucide_icon = "trending-up"
|
||||
end
|
||||
end
|
||||
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
|
||||
categories.find_by(name: Category.investment_contributions_name)
|
||||
# Handle race condition: another process created the category
|
||||
I18n.with_locale(locale) do
|
||||
categories.find_by!(name: Category.investment_contributions_name)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns account IDs for tax-advantaged accounts (401k, IRA, HSA, etc.)
|
||||
|
||||
Reference in New Issue
Block a user