mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* Domain model sketch * Scaffold out rules domain * Migrations * Remove existing data enrichment for clean slate * Sketch out business logic and basic tests * Simplify rule scope building and action executions * Get generator working again * Basic implementation + tests * Remove manual merchant management (rules will replace) * Revert "Remove manual merchant management (rules will replace)" This reverts commit 83dcbd9ff0aa7bbee211796b71aa48b71df5e57e. * Family and Provider merchants model * Fix brakeman warnings * Fix notification loader * Update notification position * Add Rule action and condition registries * Rule form with compound conditions and tests * Split out notification types, add CTA type * Rules form builder and Stimulus controller * Clean up rule registry domain * Clean up rules stimulus controller * CTA message for rule when user changes transaction category * Fix tests * Lint updates * Centralize notifications in Notifiable concern * Implement category rule prompts with auto backoff and option to disable * Fix layout bug caused by merge conflict * Initialize rule with correct action for category CTA * Add rule deletions, get rules working * Complete dynamic rule form, split Stimulus controllers by resource * Fix failing tests * Change test password to avoid chromium conflicts * Update integration tests * Centralize all test password references * Add re-apply rule action * Rule confirm modal * Run migrations * Trigger rule notification after inline category updates * Clean up rule styles * Basic attribute locking for rules * Apply attribute locks on user edits * Log data enrichments, only apply rules to unlocked attributes * Fix merge errors * Additional merge conflict fixes * Form UI improvements, ignore attribute locks on manual rule application * Batch AI auto-categorization of transactions * Auto merchant detection, ai enrichment in batches * Fix Plaid merchant assignments * Plaid category matching * Cleanup 1 * Test cleanup * Remove stale route * Fix desktop chat UI issues * Fix mobile nav styling issues
95 lines
2.2 KiB
Ruby
95 lines
2.2 KiB
Ruby
class Sync < ApplicationRecord
|
|
Error = Class.new(StandardError)
|
|
|
|
belongs_to :syncable, polymorphic: true
|
|
|
|
belongs_to :parent, class_name: "Sync", optional: true
|
|
has_many :children, class_name: "Sync", foreign_key: :parent_id, dependent: :destroy
|
|
|
|
enum :status, { pending: "pending", syncing: "syncing", completed: "completed", failed: "failed" }
|
|
|
|
scope :ordered, -> { order(created_at: :desc) }
|
|
|
|
def child?
|
|
parent_id.present?
|
|
end
|
|
|
|
def perform
|
|
Rails.logger.tagged("Sync", id, syncable_type, syncable_id) do
|
|
start!
|
|
|
|
begin
|
|
data = syncable.sync_data(self, start_date: start_date)
|
|
update!(data: data) if data
|
|
|
|
complete! unless has_pending_child_syncs?
|
|
|
|
Rails.logger.info("Sync completed, starting post-sync")
|
|
|
|
syncable.post_sync(self) unless has_pending_child_syncs?
|
|
|
|
if has_parent?
|
|
notify_parent_of_completion!
|
|
end
|
|
|
|
Rails.logger.info("Post-sync completed")
|
|
rescue StandardError => error
|
|
fail! error
|
|
raise error if Rails.env.development?
|
|
end
|
|
end
|
|
end
|
|
|
|
def handle_child_completion_event
|
|
unless has_pending_child_syncs?
|
|
if has_failed_child_syncs?
|
|
fail!(Error.new("One or more child syncs failed"))
|
|
else
|
|
complete!
|
|
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
|
|
end
|
|
|
|
def complete!
|
|
Rails.logger.info("Sync completed")
|
|
update! status: :completed, last_ran_at: Time.current
|
|
end
|
|
|
|
def fail!(error)
|
|
Rails.logger.error("Sync failed: #{error.message}")
|
|
|
|
Sentry.capture_exception(error) do |scope|
|
|
scope.set_context("sync", { id: id, syncable_type: syncable_type, syncable_id: syncable_id })
|
|
end
|
|
|
|
update!(
|
|
status: :failed,
|
|
error: error.message,
|
|
last_ran_at: Time.current
|
|
)
|
|
end
|
|
end
|