From 00b2be800d4adceabfdc6b712ff6684bdddc1a0c Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Wed, 18 Sep 2024 13:00:16 +0530 Subject: [PATCH 1/3] Implement fuzzy search mechanism to find netsuite contact --- src/adapters/netsuite/index.js | 116 ++++++++++++++------------------- 1 file changed, 50 insertions(+), 66 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index 34566026..8e9f9817 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -93,74 +93,57 @@ async function unAuthorize({ user }) { async function findContact({ user, authHeader, phoneNumber, overridingFormat }) { try { - const numberToQueryArray = []; - if (overridingFormat === '') { - numberToQueryArray.push(phoneNumber.replace(' ', '+')); - } - else { - const formats = overridingFormat.split(','); - for (var format of formats) { - const phoneNumberObj = parsePhoneNumber(phoneNumber.replace(' ', '+')); - if (phoneNumberObj.valid) { - const phoneNumberWithoutCountryCode = phoneNumberObj.number.significant; - let formattedNumber = format; - for (const numberBit of phoneNumberWithoutCountryCode) { - formattedNumber = formattedNumber.replace('*', numberBit); - } - numberToQueryArray.push(formattedNumber); - } - } - } + const phoneNumberObj = parsePhoneNumber(phoneNumber.replace(' ', '+')); + const phoneNumberWithoutCountryCode = phoneNumberObj.number.significant; const matchedContactInfo = []; - for (var numberToQuery of numberToQueryArray) { - if (numberToQuery !== 'undefined' && numberToQuery !== null && numberToQuery !== '') { - //For Contact search - const personInfo = await axios.post( - `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, - { - q: `SELECT * FROM contact WHERE phone = ${numberToQuery} OR homePhone = ${numberToQuery} OR mobilePhone = ${numberToQuery} OR officePhone = ${numberToQuery}` - }, - { - headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } - }); - if (personInfo.data.items.length > 0) { - for (var result of personInfo.data.items) { - let firstName = result.firstname ?? ''; - let middleName = result.middlename ?? ''; - let lastName = result.lastname ?? ''; - const contactName = (firstName + middleName + lastName).length > 0 ? `${firstName} ${middleName} ${lastName}` : result.entitytitle; - matchedContactInfo.push({ - id: result.id, - name: contactName, - phone: numberToQuery, - additionalInfo: null, - type: 'contact' - }) - } + if (phoneNumberWithoutCountryCode !== 'undefined' && phoneNumberWithoutCountryCode !== null && phoneNumberWithoutCountryCode !== '') { + const contactQuery = `SELECT * FROM contact WHERE REGEXP_REPLACE(phone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(homePhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(mobilePhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(officePhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%'`; + const customerQuery = `SELECT * FROM customer WHERE REGEXP_REPLACE(phone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(homePhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(mobilePhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%' OR REGEXP_REPLACE(altPhone, '[^0-9]', '') LIKE '%${phoneNumberWithoutCountryCode}%'`; + const personInfo = await axios.post( + `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, + { + q: contactQuery + }, + { + headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } + }); + if (personInfo.data.items.length > 0) { + for (var result of personInfo.data.items) { + let firstName = result.firstname ?? ''; + let middleName = result.middlename ?? ''; + let lastName = result.lastname ?? ''; + const contactName = (firstName + middleName + lastName).length > 0 ? `${firstName} ${middleName} ${lastName}` : result.entitytitle; + matchedContactInfo.push({ + id: result.id, + name: contactName, + phone: phoneNumberWithoutCountryCode, + additionalInfo: null, + type: 'contact' + }) } - //For Customer search - const customerInfo = await axios.post( - `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, - { - q: `SELECT * FROM customer WHERE phone = ${numberToQuery} OR homePhone = ${numberToQuery} OR mobilePhone = ${numberToQuery} OR altPhone = ${numberToQuery}` - }, - { - headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } - }); - if (customerInfo.data.items.length > 0) { - for (var result of customerInfo.data.items) { - let firstName = result.firstname ?? ''; - let middleName = result.middlename ?? ''; - let lastName = result.lastname ?? ''; - const customerName = (firstName + middleName + lastName).length > 0 ? `${firstName} ${middleName} ${lastName}` : result.entitytitle; - matchedContactInfo.push({ - id: result.id, - name: customerName, - phone: numberToQuery, - additionalInfo: null, - type: 'custjob' - }) - } + } + //For Customer search + const customerInfo = await axios.post( + `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, + { + q: customerQuery + }, + { + headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } + }); + if (customerInfo.data.items.length > 0) { + for (var result of customerInfo.data.items) { + let firstName = result.firstname ?? ''; + let middleName = result.middlename ?? ''; + let lastName = result.lastname ?? ''; + const customerName = (firstName + middleName + lastName).length > 0 ? `${firstName} ${middleName} ${lastName}` : result.entitytitle; + matchedContactInfo.push({ + id: result.id, + name: customerName, + phone: phoneNumberWithoutCountryCode, + additionalInfo: null, + type: 'custjob' + }) } } } @@ -174,6 +157,7 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) matchedContactInfo, }; } catch (error) { + console.log({ message: "Error in finding contact", error }); const isForbiddenError = isNetSuiteForbiddenError(error); const errorMessage = isForbiddenError ? "Permission violation: Make Sure You have 'Reports -> SuiteAnalytics Workbook, Lists -> Contacts & Lists -> Customer' permission to fetch details. Please contact your administrator." From ca444f8a33610856e19953ca4600761b0c440f47 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Wed, 18 Sep 2024 13:15:33 +0530 Subject: [PATCH 2/3] Used phonedNumber to return Netsuite API response instead of phoneNumberWithoutCountryCode --- src/adapters/netsuite/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index 8e9f9817..6993aa81 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -116,7 +116,7 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) matchedContactInfo.push({ id: result.id, name: contactName, - phone: phoneNumberWithoutCountryCode, + phone: phoneNumber, additionalInfo: null, type: 'contact' }) @@ -140,7 +140,7 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) matchedContactInfo.push({ id: result.id, name: customerName, - phone: phoneNumberWithoutCountryCode, + phone: phoneNumber, additionalInfo: null, type: 'custjob' }) From 33c65bac78d55208d2957cc0e39c5d797904a197 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Wed, 18 Sep 2024 13:37:34 +0530 Subject: [PATCH 3/3] Typo --- src/adapters/netsuite/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index 6993aa81..d078dfc7 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -116,7 +116,10 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) matchedContactInfo.push({ id: result.id, name: contactName, - phone: phoneNumber, + phone: result.phone ?? '', + homephone: result.homephone ?? '', + mobilephone: result.mobilephone ?? '', + officephone: result.officephone ?? '', additionalInfo: null, type: 'contact' }) @@ -140,7 +143,10 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) matchedContactInfo.push({ id: result.id, name: customerName, - phone: phoneNumber, + phone: result.phone ?? '', + homephone: result.homephone ?? '', + mobilephone: result.mobilephone ?? '', + altphone: result.altphone ?? '', additionalInfo: null, type: 'custjob' })