From e26e5c5aec7b266496445a4027b27b443d6ccdaf Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Sun, 18 May 2025 15:02:51 -0400 Subject: [PATCH] Auto sync preference, max limit on account CSV imports (#2259) * Auto sync preference, max limit on account CSV imports * MaxRowCountExceededError --- app/controllers/concerns/auto_sync.rb | 1 + app/controllers/imports_controller.rb | 2 ++ app/models/account_import.rb | 4 ++++ app/models/import.rb | 13 +++++++++++++ ...0518181619_add_auto_sync_preference_to_family.rb | 5 +++++ db/schema.rb | 3 ++- test/controllers/concerns/auto_sync_test.rb | 8 ++++++++ 7 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20250518181619_add_auto_sync_preference_to_family.rb diff --git a/app/controllers/concerns/auto_sync.rb b/app/controllers/concerns/auto_sync.rb index e6ced672..15cdc557 100644 --- a/app/controllers/concerns/auto_sync.rb +++ b/app/controllers/concerns/auto_sync.rb @@ -13,6 +13,7 @@ module AutoSync def family_needs_auto_sync? return false unless Current.family&.accounts&.active&.any? return false if (Current.family.last_sync_created_at&.to_date || 1.day.ago) >= Date.current + return false unless Current.family.auto_sync_on_login Rails.logger.info "Auto-syncing family #{Current.family.id}, last sync was #{Current.family.last_sync_created_at}" diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index c1b51c23..20e5f9c4 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -5,6 +5,8 @@ class ImportsController < ApplicationController @import.publish_later redirect_to import_path(@import), notice: "Your import has started in the background." + rescue Import::MaxRowCountExceededError + redirect_back_or_to import_path(@import), alert: "Your import exceeds the maximum row count of #{@import.max_row_count}." end def index diff --git a/app/models/account_import.rb b/app/models/account_import.rb index 96fdfd47..aa4c6dfe 100644 --- a/app/models/account_import.rb +++ b/app/models/account_import.rb @@ -54,4 +54,8 @@ class AccountImport < Import CSV.parse(template, headers: true) end + + def max_row_count + 50 + end end diff --git a/app/models/import.rb b/app/models/import.rb index e96d1fc1..b0a02ea0 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -1,4 +1,6 @@ class Import < ApplicationRecord + MaxRowCountExceededError = Class.new(StandardError) + TYPES = %w[TransactionImport TradeImport AccountImport MintImport].freeze SIGNAGE_CONVENTIONS = %w[inflows_positive inflows_negative] SEPARATORS = [ [ "Comma (,)", "," ], [ "Semicolon (;)", ";" ] ].freeze @@ -52,6 +54,7 @@ class Import < ApplicationRecord end def publish_later + raise MaxRowCountExceededError if row_count_exceeded? raise "Import is not publishable" unless publishable? update! status: :importing @@ -60,6 +63,8 @@ class Import < ApplicationRecord end def publish + raise MaxRowCountExceededError if row_count_exceeded? + import! family.sync_later @@ -220,7 +225,15 @@ class Import < ApplicationRecord ) end + def max_row_count + 10000 + end + private + def row_count_exceeded? + rows.count > max_row_count + end + def import! # no-op, subclasses can implement for customization of algorithm end diff --git a/db/migrate/20250518181619_add_auto_sync_preference_to_family.rb b/db/migrate/20250518181619_add_auto_sync_preference_to_family.rb new file mode 100644 index 00000000..80e1cd9f --- /dev/null +++ b/db/migrate/20250518181619_add_auto_sync_preference_to_family.rb @@ -0,0 +1,5 @@ +class AddAutoSyncPreferenceToFamily < ActiveRecord::Migration[7.2] + def change + add_column :families, :auto_sync_on_login, :boolean, default: true, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 5b9426c7..cdb863c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_05_16_180846) do +ActiveRecord::Schema[7.2].define(version: 2025_05_18_181619) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -227,6 +227,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_16_180846) do t.string "timezone" t.boolean "data_enrichment_enabled", default: false t.boolean "early_access", default: false + t.boolean "auto_sync_on_login", default: true, null: false end create_table "holdings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| diff --git a/test/controllers/concerns/auto_sync_test.rb b/test/controllers/concerns/auto_sync_test.rb index a388456b..462850f8 100644 --- a/test/controllers/concerns/auto_sync_test.rb +++ b/test/controllers/concerns/auto_sync_test.rb @@ -38,4 +38,12 @@ class AutoSyncTest < ActionDispatch::IntegrationTest get root_path end end + + test "does not auto-sync if preference is disabled" do + @family.update!(auto_sync_on_login: false) + + assert_no_difference "Sync.count" do + get root_path + end + end end