Refactor EstimateSyncJob to support syncing by ID and document number; add EstimateSyncService for handling estimate synchronization

This commit is contained in:
2026-02-28 08:24:25 -05:00
parent a80f59cc45
commit 19911b7940
3 changed files with 123 additions and 101 deletions

View File

@@ -12,84 +12,25 @@ class EstimateSyncJob < ApplicationJob
queue_as :default
retry_on StandardError, wait: 5.minutes, attempts: 5
# Sync estimates from QuickBooks Online to local database
def perform(full_sync: false)
def perform(full_sync: false, id: nil, doc_number: nil)
qbo = Qbo.first
return unless qbo
log "Starting #{full_sync ? 'full' : 'incremental'} sync..."
log "Starting #{full_sync ? 'full' : 'incremental'} sync for estimate ##{id || doc_number || 'all'}..."
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = EstimateSyncService.new(qbo: qbo)
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
page = 1
loop do
collection = fetch_estimates(service, page, full_sync)
entries = Array(collection&.entries)
break if entries.empty?
entries.each { |c| sync_estimates(c) }
page += 1
break if entries.size < 1000
if id.present?
service.sync_by_id(id)
elsif doc_number.present?
service.sync_by_doc(doc_number)
else
service.sync(full_sync: full_sync)
end
log "Completed sync."
Qbo.update_time_stamp
end
rescue => e
log "Fatal error: #{e.message}"
log e.backtrace.join("\n")
raise # allows retry
end
private
# Fetch either all or incremental estimates
def fetch_estimates(service, page, full_sync)
start_position = (page - 1) * 1000 + 1
if full_sync
service.query("SELECT * FROM Estimate STARTPOSITION #{start_position} MAXRESULTS 1000")
else
last_update = Estimate.maximum(:updated_at) || 1.year.ago
service.query(<<~SQL.squish)
SELECT * FROM Estimate
WHERE MetaData.LastUpdatedTime > '#{last_update.utc.iso8601}'
STARTPOSITION #{start_position}
MAXRESULTS 1000
SQL
end
rescue => e
log "Failed to fetch page #{page}: #{e.message}"
nil
end
# Sync a single estimate record
def sync_estimates(e)
log "Processing estimate #{e.id} doc=#{e.doc_number} status=#{e.txn_status}"
estimate = Estimate.find_or_initialize_by(id: e.id)
estimate.doc_number = e.doc_number
estimate.estimate_id = e.estimate_ref.value
estimate.txn_date = e.txn_date
if estimate.changed?
estimate.save
log "Updated estimate #{e.id}"
end
end
# TODO remove deleted estimates
rescue => error
log "Failed to sync estimate #{e.id}: #{error.message}"
end
def log(msg)
Rails.logger.info "[EstimateSyncJob] #{msg}"
end

View File

@@ -32,43 +32,12 @@ class Estimate < ActiveRecord::Base
# sync only one estimate
def self.sync_by_doc_number(number)
log "Syncing estimate by doc number #{number}"
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
process_estimate(service.find_by( :doc_number, number).first)
end
end
# update an estimate
def self.update(id)
qbo = Qbo.first
estimate = qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
service.fetch_by_id(id)
end
return unless estimate
e = find_or_create_by(id: id)
e.doc_number = estimate.doc_number
e.txn_date = estimate.txn_date
e.save!
end
# process an estimate into the database
def self.process_estimate(qbo_estimate)
log "Processing estimate #{qbo_estimate.id}"
estimate = find_or_create_by(id: qbo_estimate.id)
estimate.doc_number = qbo_estimate.doc_number
estimate.customer_id = qbo_estimate.customer_ref.value
estimate.id = qbo_estimate.id
estimate.txn_date = qbo_estimate.txn_date
estimate.save!
EstimateSyncJob.perform_later(doc_number: number)
end
# download the pdf from quickbooks
def pdf
log "Downloading PDF for estimate ##{self.id}..."
qbo = Qbo.first
qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: qbo.realm_id, access_token: access_token)
@@ -99,6 +68,7 @@ class Estimate < ActiveRecord::Base
# pull the details
def pull
log "Pulling details for estimate ##{self.id}..."
begin
raise Exception unless self.id
qbo = Qbo.first

View File

@@ -0,0 +1,111 @@
#The MIT License (MIT)
#
#Copyright (c) 2016 - 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class EstimateSyncService
PAGE_SIZE = 1000
def initialize(qbo:)
@qbo = qbo
end
# Sync all estimates, or only those updated since the last sync
def sync(full_sync: false)
log "Starting #{full_sync ? 'full' : 'incremental'} estimate sync"
@qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: @qbo.realm_id, access_token: access_token)
page = 1
loop do
collection = fetch_page(service, page, full_sync)
entries = Array(collection&.entries)
break if entries.empty?
entries.each { |remote| persist(remote) }
break if entries.size < PAGE_SIZE
page += 1
end
end
log "Estimate sync complete"
end
# Sync a single estimate by its QBO ID, used for webhook updates
def sync_by_doc(doc_number)
log "Syncing estimate by doc_number: #{doc_number}"
@qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: @qbo.realm_id, access_token: access_token)
remote = service.find_by( :doc_number, doc_number).first
log "Found estimate with ID #{remote.id} for doc_number #{doc_number}" if remote
persist(remote)
end
end
# Sync a single estimate by its QBO ID, used for webhook updates
def sync_by_id(id)
log "Syncing estimate by ID: #{id}"
@qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Estimate.new(company_id: @qbo.realm_id, access_token: access_token)
remote = service.fetch_by_id(id)
persist(remote)
end
end
private
# Fetch a page of estimates, either all or only those updated since the last sync
def fetch_page(service, page, full_sync)
log "Fetching page #{page} of estimates (full_sync: #{full_sync})"
start_position = (page - 1) * PAGE_SIZE + 1
if full_sync
service.query("SELECT * FROM Estimate STARTPOSITION #{start_position} MAXRESULTS #{PAGE_SIZE}")
else
last_update = Estimate.maximum(:updated_at) || 1.year.ago
service.query(<<~SQL.squish)
SELECT * FROM Estimate
WHERE MetaData.LastUpdatedTime > '#{last_update.utc.iso8601}'
STARTPOSITION #{start_position}
MAXRESULTS #{PAGE_SIZE}
SQL
end
end
# Create or update a local Estimate record based on the QBO remote data
def persist(remote)
log "Persisting estimate #{remote.id}"
local = Estimate.find_or_initialize_by(id: remote.id)
#if remote.txn_status?
local.doc_number = remote.doc_number
local.txn_date = remote.txn_date
local.customer = Customer.find_by(id: remote.customer_ref&.value)
if local.changed?
local.save
log "Updated estimate #{remote.id}"
end
# TODO handle deleted estimates
#else
# if local.persisted?
# local.destroy
# log "Deleted estimate #{remote.id}"
# end
# end
rescue => e
log "Failed to sync estimate #{remote.id}: #{e.message}"
end
def log(msg)
Rails.logger.info "[EstimateSyncService] #{msg}"
end
end