From 908b3e24899a69591de8db641522de9eac51b209 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Mon, 12 May 2025 15:41:14 -0400 Subject: [PATCH 1/4] Temporary disable of sync cascade behavior --- app/models/sync.rb | 49 ++++------------------------------------ test/models/sync_test.rb | 40 -------------------------------- 2 files changed, 4 insertions(+), 85 deletions(-) diff --git a/app/models/sync.rb b/app/models/sync.rb index 826899ce..4d923f12 100644 --- a/app/models/sync.rb +++ b/app/models/sync.rb @@ -21,58 +21,17 @@ class Sync < ApplicationRecord begin syncable.sync_data(self, start_date: start_date) - unless has_pending_child_syncs? - complete! - Rails.logger.info("Sync completed, starting post-sync") - syncable.post_sync(self) - Rails.logger.info("Post-sync completed") - end + complete! + Rails.logger.info("Sync completed, starting post-sync") + syncable.post_sync(self) + Rails.logger.info("Post-sync completed") rescue StandardError => error fail! error, report_error: true - ensure - notify_parent_of_completion! if has_parent? - end - end - end - - def handle_child_completion_event - Sync.transaction do - # We need this to ensure 2 child syncs don't update the parent at the exact same time with different results - # and cause the sync to hang in "syncing" status indefinitely - self.lock! - - unless has_pending_child_syncs? - if has_failed_child_syncs? - fail!(Error.new("One or more child syncs failed")) - else - complete! - end - - # If this sync is both a child and a parent, we need to notify the parent of completion - notify_parent_of_completion! if has_parent? - - syncable.post_sync(self) end end end private - def has_pending_child_syncs? - children.where(status: [ :pending, :syncing ]).any? - end - - def has_failed_child_syncs? - children.where(status: :failed).any? - end - - def has_parent? - parent_id.present? - end - - def notify_parent_of_completion! - parent.handle_child_completion_event - end - def start! Rails.logger.info("Starting sync") update! status: :syncing diff --git a/test/models/sync_test.rb b/test/models/sync_test.rb index 6b246a1f..99019146 100644 --- a/test/models/sync_test.rb +++ b/test/models/sync_test.rb @@ -31,44 +31,4 @@ class SyncTest < ActiveSupport::TestCase assert_equal "failed", @sync.status assert_equal "test sync error", @sync.error end - - # Order is important here. Parent syncs must implement sync_data so that their own work - # is 100% complete *prior* to queueing up child syncs. - test "runs sync with child syncs" do - family = families(:dylan_family) - - parent = Sync.create!(syncable: family) - child1 = Sync.create!(syncable: family.accounts.first, parent: parent) - child2 = Sync.create!(syncable: family.accounts.second, parent: parent) - grandchild = Sync.create!(syncable: family.accounts.last, parent: child2) - - parent.syncable.expects(:sync_data).returns([]).once - child1.syncable.expects(:sync_data).returns([]).once - child2.syncable.expects(:sync_data).returns([]).once - grandchild.syncable.expects(:sync_data).returns([]).once - - assert_equal "pending", parent.status - assert_equal "pending", child1.status - assert_equal "pending", child2.status - assert_equal "pending", grandchild.status - - parent.perform - assert_equal "syncing", parent.reload.status - - child1.perform - assert_equal "completed", child1.reload.status - assert_equal "syncing", parent.reload.status - - child2.perform - assert_equal "syncing", child2.reload.status - assert_equal "completed", child1.reload.status - assert_equal "syncing", parent.reload.status - - # Will complete the parent and grandparent syncs - grandchild.perform - assert_equal "completed", grandchild.reload.status - assert_equal "completed", child1.reload.status - assert_equal "completed", child2.reload.status - assert_equal "completed", parent.reload.status - end end From 9e6e4b1ce6937879d0061ca5ab845bf8b629652f Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Mon, 12 May 2025 18:55:19 -0400 Subject: [PATCH 2/4] Only run Plaid syncs via webhook after initial sync --- app/models/family.rb | 5 ----- app/views/accounts/index.html.erb | 19 ++++++++++--------- app/views/accounts/show/_header.html.erb | 17 +++-------------- app/views/plaid_items/_plaid_item.html.erb | 2 +- test/models/family_test.rb | 4 ---- 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/app/models/family.rb b/app/models/family.rb index f565d54d..a3a73eec 100644 --- a/app/models/family.rb +++ b/app/models/family.rb @@ -74,11 +74,6 @@ class Family < ApplicationRecord account.sync_later(start_date: start_date, parent_sync: sync) end - Rails.logger.info("Syncing plaid items for family #{id}") - plaid_items.each do |plaid_item| - plaid_item.sync_later(start_date: start_date, parent_sync: sync) - end - Rails.logger.info("Applying rules for family #{id}") rules.each do |rule| rule.apply_later diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb index 4893e44c..b4c78332 100644 --- a/app/views/accounts/index.html.erb +++ b/app/views/accounts/index.html.erb @@ -2,15 +2,16 @@

<%= t(".accounts") %>

