Skip to content

Commit

Permalink
feat (netsuite-integration): Add BE support for netsuite TBA (#2238)
Browse files Browse the repository at this point in the history
## Context

Currently Lago is integrated with netsuite accounting tool.

## Description

This PR adds BE support required fields related to the token based auth.

Also, Lago's integration aggregator changed definition for
'Provider-Config-Key' property so that is also tackled

---------

Co-authored-by: Alexandre Monjol <[email protected]>
  • Loading branch information
lovrocolic and ansmonjol authored Jul 2, 2024
1 parent 3154434 commit 9b3643f
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 24 deletions.
8 changes: 8 additions & 0 deletions app/graphql/types/integrations/netsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ class Netsuite < Types::BaseObject
field :sync_invoices, Boolean
field :sync_payments, Boolean
field :sync_sales_orders, Boolean
field :token_id, String, null: true
field :token_secret, String, null: true

# NOTE: Client secret is a sensitive information. It should not be sent back to the
# front end application. Instead we send an obfuscated value
def client_secret
"#{"•" * 8}#{object.client_secret[-3..]}"
end

def token_secret
return nil unless object.token_secret

"#{"•" * 8}#{object.token_secret[-3..]}"
end

def has_mappings_configured
object.integration_collection_mappings
.where(type: 'IntegrationCollectionMappings::NetsuiteCollectionMapping')
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/integrations/netsuite/create_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CreateInput < Types::BaseInputObject
argument :client_secret, String, required: true
argument :connection_id, String, required: true
argument :script_endpoint_url, String, required: true
argument :token_id, String, required: true
argument :token_secret, String, required: true

argument :sync_credit_notes, Boolean, required: false
argument :sync_invoices, Boolean, required: false
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/integrations/netsuite/update_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class UpdateInput < Types::BaseInputObject
argument :client_secret, String, required: false
argument :connection_id, String, required: false
argument :script_endpoint_url, String, required: false
argument :token_id, String, required: false
argument :token_secret, String, required: false

argument :sync_credit_notes, Boolean, required: false
argument :sync_invoices, Boolean, required: false
Expand Down
5 changes: 3 additions & 2 deletions app/models/integrations/netsuite_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class NetsuiteIntegration < BaseIntegration
:sync_invoices,
:sync_payments,
:sync_sales_orders,
:script_endpoint_url
secrets_accessors :connection_id, :client_secret
:script_endpoint_url,
:token_id
secrets_accessors :connection_id, :client_secret, :token_secret

def account_id=(value)
push_to_settings(key: 'account_id', value: value&.downcase&.strip&.split(' ')&.join('-'))
Expand Down
2 changes: 1 addition & 1 deletion app/services/integrations/aggregator/accounts_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
9 changes: 9 additions & 0 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ def provider
end
end

def provider_key
case integration.type
when 'Integrations::NetsuiteIntegration'
'netsuite-tba'
when 'Integrations::XeroIntegration'
'xero'
end
end

def http_client
LagoHttpClient::Client.new(endpoint_url)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/integrations/aggregator/items_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call

def headers
{
'Provider-Config-Key' => 'netsuite',
'Provider-Config-Key' => 'netsuite-tba',
'Authorization' => "Bearer #{secret_key}"
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/integrations/aggregator/sync_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def action_path

def call
payload = {
provider_config_key: provider,
provider_config_key: provider_key,
syncs: sync_list
}

Expand Down
2 changes: 1 addition & 1 deletion app/services/integrations/aggregator/tax_items_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def headers
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{secret_key}",
'Provider-Config-Key' => provider
'Provider-Config-Key' => provider_key
}
end

Expand Down
2 changes: 2 additions & 0 deletions app/services/integrations/netsuite/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def call(**args)
client_id: args[:client_id],
client_secret: args[:client_secret],
account_id: args[:account_id],
token_id: args[:token_id],
token_secret: args[:token_secret],
connection_id: args[:connection_id],
script_endpoint_url: args[:script_endpoint_url],
sync_credit_notes: ActiveModel::Type::Boolean.new.cast(args[:sync_credit_notes]),
Expand Down
6 changes: 6 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion spec/graphql/mutations/integrations/netsuite/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
syncInvoices,
syncCreditNotes,
syncPayments,
scriptEndpointUrl
scriptEndpointUrl,
tokenId,
tokenSecret
}
}
GQL
Expand Down Expand Up @@ -50,6 +52,8 @@
accountId: '012',
clientId: '123',
clientSecret: '456',
tokenId: 'xyz',
tokenSecret: 'zyx',
connectionId: 'this-is-random-uuid'
}
}
Expand All @@ -61,6 +65,8 @@
expect(result_data['id']).to be_present
expect(result_data['code']).to eq(code)
expect(result_data['name']).to eq(name)
expect(result_data['tokenId']).to eq('xyz')
expect(result_data['tokenSecret']).to eq('••••••••…zyx')
expect(result_data['scriptEndpointUrl']).to eq(script_endpoint_url)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}",
'Provider-Config-Key' => 'netsuite'
'Provider-Config-Key' => 'netsuite-tba'
}
end

Expand Down
2 changes: 2 additions & 0 deletions spec/graphql/types/integrations/netsuite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
it { is_expected.to have_field(:has_mappings_configured).of_type('Boolean') }
it { is_expected.to have_field(:name).of_type('String!') }
it { is_expected.to have_field(:script_endpoint_url).of_type('String!') }
it { is_expected.to have_field(:token_id).of_type('String') }
it { is_expected.to have_field(:token_secret).of_type('String') }

it { is_expected.to have_field(:sync_credit_notes).of_type('Boolean') }
it { is_expected.to have_field(:sync_invoices).of_type('Boolean') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}",
'Provider-Config-Key' => 'netsuite'
'Provider-Config-Key' => 'netsuite-tba'
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}",
'Provider-Config-Key' => integration_type
'Provider-Config-Key' => integration_type_key
}
end

Expand All @@ -35,6 +35,7 @@
context 'when response is a string' do
let(:integration) { create(:netsuite_integration, organization:) }
let(:integration_type) { 'netsuite' }
let(:integration_type_key) { 'netsuite-tba' }

let(:params) do
{
Expand Down Expand Up @@ -81,6 +82,7 @@
context 'when response is a hash' do
let(:integration) { create(:xero_integration, organization:) }
let(:integration_type) { 'xero' }
let(:integration_type_key) { 'xero' }

let(:params) do
[
Expand Down Expand Up @@ -145,6 +147,7 @@
context 'when service call is not successful' do
let(:integration) { create(:netsuite_integration, organization:) }
let(:integration_type) { 'netsuite' }
let(:integration_type_key) { 'netsuite-tba' }

let(:params) do
{
Expand Down
Loading

0 comments on commit 9b3643f

Please sign in to comment.