From 6f8af9bba8b2053e8371f62b30542a70f4dde76b Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Fri, 27 Feb 2026 23:07:12 -0500 Subject: [PATCH] Implement Employee synchronization; add EmployeeSyncJob and EmployeeSyncService for improved background processing and logging --- app/controllers/qbo_controller.rb | 12 +-- app/jobs/employee_sync_job.rb | 35 +++++++++ app/jobs/webhook_process_job.rb | 1 + app/models/employee.rb | 34 ++------- app/services/employee_sync_service.rb | 93 ++++++++++++++++++++++++ db/migrate/042_add_employee_timestamp.rb | 15 ++++ 6 files changed, 150 insertions(+), 40 deletions(-) create mode 100644 app/jobs/employee_sync_job.rb create mode 100644 app/services/employee_sync_service.rb create mode 100644 db/migrate/042_add_employee_timestamp.rb diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index 5caf53e..26eecf8 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -96,17 +96,7 @@ class QboController < ApplicationController CustomerSyncJob.perform_later(full_sync: true) EstimateSyncJob.perform_later(full_sync: true) InvoiceSyncJob.perform_later(full_sync: true) - - # Update info in background - Thread.new do - if Qbo.exists? - Employee.sync - - # Record the last sync time - Qbo.update_time_stamp - end - ActiveRecord::Base.connection.close - end + EmployeeSyncJob.perform_later(full_sync: true) redirect_to :home, flash: { notice: I18n.t(:label_syncing) } end diff --git a/app/jobs/employee_sync_job.rb b/app/jobs/employee_sync_job.rb new file mode 100644 index 0000000..49a6e04 --- /dev/null +++ b/app/jobs/employee_sync_job.rb @@ -0,0 +1,35 @@ +#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 EmployeeSyncJob < ApplicationJob + queue_as :default + retry_on StandardError, wait: 5.minutes, attempts: 5 + + def perform(full_sync: false, id: nil) + qbo = Qbo.first + return unless qbo + + log "Starting #{full_sync ? 'full' : 'incremental'} sync for employee ##{id || 'all'}..." + + service = EmployeeSyncService.new(qbo: qbo) + + if id.present? + service.sync_by_id(id) + else + service.sync(full_sync: full_sync) + end + end + + private + + def log(msg) + Rails.logger.info "[EmployeeSyncJob] #{msg}" + end +end \ No newline at end of file diff --git a/app/jobs/webhook_process_job.rb b/app/jobs/webhook_process_job.rb index 6fa3058..5d4395f 100644 --- a/app/jobs/webhook_process_job.rb +++ b/app/jobs/webhook_process_job.rb @@ -15,6 +15,7 @@ class WebhookProcessJob < ActiveJob::Base Customer Invoice Estimate + Employee ].freeze # Process incoming QBO webhook notifications and sync relevant data to Redmine diff --git a/app/models/employee.rb b/app/models/employee.rb index 87cace8..1b89ece 100644 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -12,38 +12,14 @@ class Employee < ActiveRecord::Base has_many :users validates_presence_of :id, :name - - def self.sync - qbo = Qbo.first - employees = qbo.perform_authenticated_request do |access_token| - service = Quickbooks::Service::Employee.new(company_id: qbo.realm_id, access_token: access_token) - service.all - end - return unless employees - - transaction do - employees.each { |e| - logger.info "Processing employee #{e.id}" - employee = find_or_create_by(id: e.id) - employee.name = e.display_name - employee.id = e.id - employee.save! - } - end + def self.sync + EmployeeSyncJob.perform_later(full_sync: true) end + # Sync a single employee by ID, typically triggered by a webhook notification or manual sync request def self.sync_by_id(id) - qbo = Qbo.first - employee = qbo.perform_authenticated_request do |access_token| - service = Quickbooks::Service::Employee.new(company_id: qbo.realm_id, access_token: access_token) - service.fetch_by_id(id) - end - - return unless employee - employee = find_or_create_by(id: employee.id) - employee.name = employee.display_name - employee.id = employee.id - employee.save! + EmployeeSyncJob.perform_later(id: id) end + end diff --git a/app/services/employee_sync_service.rb b/app/services/employee_sync_service.rb new file mode 100644 index 0000000..eb11d0b --- /dev/null +++ b/app/services/employee_sync_service.rb @@ -0,0 +1,93 @@ +#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 EmployeeSyncService + PAGE_SIZE = 1000 + + def initialize(qbo:) + @qbo = qbo + end + + # Sync all employees, or only those updated since the last sync + def sync(full_sync: false) + log "Starting #{full_sync ? 'full' : 'incremental'} employee sync" + + @qbo.perform_authenticated_request do |access_token| + service = Quickbooks::Service::Employee.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 "Employee sync complete" + end + + # Sync a single employee by its QBO ID, used for webhook updates + def sync_by_id(id) + @qbo.perform_authenticated_request do |access_token| + service = Quickbooks::Service::Employee.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 employees, 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 Employee STARTPOSITION #{start_position} MAXRESULTS #{PAGE_SIZE}") + else + last_update = Employee.maximum(:qbo_updated_at) || 1.year.ago + service.query(<<~SQL.squish) + SELECT * FROM Employee + WHERE MetaData.LastUpdatedTime > '#{last_update.utc.iso8601}' + STARTPOSITION #{start_position} + MAXRESULTS #{PAGE_SIZE} + SQL + end + end + + # Create or update a local Employee record based on the QBO remote data + def persist(remote) + employee = Employee.find_or_initialize_by(id: remote.id) + + if remote.active? + employee.name = remote.display_name + + if employee.changed? + employee.save + log "Updated employee #{remote.id}" + end + else + if employee.persisted? + employee.destroy + log "Deleted employee #{remote.id}" + end + end + rescue => e + log "Failed to sync employee #{remote.id}: #{e.message}" + end + + def log(msg) + Rails.logger.info "[EmployeeSyncService] #{msg}" + end +end \ No newline at end of file diff --git a/db/migrate/042_add_employee_timestamp.rb b/db/migrate/042_add_employee_timestamp.rb new file mode 100644 index 0000000..7422b08 --- /dev/null +++ b/db/migrate/042_add_employee_timestamp.rb @@ -0,0 +1,15 @@ +#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 AddEmployeeTimestamp < ActiveRecord::Migration[7.0] + def change + add_timestamps(:employees, null: true) + end +end