Moved QBO fetch from customer model into service

This commit is contained in:
2026-03-03 19:49:36 -05:00
parent be400c2b2a
commit 4403267abb
2 changed files with 22 additions and 14 deletions

View File

@@ -164,9 +164,9 @@ class Customer < ActiveRecord::Base
# Push the updates # Push the updates
def save_with_push def save_with_push
qbo = QboConnectionService.current!
log "Starting push for customer ##{self.id}..." log "Starting push for customer ##{self.id}..."
CustomerPushService.new(qbo: qbo, customer: self).push() qbo = QboConnectionService.current!
CustomerService.new(qbo: qbo, customer: self).push()
save_without_push save_without_push
end end
@@ -180,16 +180,7 @@ class Customer < ActiveRecord::Base
return Quickbooks::Model::Customer.new unless id.present? return Quickbooks::Model::Customer.new unless id.present?
log "Fetching details for customer ##{id} from QBO..." log "Fetching details for customer ##{id} from QBO..."
qbo = QboConnectionService.current! qbo = QboConnectionService.current!
qbo.perform_authenticated_request do |access_token| CustomerService.new(qbo: qbo, customer: self).pull()
service = Quickbooks::Service::Customer.new(
company_id: qbo.realm_id,
access_token: access_token
)
service.fetch_by_id(id)
end
rescue => e
log "Fetch failed for #{id}: #{e.message}"
Quickbooks::Model::Customer.new
end end
# Log messages with the entity type for better traceability # Log messages with the entity type for better traceability

View File

@@ -8,7 +8,7 @@
# #
#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. #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 CustomerPushService class CustomerService
# Initializes the service with a QBO client and an optional customer record. The QBO client is used to communicate with QuickBooks Online, while the customer record contains the data that needs to be pushed to QBO. If no customer is provided, the service will not perform any operations. # Initializes the service with a QBO client and an optional customer record. The QBO client is used to communicate with QuickBooks Online, while the customer record contains the data that needs to be pushed to QBO. If no customer is provided, the service will not perform any operations.
def initialize(qbo:, customer: nil) def initialize(qbo:, customer: nil)
@@ -18,7 +18,24 @@ class CustomerPushService
@customer = customer @customer = customer
end end
# Pushes the customer data to QuickBooks Online. This method handles the communication with QBO, including authentication and error handling. It uses the QBO client to send the customer data and logs the process for monitoring and debugging purposes. If the push is successful, it returns the customer record; otherwise, it logs the error and returns false. # Pulls the customer data from QuickBooks Online.
def pull
return Quickbooks::Model::Customer.new unless @customer.present?
log "Fetching details for customer ##{@customer.id} from QBO..."
qbo = QboConnectionService.current!
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(@customer.id)
end
rescue => e
log "Fetch failed for #{@customer.id}: #{e.message}"
Quickbooks::Model::Customer.new
end
# Pushes the customer data to QuickBooks Online. This method handles the communication with QBO, including authentication and error handling. It uses the QBO client to send the customer data and logs the process for monitoring and debugging purposes. If the push is successful, it returns the customer record; otherwise, it logs the error and returns false.
def push def push
log "Pushing customer ##{@customer.id} to QBO..." log "Pushing customer ##{@customer.id} to QBO..."