2024-07-25 12:51:50 -04:00
|
|
|
module AutoSync
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2024-11-15 13:49:37 -05:00
|
|
|
before_action :sync_family, if: :family_needs_auto_sync?
|
2024-07-25 12:51:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def sync_family
|
2024-11-15 13:49:37 -05:00
|
|
|
Current.family.sync_later
|
|
|
|
end
|
|
|
|
|
|
|
|
def family_needs_auto_sync?
|
2025-05-13 16:14:29 -04:00
|
|
|
return false unless Current.family&.accounts&.active&.any?
|
|
|
|
return false if (Current.family.last_sync_created_at&.to_date || 1.day.ago) >= Date.current
|
2025-05-18 15:02:51 -04:00
|
|
|
return false unless Current.family.auto_sync_on_login
|
2024-11-15 13:49:37 -05:00
|
|
|
|
2025-05-13 16:14:29 -04:00
|
|
|
Rails.logger.info "Auto-syncing family #{Current.family.id}, last sync was #{Current.family.last_sync_created_at}"
|
|
|
|
|
|
|
|
true
|
2024-07-25 12:51:50 -04:00
|
|
|
end
|
|
|
|
end
|