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

Implement fuzzy search mechanism to find netsuite contact #49

Merged
merged 3 commits into from
Sep 18, 2024
Merged
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
122 changes: 56 additions & 66 deletions src/adapters/netsuite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,74 +93,63 @@ 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: result.phone ?? '',
homephone: result.homephone ?? '',
mobilephone: result.mobilephone ?? '',
officephone: result.officephone ?? '',
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: result.phone ?? '',
homephone: result.homephone ?? '',
mobilephone: result.mobilephone ?? '',
altphone: result.altphone ?? '',
additionalInfo: null,
type: 'custjob'
})
}
}
}
Expand All @@ -174,6 +163,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."
Expand Down
Loading