- <%= render ButtonComponent.new( - text: "Sync all", - href: sync_all_accounts_path, - method: :post, - variant: "outline", - disabled: Current.family.syncing?, - icon: "refresh-cw", - class: "" - ) %> + <% if Rails.env.development? %> + <%= render ButtonComponent.new( + text: "Sync all", + href: sync_all_accounts_path, + method: :post, + variant: "outline", + disabled: Current.family.syncing?, + icon: "refresh-cw", + ) %> + <% end %> <%= render LinkComponent.new( text: "New account", diff --git a/app/views/accounts/show/_header.html.erb b/app/views/accounts/show/_header.html.erb index 4c60e832..b64408ce 100644 --- a/app/views/accounts/show/_header.html.erb +++ b/app/views/accounts/show/_header.html.erb @@ -20,26 +20,15 @@ <% end %>
- <% if account.plaid_account_id.present? %> - <% if Rails.env.development? %> - <%= icon( + <% if Rails.env.development? %> + <%= icon( "refresh-cw", as_button: true, size: "sm", - href: sync_plaid_item_path(account.plaid_account.plaid_item), + href: account.linked? ? sync_plaid_item_path(account.plaid_account.plaid_item) : sync_account_path(account), disabled: account.syncing?, frame: :_top ) %> - <% end %> - <% else %> - <%= icon( - "refresh-cw", - as_button: true, - size: "sm", - href: sync_account_path(account), - disabled: account.syncing?, - frame: :_top - ) %> <% end %> <%= render "accounts/show/menu", account: account %> diff --git a/app/views/plaid_items/_plaid_item.html.erb b/app/views/plaid_items/_plaid_item.html.erb index 7c3dc8b2..61dea7dc 100644 --- a/app/views/plaid_items/_plaid_item.html.erb +++ b/app/views/plaid_items/_plaid_item.html.erb @@ -92,7 +92,7 @@
<% end %> - <% else %> + <% elsif Rails.env.development? %> <%= icon( "refresh-cw", as_button: true, diff --git a/test/models/family_test.rb b/test/models/family_test.rb index 7223e64b..24876a77 100644 --- a/test/models/family_test.rb +++ b/test/models/family_test.rb @@ -20,10 +20,6 @@ class FamilyTest < ActiveSupport::TestCase .with(start_date: nil, parent_sync: family_sync) .times(manual_accounts_count) - PlaidItem.any_instance.expects(:sync_later) - .with(start_date: nil, parent_sync: family_sync) - .times(items_count) - @syncable.sync_data(family_sync, start_date: family_sync.start_date) end end From 0fb689290a2199272688b4f832cfbbef5e77eba2 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Tue, 13 May 2025 08:32:53 -0400 Subject: [PATCH 3/4] Family subscription unique index --- .../20250513122703_add_uniqueness_to_subscriptions.rb | 6 ++++++ db/schema.rb | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20250513122703_add_uniqueness_to_subscriptions.rb diff --git a/db/migrate/20250513122703_add_uniqueness_to_subscriptions.rb b/db/migrate/20250513122703_add_uniqueness_to_subscriptions.rb new file mode 100644 index 00000000..f97b67f9 --- /dev/null +++ b/db/migrate/20250513122703_add_uniqueness_to_subscriptions.rb @@ -0,0 +1,6 @@ +class AddUniquenessToSubscriptions < ActiveRecord::Migration[7.2] + def change + remove_index :subscriptions, :family_id + add_index :subscriptions, :family_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d76a125..c9ba49e5 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_09_182903) do +ActiveRecord::Schema[7.2].define(version: 2025_05_13_122703) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -580,7 +580,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_09_182903) do t.datetime "trial_ends_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["family_id"], name: "index_subscriptions_on_family_id" + t.index ["family_id"], name: "index_subscriptions_on_family_id", unique: true end create_table "syncs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| From df8e22afe9e56341365e7d7e0b2b58141949ad33 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Tue, 13 May 2025 08:56:32 -0400 Subject: [PATCH 4/4] Stripe tasks --- lib/tasks/stripe.rake | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/tasks/stripe.rake diff --git a/lib/tasks/stripe.rake b/lib/tasks/stripe.rake new file mode 100644 index 00000000..7e2124de --- /dev/null +++ b/lib/tasks/stripe.rake @@ -0,0 +1,28 @@ +namespace :stripe do + desc "Sync legacy Stripe subscriptions" + task sync_legacy_subscriptions: :environment do + cli = Stripe::StripeClient.new(ENV["STRIPE_SECRET_KEY"]) + + subs = cli.v1.subscriptions.list + + subs.auto_paging_each do |sub| + details = sub.items.data.first + + family = Family.find_by(stripe_customer_id: sub.customer) + + if family.nil? + puts "Family not found for Stripe customer ID: #{sub.customer}, skipping" + next + end + + family.subscription.update!( + stripe_id: sub.id, + status: sub.status, + interval: details.plan.interval, + amount: details.plan.amount / 100.0, + currency: details.plan.currency.upcase, + current_period_ends_at: Time.at(details.current_period_end) + ) + end + end +end