Skip to content

Commit

Permalink
fix(netsuite): Fix possible duplicate integration resources creation (#…
Browse files Browse the repository at this point in the history
…2826)

## Context

When the Netsuite jobs that create invoices, credit note and paymetns
were enqueued at the same time, they sometimes created duplicate
integration resources.

## Description

This PR fixes this issue by checking if the resource already exists in
our DB.
  • Loading branch information
ivannovosad authored Nov 18, 2024
1 parent 434c860 commit c057779
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def call
return result unless integration
return result unless integration.sync_credit_notes
return result unless credit_note.finalized?
return result if payload.integration_credit_note

response = http_client.post_with_response(payload, headers)
response = http_client.post_with_response(payload.body, headers)
body = JSON.parse(response.body)

if body.is_a?(Hash)
Expand Down Expand Up @@ -73,7 +74,7 @@ def payload
Integrations::Aggregator::CreditNotes::Payloads::Factory.new_instance(
integration_customer:,
credit_note:
).body
)
end

def process_hash_result(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def body
]
end

def integration_credit_note
@integration_credit_note ||=
IntegrationResource.find_by(integration:, syncable: credit_note, resource_type: 'credit_note')
end

private

attr_reader :integration_customer, :credit_note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def call
return result unless integration
return result unless integration.sync_invoices
return result unless invoice.finalized?
return result if payload.integration_invoice

response = http_client.post_with_response(payload.body, headers)
body = JSON.parse(response.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def call
return result unless integration
return result unless integration.sync_invoices
return result unless invoice.finalized?
return result if payload.integration_invoice

Integrations::Hubspot::Invoices::DeployPropertiesService.call(integration:)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def call
return result unless integration
return result unless integration.sync_payments
return result unless invoice.finalized?
return result if payload.integration_payment

response = http_client.post_with_response(payload, headers)
response = http_client.post_with_response(payload.body, headers)
body = JSON.parse(response.body)

if body.is_a?(Hash)
Expand Down Expand Up @@ -72,7 +73,7 @@ def invoice
end

def payload
Integrations::Aggregator::Payments::Payloads::Factory.new_instance(integration:, payment:).body
Integrations::Aggregator::Payments::Payloads::Factory.new_instance(integration:, payment:)
end

def process_hash_result(body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def body
]
end

def integration_payment
@integration_payment ||=
IntegrationResource.find_by(integration:, syncable: payment, resource_type: 'payment')
end

private

attr_reader :payment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@
end

describe '#call' do
context 'when integration_credit_note exists' do
let(:integration_credit_note) do
create(:integration_resource, integration:, syncable: credit_note, resource_type: 'credit_note')
end

let(:response) { instance_double(Net::HTTPOK) }

before do
allow(lago_client).to receive(:post_with_response).with(params, headers).and_return(response)
integration_credit_note
end

it 'returns result without making an API call' do
expect(lago_client).not_to have_received(:post_with_response)
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.external_id).to be_nil
end
end
end

context 'when service call is successful' do
let(:response) { instance_double(Net::HTTPOK) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@
end

describe '#call' do
context 'when integration_invoice exists' do
let(:integration_invoice) { create(:integration_resource, integration:, syncable: invoice) }
let(:response) { instance_double(Net::HTTPOK) }

before do
allow(lago_client).to receive(:post_with_response).with(params, headers).and_return(response)
integration_invoice
end

it 'returns result without making an API call' do
expect(lago_client).not_to have_received(:post_with_response)
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.external_id).to be_nil
end
end
end

context 'when service call is successful' do
let(:response) { instance_double(Net::HTTPOK) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@
end

describe '#call' do
context 'when integration_payment exists' do
let(:integration_payment) do
create(:integration_resource, integration:, syncable: payment, resource_type: 'payment')
end

let(:response) { instance_double(Net::HTTPOK) }

before do
allow(lago_client).to receive(:post_with_response).with(params, headers).and_return(response)
integration_payment
end

it 'returns result without making an API call' do
expect(lago_client).not_to have_received(:post_with_response)
result = service_call

aggregate_failures do
expect(result).to be_success
expect(result.external_id).to be_nil
end
end
end

context 'when service call is successful' do
let(:response) { instance_double(Net::HTTPOK) }

Expand Down

0 comments on commit c057779

Please sign in to comment.