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

fix(netsuite): Fix possible duplicate integration resources creation #2826

Merged
merged 1 commit into from
Nov 18, 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
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