From 2ddaf797b2a3eedd5343075df1ba542ccc456ec5 Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Tue, 10 Sep 2024 11:16:38 +0100 Subject: [PATCH 1/2] feat(customer-type): Update netsuite payload with new fields --- .../aggregator/contacts/payloads/netsuite.rb | 21 ++- .../contacts/payloads/netsuite_spec.rb | 127 +++++++++++++++++- 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/app/services/integrations/aggregator/contacts/payloads/netsuite.rb b/app/services/integrations/aggregator/contacts/payloads/netsuite.rb index f932d8437d5..93cae465aa3 100644 --- a/app/services/integrations/aggregator/contacts/payloads/netsuite.rb +++ b/app/services/integrations/aggregator/contacts/payloads/netsuite.rb @@ -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 } @@ -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 } @@ -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 diff --git a/spec/services/integrations/aggregator/contacts/payloads/netsuite_spec.rb b/spec/services/integrations/aggregator/contacts/payloads/netsuite_spec.rb index 2c209a7bac2..0d075e26e2c 100644 --- a/spec/services/integrations/aggregator/contacts/payloads/netsuite_spec.rb +++ b/spec/services/integrations/aggregator/contacts/payloads/netsuite_spec.rb @@ -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 }, @@ -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 @@ -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:) } From 429a04e2276e45b7d9c11e362ef6d666feaa15d4 Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Tue, 10 Sep 2024 11:38:58 +0100 Subject: [PATCH 2/2] feat(customer-type): Fix netsuite integration contacts specs --- .../integrations/aggregator/contacts/create_service_spec.rb | 2 ++ .../integrations/aggregator/contacts/update_service_spec.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/spec/services/integrations/aggregator/contacts/create_service_spec.rb b/spec/services/integrations/aggregator/contacts/create_service_spec.rb index fd7f126b5fc..6795502ad23 100644 --- a/spec/services/integrations/aggregator/contacts/create_service_spec.rb +++ b/spec/services/integrations/aggregator/contacts/create_service_spec.rb @@ -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, @@ -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, diff --git a/spec/services/integrations/aggregator/contacts/update_service_spec.rb b/spec/services/integrations/aggregator/contacts/update_service_spec.rb index 720912df3a8..4c64901a5e8 100644 --- a/spec/services/integrations/aggregator/contacts/update_service_spec.rb +++ b/spec/services/integrations/aggregator/contacts/update_service_spec.rb @@ -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, @@ -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,