mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
Reworked push on save and refactored variable names
This commit is contained in:
@@ -15,6 +15,10 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
self.abstract_class = true
|
self.abstract_class = true
|
||||||
validates_presence_of :id
|
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.
|
# Returns the details of the entity.
|
||||||
# If the details have already been fetched, it returns the cached version.
|
# 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.
|
# 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
|
xml = Rails.cache.fetch(details_cache_key, expires_in: 10.minutes) do
|
||||||
fetch_details.to_xml_ns
|
fetch_details.to_xml_ns
|
||||||
end
|
end
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
qbo_model_class.from_xml(xml)
|
||||||
model_class.from_xml(xml)
|
|
||||||
end
|
end
|
||||||
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
|
def details_cache_key
|
||||||
"#{model_name.name}:#{id}:qbo_details:#{updated_at.to_i}"
|
"#{model_name.name}:#{id}:qbo_details:#{updated_at.to_i}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Returns the last sync time formatted for display.
|
# Returns the last sync time formatted for display.
|
||||||
# If no sync has occurred, returns a default message.
|
# If no sync has occurred, returns a default message.
|
||||||
def self.last_sync
|
def self.last_sync
|
||||||
@@ -45,8 +47,7 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
# Magic Method
|
# Magic Method
|
||||||
# Maps Get/Set methods to QBO entity object
|
# Maps Get/Set methods to QBO entity object
|
||||||
def method_missing(method_name, *args, &block)
|
def method_missing(method_name, *args, &block)
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
if qbo_model_class.method_defined?(method_name)
|
||||||
if model_class.method_defined?(method_name)
|
|
||||||
details
|
details
|
||||||
@details.public_send(method_name, *args, &block)
|
@details.public_send(method_name, *args, &block)
|
||||||
else
|
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.
|
# 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.
|
# 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)
|
def respond_to_missing?(method_name, include_private = false)
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
qbo_model_class.method_defined?(method_name) || super
|
||||||
model_class.method_defined?(method_name) || super
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sync all entities, typically triggered by a scheduled task or manual sync request
|
# 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)
|
job.perform_later(id: id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Push the updates
|
def skip_qbo_push?
|
||||||
def save_with_push
|
!!skip_qbo_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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :save_without_push, :save
|
|
||||||
alias_method :save, :save_with_push
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def log(msg)
|
def log(msg)
|
||||||
@@ -96,8 +87,23 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
def fetch_details
|
def fetch_details
|
||||||
log "Fetching details for #{model_name.name} ##{id} from QBO..."
|
log "Fetching details for #{model_name.name} ##{id} from QBO..."
|
||||||
qbo = QboConnectionService.current!
|
qbo = QboConnectionService.current!
|
||||||
service_class = "#{model_name.name}Service".constantize
|
service_class.new(qbo: qbo, local: self).pull()
|
||||||
service_class.new(qbo: qbo, remote: 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
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -10,22 +10,17 @@
|
|||||||
|
|
||||||
class ServiceBase
|
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.
|
# 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.
|
# If no local is provided, the service will not perform any operations.
|
||||||
def initialize(qbo:, remote: nil)
|
def initialize(qbo:, local: nil)
|
||||||
raise "No QBO configuration found" unless qbo
|
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
|
@qbo = qbo
|
||||||
@entity = remote.class.name
|
@entity = local.class.name
|
||||||
@remote = remote
|
@local = local
|
||||||
end
|
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
|
# Subclasses must implement this to build a new QBO entity if a remote is not found
|
||||||
def build_qbo_remote
|
def build_qbo_remote
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@@ -33,9 +28,9 @@ class ServiceBase
|
|||||||
|
|
||||||
# Pulls the Item data from QuickBooks Online.
|
# Pulls the Item data from QuickBooks Online.
|
||||||
def pull
|
def pull
|
||||||
return build_qbo_remote unless @remote.present?
|
return build_qbo_remote unless @local.present?
|
||||||
return build_qbo_remote unless @remote.id
|
return build_qbo_remote unless @local.id
|
||||||
log "Fetching details for #{@entity} ##{@remote.id} from QBO..."
|
log "Fetching details for #{@entity} ##{@local.id} from QBO..."
|
||||||
qbo = QboConnectionService.current!
|
qbo = QboConnectionService.current!
|
||||||
qbo.perform_authenticated_request do |access_token|
|
qbo.perform_authenticated_request do |access_token|
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
service_class = "Quickbooks::Service::#{@entity}".constantize
|
||||||
@@ -43,16 +38,19 @@ class ServiceBase
|
|||||||
company_id: qbo.realm_id,
|
company_id: qbo.realm_id,
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
service.fetch_by_id(@remote.id)
|
service.fetch_by_id(@local.id)
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
log "Fetch failed for #{@remote.id}: #{e.message}"
|
log "Fetch failed for #{@local.id}: #{e.message}"
|
||||||
build_qbo_remote
|
build_qbo_remote
|
||||||
end
|
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
|
def push
|
||||||
log "Pushing #{@entity} ##{@remote.id} to QBO..."
|
log "Pushing #{@entity} ##{@local.id} to QBO..."
|
||||||
|
|
||||||
remote = @qbo.perform_authenticated_request do |access_token|
|
remote = @qbo.perform_authenticated_request do |access_token|
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
service_class = "Quickbooks::Service::#{@entity}".constantize
|
||||||
@@ -60,16 +58,18 @@ class ServiceBase
|
|||||||
company_id: @qbo.realm_id,
|
company_id: @qbo.realm_id,
|
||||||
access_token: access_token
|
access_token: access_token
|
||||||
)
|
)
|
||||||
if @remote.id.present?
|
if @local.id.present?
|
||||||
service.update(@remote.details)
|
log "Updating #{@entity}"
|
||||||
|
service.update(@local.details)
|
||||||
else
|
else
|
||||||
service.create(@remote.details)
|
log "Creating #{@entity}"
|
||||||
|
service.create(@local.details)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@remote.id = remote.id unless @remote.persisted?
|
@local.id = remote.id unless @local.persisted?
|
||||||
log "Push for remote ##{@remote.id} completed."
|
log "Push for remote ##{@local.id} completed."
|
||||||
return @remote
|
return @local
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ class SyncServiceBase
|
|||||||
@qbo.perform_authenticated_request do |access_token|
|
@qbo.perform_authenticated_request do |access_token|
|
||||||
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
||||||
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
||||||
|
|
||||||
remote = service.fetch_by_id(id)
|
remote = service.fetch_by_id(id)
|
||||||
persist(remote)
|
persist(remote)
|
||||||
end
|
end
|
||||||
@@ -111,7 +110,8 @@ class SyncServiceBase
|
|||||||
process_attributes(local, remote)
|
process_attributes(local, remote)
|
||||||
|
|
||||||
if local.changed?
|
if local.changed?
|
||||||
local.save!
|
local.skip_qbo_push = true
|
||||||
|
local.save
|
||||||
log "Updated #{@entity.name} #{remote.id}"
|
log "Updated #{@entity.name} #{remote.id}"
|
||||||
attach_documents(local, remote)
|
attach_documents(local, remote)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user