From eee99e4d835e094a7894633c5a0c703336a2dca1 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 28 Feb 2026 07:50:07 -0500 Subject: [PATCH] Implement CustomerSyncService for customer synchronization and update CustomerSyncJob to support syncing by ID --- app/jobs/customer_sync_job.rb | 78 ++-------------------- app/models/customer.rb | 35 ++-------- app/services/customer_sync_service.rb | 95 +++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 100 deletions(-) create mode 100644 app/services/customer_sync_service.rb diff --git a/app/jobs/customer_sync_job.rb b/app/jobs/customer_sync_job.rb index 551c08e..313e0bf 100644 --- a/app/jobs/customer_sync_job.rb +++ b/app/jobs/customer_sync_job.rb @@ -12,84 +12,20 @@ class CustomerSyncJob < ApplicationJob queue_as :default retry_on StandardError, wait: 5.minutes, attempts: 5 - def perform(full_sync: false) + # Perform a full sync of all customers, or an incremental sync of only those updated since the last sync + def perform(full_sync: false, id: nil) qbo = Qbo.first return unless qbo - log "Starting #{full_sync ? 'full' : 'incremental'} sync..." + log "Starting #{full_sync ? 'full' : 'incremental'} sync for customer ##{id || 'all'}..." - qbo = Qbo.first - qbo.perform_authenticated_request do |access_token| - - service = Quickbooks::Service::Customer.new(company_id: qbo.realm_id, access_token: access_token) + service = CustomerSyncService.new(qbo: qbo) - page = 1 - loop do - collection = fetch_customers(service, page, full_sync) - entries = Array(collection&.entries) - - break if entries.empty? - - entries.each { |c| sync_customer(c) } - - page += 1 - break if entries.size < 1000 - 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 customers - def fetch_customers(service, page, full_sync) - start_position = (page - 1) * 1000 + 1 - - if full_sync - service.query("SELECT * FROM Customer STARTPOSITION #{start_position} MAXRESULTS 1000") + if id.present? + service.sync_by_id(id) else - last_update = Customer.maximum(:updated_at) || 1.year.ago - service.query(<<~SQL.squish) - SELECT * FROM Customer - WHERE MetaData.LastUpdatedTime > '#{last_update.utc.iso8601}' - STARTPOSITION #{start_position} - MAXRESULTS 1000 - SQL + service.sync(full_sync: full_sync) end - rescue => e - log "Failed to fetch page #{page}: #{e.message}" - nil - end - - # Sync a single customer record - def sync_customer(c) - log "Processing customer #{c.id} / #{c.display_name} (active=#{c.active?})" - - customer = Customer.find_or_initialize_by(id: c.id) - - if c.active? - customer.name = c.display_name - customer.phone_number = c.primary_phone&.free_form_number&.gsub(/\D/, '') - customer.mobile_phone_number = c.mobile_phone&.free_form_number&.gsub(/\D/, '') - - if customer.changed? - customer.save_without_push - log "Updated customer #{c.id}" - end - else - if customer.persisted? && customer.active? - customer.destroy - log "Deleted customer #{c.id}" - end - end - rescue => e - log "Failed to sync customer #{c.id}: #{e.message}" end private diff --git a/app/models/customer.rb b/app/models/customer.rb index 739b189..58d14f2 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -149,12 +149,6 @@ class Customer < ActiveRecord::Base end end end - - # proforms a bruteforce sync operation - # This needs to be simplified - def self.sync - CustomerSyncJob.perform_later(full_sync: false) - end # Seach for customers by name or phone number def self.search(search) @@ -177,30 +171,13 @@ class Customer < ActiveRecord::Base end # proforms a bruteforce sync operation - # This needs to be simplified + def self.sync + CustomerSyncJob.perform_later(full_sync: false) + end + + # proforms a bruteforce sync operation def self.sync_by_id(id) - qbo = Qbo.first - c = qbo.perform_authenticated_request do |access_token| - service = Quickbooks::Service::Customer.new(company_id: qbo.realm_id, access_token: access_token) - service.fetch_by_id(id) - end - - return unless c - - customer = Customer.find_or_create_by(id: c.id) - if c.active? - #if not customer.name.eql? c.display_name - customer.name = c.display_name - customer.id = c.id - customer.phone_number = c.primary_phone.free_form_number.tr('^0-9', '') unless c.primary_phone.nil? - customer.mobile_phone_number = c.mobile_phone.free_form_number.tr('^0-9', '') unless c.mobile_phone.nil? - customer.save_without_push - #end - else - if not customer.new_record? - customer.delete - end - end + CustomerSyncJob.perform_later(id: id) end # returns a human readable string diff --git a/app/services/customer_sync_service.rb b/app/services/customer_sync_service.rb new file mode 100644 index 0000000..23919ee --- /dev/null +++ b/app/services/customer_sync_service.rb @@ -0,0 +1,95 @@ +#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 CustomerSyncService + PAGE_SIZE = 1000 + + def initialize(qbo:) + @qbo = qbo + end + + # Sync all customers, or only those updated since the last sync + def sync(full_sync: false) + log "Starting #{full_sync ? 'full' : 'incremental'} customer sync" + + @qbo.perform_authenticated_request do |access_token| + service = Quickbooks::Service::Customer.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 "Customer sync complete" + end + + # Sync a single customer by its QBO ID, used for webhook updates + def sync_by_id(id) + @qbo.perform_authenticated_request do |access_token| + service = Quickbooks::Service::Customer.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 customers, either all or only those updated since the last sync + def fetch_page(service, page, full_sync) + start_position = (page - 1) * PAGE_SIZE + 1 + + if full_sync + service.query("SELECT * FROM Customer STARTPOSITION #{start_position} MAXRESULTS #{PAGE_SIZE}") + else + last_update = Customer.maximum(:qbo_updated_at) || 1.year.ago + service.query(<<~SQL.squish) + SELECT * FROM Customer + WHERE MetaData.LastUpdatedTime > '#{last_update.utc.iso8601}' + STARTPOSITION #{start_position} + MAXRESULTS #{PAGE_SIZE} + SQL + end + end + + # Create or update a local Customer record based on the QBO remote data + def persist(remote) + local = Customer.find_or_initialize_by(id: remote.id) + + if remote.active? + local.name = remote.display_name + local.phone_number = remote.primary_phone&.free_form_number&.gsub(/\D/, '') + local.mobile_phone_number = remote.mobile_phone&.free_form_number&.gsub(/\D/, '') + + if local.changed? + local.save + log "Updated customer #{remote.id}" + end + else + if local.persisted? + local.destroy + log "Deleted customer #{remote.id}" + end + end + rescue => e + log "Failed to sync customer #{remote.id}: #{e.message}" + end + + def log(msg) + Rails.logger.info "[CustomerSyncService] #{msg}" + end +end \ No newline at end of file