diff --git a/app/models/qbo_base_model.rb b/app/models/qbo_base_model.rb index 198c9cb..c681b77 100644 --- a/app/models/qbo_base_model.rb +++ b/app/models/qbo_base_model.rb @@ -15,6 +15,10 @@ class QboBaseModel < ActiveRecord::Base self.abstract_class = true validates_presence_of :id + attr_accessor :skip_qbo_push + before_validation :push_to_qbo, on: :create + after_commit :push_to_qbo, on: :update, unless: :skip_qbo_push? + # Returns the details of the entity. # If the details have already been fetched, it returns the cached version. # Otherwise, it fetches the details from QuickBooks Online and caches them for future use. @@ -24,17 +28,15 @@ class QboBaseModel < ActiveRecord::Base xml = Rails.cache.fetch(details_cache_key, expires_in: 10.minutes) do fetch_details.to_xml_ns end - model_class = "Quickbooks::Model::#{model_name.name}".constantize - model_class.from_xml(xml) + qbo_model_class.from_xml(xml) end end - # Generates a unique cache key for storing this customer's QBO details. + # Generates a unique cache key for storing this customer's QBO details. def details_cache_key "#{model_name.name}:#{id}:qbo_details:#{updated_at.to_i}" end - # Returns the last sync time formatted for display. # If no sync has occurred, returns a default message. def self.last_sync @@ -45,8 +47,7 @@ class QboBaseModel < ActiveRecord::Base # Magic Method # Maps Get/Set methods to QBO entity object def method_missing(method_name, *args, &block) - model_class = "Quickbooks::Model::#{model_name.name}".constantize - if model_class.method_defined?(method_name) + if qbo_model_class.method_defined?(method_name) details @details.public_send(method_name, *args, &block) else @@ -57,8 +58,7 @@ class QboBaseModel < ActiveRecord::Base # Repsonds to missing methods by delegating to the QBO customer details object if the method is defined there. # This allows for dynamic access to any attributes or methods of the QBO customer without having to explicitly define them in the Subclass model, providing flexibility and reducing boilerplate code. def respond_to_missing?(method_name, include_private = false) - model_class = "Quickbooks::Model::#{model_name.name}".constantize - model_class.method_defined?(method_name) || super + qbo_model_class.method_defined?(method_name) || super end # Sync all entities, typically triggered by a scheduled task or manual sync request @@ -73,19 +73,10 @@ class QboBaseModel < ActiveRecord::Base job.perform_later(id: id) end - # Push the updates - def save_with_push - log "Starting push for #{model_name.name} ##{self.id}..." - qbo = QboConnectionService.current! - service = "#{model_name.name}Service".constantize - service.new(qbo: qbo, remote: self).push() - Rails.cache.delete(details_cache_key) - save_without_push + def skip_qbo_push? + !!skip_qbo_push end - alias_method :save_without_push, :save - alias_method :save, :save_with_push - private def log(msg) @@ -96,8 +87,23 @@ class QboBaseModel < ActiveRecord::Base def fetch_details log "Fetching details for #{model_name.name} ##{id} from QBO..." qbo = QboConnectionService.current! - service_class = "#{model_name.name}Service".constantize - service_class.new(qbo: qbo, remote: self).pull() + service_class.new(qbo: qbo, local: self).pull() + end + + def push_to_qbo + log "Starting push for #{model_name.name} ##{id}..." + qbo = QboConnectionService.current! + reslut = service_class.new(qbo: qbo, local: self).push + Rails.cache.delete(details_cache_key) + return reslut + end + + def qbo_model_class + @qbo_model_class ||= "Quickbooks::Model::#{model_name.name}".constantize + end + + def service_class + @service_class ||= "#{model_name.name}Service".constantize end end \ No newline at end of file diff --git a/app/services/service_base.rb b/app/services/service_base.rb index 001bfc3..1fef142 100644 --- a/app/services/service_base.rb +++ b/app/services/service_base.rb @@ -10,22 +10,17 @@ class ServiceBase - # Subclasses should Initializes the service with a QBO client and an optional remote record. + # Subclasses should Initializes the service with a QBO client and a local record. # The QBO client is used to communicate with QuickBooks Online, while the local record contains the data that needs to be pushed to QBO. - # If no remote is provided, the service will not perform any operations. - def initialize(qbo:, remote: nil) + # If no local is provided, the service will not perform any operations. + def initialize(qbo:, local: nil) raise "No QBO configuration found" unless qbo - raise "#{@entity} record is required for push operation" unless remote + raise "#{@entity} record is required for push operation" unless local @qbo = qbo - @entity = remote.class.name - @remote = remote + @entity = local.class.name + @local = local end - # # Subclasses must implement this to specify which local model they sync (e.g. Customer, Invoice) - # def self.model_class - # raise NotImplementedError - # end - # Subclasses must implement this to build a new QBO entity if a remote is not found def build_qbo_remote raise NotImplementedError @@ -33,9 +28,9 @@ class ServiceBase # Pulls the Item data from QuickBooks Online. def pull - return build_qbo_remote unless @remote.present? - return build_qbo_remote unless @remote.id - log "Fetching details for #{@entity} ##{@remote.id} from QBO..." + return build_qbo_remote unless @local.present? + return build_qbo_remote unless @local.id + log "Fetching details for #{@entity} ##{@local.id} from QBO..." qbo = QboConnectionService.current! qbo.perform_authenticated_request do |access_token| service_class = "Quickbooks::Service::#{@entity}".constantize @@ -43,16 +38,19 @@ class ServiceBase company_id: qbo.realm_id, access_token: access_token ) - service.fetch_by_id(@remote.id) + service.fetch_by_id(@local.id) end rescue => e - log "Fetch failed for #{@remote.id}: #{e.message}" + log "Fetch failed for #{@local.id}: #{e.message}" build_qbo_remote end - # Pushes the Item data to QuickBooks Online. This method handles the communication with QBO, including authentication and error handling. It uses the QBO client to send the remote data and logs the process for monitoring and debugging purposes. If the push is successful, it returns the remote record; otherwise, it logs the error and returns false. + # Pushes the Item data to QuickBooks Online. + # This method handles the communication with QBO, including authentication and error handling. + # It uses the QBO client to send the remote data and logs the process for monitoring and debugging purposes. + # If the push is successful, it returns the remote record; otherwise, it logs the error and returns false. def push - log "Pushing #{@entity} ##{@remote.id} to QBO..." + log "Pushing #{@entity} ##{@local.id} to QBO..." remote = @qbo.perform_authenticated_request do |access_token| service_class = "Quickbooks::Service::#{@entity}".constantize @@ -60,16 +58,18 @@ class ServiceBase company_id: @qbo.realm_id, access_token: access_token ) - if @remote.id.present? - service.update(@remote.details) + if @local.id.present? + log "Updating #{@entity}" + service.update(@local.details) else - service.create(@remote.details) + log "Creating #{@entity}" + service.create(@local.details) end end - @remote.id = remote.id unless @remote.persisted? - log "Push for remote ##{@remote.id} completed." - return @remote + @local.id = remote.id unless @local.persisted? + log "Push for remote ##{@local.id} completed." + return @local end private diff --git a/app/services/sync_service_base.rb b/app/services/sync_service_base.rb index 440edbc..754036d 100644 --- a/app/services/sync_service_base.rb +++ b/app/services/sync_service_base.rb @@ -59,7 +59,6 @@ class SyncServiceBase @qbo.perform_authenticated_request do |access_token| service_class = "Quickbooks::Service::#{@entity.name}".constantize service = service_class.new(company_id: @qbo.realm_id, access_token: access_token) - remote = service.fetch_by_id(id) persist(remote) end @@ -111,7 +110,8 @@ class SyncServiceBase process_attributes(local, remote) if local.changed? - local.save! + local.skip_qbo_push = true + local.save log "Updated #{@entity.name} #{remote.id}" attach_documents(local, remote) end