The personal finance app for everyone
Add post-sync hook (#1479)
Zach Gollwitzere641cfccd47a
diff --git a/app/models/concerns/syncable.rb b/app/models/concerns/syncable.rbindex ec6abb2e..042eb6b1 100644--- a/app/models/concerns/syncable.rb+++ b/app/models/concerns/syncable.rb@@ -22,6 +22,10 @@ module Syncableraise NotImplementedError, "Subclasses must implement the `sync_data` method"end+ def post_sync+ # no-op, syncable can optionally provide implementation+ end+def sync_errorlatest_sync.errorenddiff --git a/app/models/family.rb b/app/models/family.rbindex 0e9226f8..673df231 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -32,6 +32,10 @@ class Family < ApplicationRecordendend+ def post_sync+ broadcast_refresh+ end+def syncing?super || accounts.manual.any?(&:syncing?) || plaid_items.any?(&:syncing?)enddiff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rbindex d456285e..5492edaf 100644--- a/app/models/plaid_item.rb+++ b/app/models/plaid_item.rb@@ -42,6 +42,10 @@ class PlaidItem < ApplicationRecordendend+ def post_sync+ family.broadcast_refresh+ end+def destroy_laterupdate!(scheduled_for_deletion: true)DestroyJob.perform_later(self)diff --git a/app/models/sync.rb b/app/models/sync.rbindex c0a8b53c..84e078da 100644--- a/app/models/sync.rb+++ b/app/models/sync.rb@@ -8,32 +8,27 @@ class Sync < ApplicationRecorddef performstart!- syncable.sync_data(start_date: start_date)-- complete!- rescue StandardError => error- fail! error- raise error if Rails.env.development?+ begin+ syncable.sync_data(start_date: start_date)+ complete!+ rescue StandardError => error+ fail! error+ raise error if Rails.env.development?+ ensure+ syncable.post_sync+ endendprivate- def family- syncable.is_a?(Family) ? syncable : syncable.family- end-def start!update! status: :syncingenddef complete!update! status: :completed, last_ran_at: Time.current-- family.broadcast_refreshenddef fail!(error)update! status: :failed, error: error.message, last_ran_at: Time.current-- family.broadcast_refreshendend