Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc(logs): Add logs to integration invoice create service #2940

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/integration_resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class IntegrationResource < ApplicationRecord
include PaperTrailTraceable

belongs_to :syncable, polymorphic: true
belongs_to :integration, class_name: 'Integrations::BaseIntegration'

Expand Down
32 changes: 18 additions & 14 deletions app/services/integrations/aggregator/invoices/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@ def call
response = http_client.post_with_response(payload.body, headers)
body = JSON.parse(response.body)

if body.is_a?(Hash)
external_id = if body.is_a?(Hash)
process_hash_result(body)
else
process_string_result(body)
body
end

return result unless result.external_id
Rails.logger.info "Response body: #{body}"
Rails.logger.info "External ID: #{external_id}"

return result unless external_id

Rails.logger.info "Creating integration resource with external ID: #{external_id}"

IntegrationResource.create!(
integration:,
external_id: result.external_id,
external_id: external_id,
syncable_id: invoice.id,
syncable_type: 'Invoice',
resource_type: :invoice
)

Rails.logger.info "Integration resource created. external ID: #{external_id}, invoice ID: #{invoice.id}"

result.external_id = external_id
result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)
Expand Down Expand Up @@ -66,18 +74,14 @@ def call_async
def process_hash_result(body)
external_id = body['succeededInvoices']&.first.try(:[], 'id')

if external_id
result.external_id = external_id
else
message = body['failedInvoices'].first['validation_errors'].map { |error| error['Message'] }.join(". ")
code = 'Validation error'
return external_id if external_id

deliver_error_webhook(customer:, code:, message:)
end
end
message = body['failedInvoices'].first['validation_errors'].map { |error| error['Message'] }.join(". ")
code = 'Validation error'

deliver_error_webhook(customer:, code:, message:)

def process_string_result(body)
result.external_id = body
nil
end
end
end
Expand Down
Loading