mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
* Add kind field to valuation * Fix schema conflict * Add kind to valuation * Scaffold opening balance manager * Opening balance manager implementation * Update account import to use opening balance manager + tests * Update account to use opening balance manager * Fix test assertions, usage of current balance manager * Lint fixes * Add Opening Balance manager, add tests to forward calculator * Add credit card to "all cash" designation * Simplify valuation model * Add current balance manager with tests * Add current balance logic to reverse calculator and plaid sync * Tweaks to initial calc logic * Ledger testing helper, tweak assertions for reverse calculator * Update test assertions * Extract balance transformer, simplify calculators * Algo simplifications * Final tweaks to calculators * Cleanup * Fix error, propagate sync errors up to parent * Update migration script, valuation naming
73 lines
1.6 KiB
Ruby
73 lines
1.6 KiB
Ruby
module Syncable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
has_many :syncs, as: :syncable, dependent: :destroy
|
|
end
|
|
|
|
def syncing?
|
|
syncs.visible.any?
|
|
end
|
|
|
|
# Schedules a sync for syncable. If there is an existing sync pending/syncing for this syncable,
|
|
# we do not create a new sync, and attempt to expand the sync window if needed.
|
|
def sync_later(parent_sync: nil, window_start_date: nil, window_end_date: nil)
|
|
Sync.transaction do
|
|
with_lock do
|
|
sync = self.syncs.incomplete.first
|
|
|
|
if sync
|
|
Rails.logger.info("There is an existing sync, expanding window if needed (#{sync.id})")
|
|
sync.expand_window_if_needed(window_start_date, window_end_date)
|
|
else
|
|
sync = self.syncs.create!(
|
|
parent: parent_sync,
|
|
window_start_date: window_start_date,
|
|
window_end_date: window_end_date
|
|
)
|
|
|
|
SyncJob.perform_later(sync)
|
|
end
|
|
|
|
sync
|
|
end
|
|
end
|
|
end
|
|
|
|
def perform_sync(sync)
|
|
syncer.perform_sync(sync)
|
|
end
|
|
|
|
def perform_post_sync
|
|
syncer.perform_post_sync
|
|
end
|
|
|
|
def broadcast_sync_complete
|
|
sync_broadcaster.broadcast
|
|
end
|
|
|
|
def sync_error
|
|
latest_sync&.error || latest_sync&.children&.map(&:error)&.compact&.first
|
|
end
|
|
|
|
def last_synced_at
|
|
latest_sync&.completed_at
|
|
end
|
|
|
|
def last_sync_created_at
|
|
latest_sync&.created_at
|
|
end
|
|
|
|
private
|
|
def latest_sync
|
|
syncs.ordered.first
|
|
end
|
|
|
|
def syncer
|
|
self.class::Syncer.new(self)
|
|
end
|
|
|
|
def sync_broadcaster
|
|
self.class::SyncCompleteEvent.new(self)
|
|
end
|
|
end
|