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(logging): Refactor logging of integration invoice. #2944

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
31 changes: 17 additions & 14 deletions app/services/integrations/aggregator/invoices/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@ def call
response = http_client.post_with_response(payload.body, headers)
body = JSON.parse(response.body)

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

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

return result unless external_id
return result unless result.external_id

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

IntegrationResource.create!(
integration:,
external_id: external_id,
external_id: result.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}"
Rails.logger.info "Integration resource created. external ID: #{result.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 @@ -74,14 +73,18 @@ def call_async
def process_hash_result(body)
external_id = body['succeededInvoices']&.first.try(:[], 'id')

return external_id if external_id

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

deliver_error_webhook(customer:, code:, message:)
deliver_error_webhook(customer:, code:, message:)
end
end

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