Skip to content

Commit

Permalink
feat(customer-type): Update netsuite payload with new fields (#2559)
Browse files Browse the repository at this point in the history
## Context
We currently only create Lago customers as **companies**, but there is a
need to support both **companies** and **individuals**. This change is
motivated by scenarios where customers may be a mix of B2B and B2C, and
where external integrations require handling both **Contacts** and
**Companies**.

To address this, we are introducing a new field, `customer_type`, to
distinguish whether a customer is a **company** or an **individual**.
Existing customers will remain unaffected with `customer_type` set to
the default `nil`.

## Description
In this PR we're adding the new fields: `lastname`, `firstname` and
`isperson` to both create and update netsuite payloads.
  • Loading branch information
ivannovosad authored Sep 10, 2024
1 parent 3d638b7 commit 9a0a1ef
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 10 deletions.
21 changes: 17 additions & 4 deletions app/services/integrations/aggregator/contacts/payloads/netsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def create_body
'type' => 'customer', # Fixed value
'isDynamic' => true, # Fixed value
'columns' => {
'companyname' => customer.name,
'isperson' => isperson,
'subsidiary' => subsidiary_id,
'custentity_lago_id' => customer.id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_url,
'email' => email,
'phone' => phone
},
}.merge(names),
'options' => {
'ignoreMandatoryFields' => false # Fixed value
}
Expand All @@ -29,13 +29,13 @@ def update_body
'type' => 'customer',
'recordId' => integration_customer.external_customer_id,
'values' => {
'companyname' => customer.name,
'isperson' => isperson,
'subsidiary' => integration_customer.subsidiary_id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_url,
'email' => email,
'phone' => phone
},
}.merge(names),
'options' => {
'isDynamic' => false
}
Expand All @@ -44,6 +44,19 @@ def update_body

private

def names
# customer_type might be nil -> in that case it's a company so we better check for an individual type here
return {'companyname' => customer.name} unless customer.customer_type_individual?

names_hash = {'firstname' => customer.firstname, 'lastname' => customer.lastname}

customer.name.present? ? names_hash.merge('companyname' => customer.name) : names_hash
end

def isperson
customer.customer_type_individual? ? 'T' : 'F'
end

def include_lines?
!integration.legacy_script && !customer.empty_billing_and_shipping_address?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'isDynamic' => true,
'columns' => {
'companyname' => customer.name,
'isperson' => 'F',
'subsidiary' => subsidiary_id,
'custentity_lago_id' => customer.id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
Expand Down Expand Up @@ -181,6 +182,7 @@
'isDynamic' => true,
'columns' => {
'companyname' => customer.name,
'isperson' => 'F',
'subsidiary' => subsidiary_id,
'custentity_lago_id' => customer.id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
'columns' => {
'companyname' => customer.name,
'subsidiary' => subsidiary_id,
'isperson' => 'F',
'custentity_lago_id' => customer.id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_link,
'email' => customer.email.to_s.split(',').first&.strip,
'phone' => customer.phone.to_s.split(',').first&.strip
},
}.merge(
customer.customer_type_individual? ? {'firstname' => customer.firstname, 'lastname' => customer.lastname} : {}
),
'options' => {
'ignoreMandatoryFields' => false
},
Expand Down Expand Up @@ -305,26 +308,53 @@
describe "#update_body" do
subject(:update_body_call) { payload.update_body }

let(:customer) { create(:customer, customer_type:) }
let(:isperson) { payload.__send__(:isperson) }

let(:payload_body) do
{
'type' => 'customer',
'recordId' => integration_customer.external_customer_id,
'values' => {
'companyname' => customer.name,
'isperson' => isperson,
'subsidiary' => integration_customer.subsidiary_id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_link,
'email' => customer.email.to_s.split(',').first&.strip,
'phone' => customer.phone.to_s.split(',').first&.strip
},
}.merge(names),
'options' => {
'isDynamic' => false
}
}
end

it "returns the payload body" do
expect(subject).to eq payload_body
context 'when customer is an individual' do
let(:customer_type) { :individual }

let(:names) do
{
'companyname' => customer.name,
'firstname' => customer.firstname,
'lastname' => customer.lastname
}
end

it "returns the payload body" do
expect(subject).to eq payload_body
end
end

context 'when customer is not an individual' do
let(:customer_type) { [nil, :company].sample }

let(:names) do
{'companyname' => customer.name}
end

it "returns the payload body" do
expect(subject).to eq payload_body
end
end
end

Expand Down Expand Up @@ -368,8 +398,93 @@
end
end

describe '#names' do
subject(:names_call) { payload.__send__(:names) }

let(:customer) { create(:customer, customer_type:, name:) }

context 'when customer type is nil' do
let(:customer_type) { nil }
let(:name) { Faker::TvShows::SiliconValley.character }
let(:names) { {'companyname' => customer.name} }

it 'returns the result hash' do
expect(subject).to eq(names)
end
end

context 'when customer type is company' do
let(:customer_type) { :company }
let(:name) { Faker::TvShows::SiliconValley.character }

let(:names) { {'companyname' => customer.name} }

it 'returns the result hash' do
expect(subject).to eq(names)
end
end

context 'when customer type is individual' do
let(:customer_type) { :individual }

context 'when name is present' do
let(:name) { Faker::TvShows::SiliconValley.character }

let(:names) do
{'companyname' => customer.name, 'firstname' => customer.firstname, 'lastname' => customer.lastname}
end

it 'returns the result hash' do
expect(subject).to eq(names)
end
end

context 'when name is not present' do
let(:name) { nil }

let(:names) do
{'firstname' => customer.firstname, 'lastname' => customer.lastname}
end

it 'returns the result hash' do
expect(subject).to eq(names)
end
end
end
end

describe '#isperson' do
subject(:isperson_call) { payload.__send__(:isperson) }

let(:customer) { create(:customer, customer_type:) }

context 'when customer type is nil' do
let(:customer_type) { nil }

it 'returns F' do
expect(subject).to eq('F')
end
end

context 'when customer type is company' do
let(:customer_type) { :company }

it 'returns F' do
expect(subject).to eq('F')
end
end

context 'when customer type is individual' do
let(:customer_type) { :individual }

it 'returns T' do
expect(subject).to eq('T')
end
end
end

describe '#phone' do
subject(:phone_call) { payload.__send__(:phone) }
subject { payload.__send__(:phone) }

let(:customer) { create(:customer, phone:) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'recordId' => integration_customer.external_customer_id,
'values' => {
'companyname' => customer.name,
'isperson' => 'F',
'subsidiary' => integration_customer.subsidiary_id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_link,
Expand Down Expand Up @@ -131,6 +132,7 @@
'recordId' => integration_customer.external_customer_id,
'values' => {
'companyname' => customer.name,
'isperson' => 'F',
'subsidiary' => integration_customer.subsidiary_id,
'custentity_lago_sf_id' => customer.external_salesforce_id,
'custentity_lago_customer_link' => customer_link,
Expand Down

0 comments on commit 9a0a1ef

Please sign in to comment.