mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
Moved QBO fetch from customer model into service
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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,6 +18,23 @@ class CustomerPushService
|
|||||||
@customer = customer
|
@customer = customer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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.
|
# 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..."
|
||||||
Reference in New Issue
Block a user