From bcde3e9c0507fb9b0d85a67fdea8634049d6c1db Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Tue, 15 Oct 2024 16:26:42 +0530 Subject: [PATCH 1/3] remove subsidiary dependency for non oneworld license users --- src/adapters/netsuite/index.js | 53 ++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index 3408a69e..f5b4737e 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -177,16 +177,23 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) async function createCallLog({ user, contactInfo, authHeader, callLog, note, additionalSubmission }) { try { const title = callLog.customSubject ?? `${callLog.direction} Call ${callLog.direction === 'Outbound' ? 'to' : 'from'} ${contactInfo.name}`; - const subsidiary = await axios.post( - `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, - { - q: `SELECT * FROM Subsidiary WHERE id = ${user?.platformAdditionalInfo?.subsidiaryId}` - }, - { - headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } - }); - const timeZone = getTimeZone(subsidiary.data.items[0]?.country, subsidiary.data.items[0]?.state); - const callStartTime = moment(moment(callLog.startTime).toISOString()).tz(timeZone); + const subsidiaryId = user?.platformAdditionalInfo?.subsidiaryId; + let callStartTime = moment(moment(callLog.startTime).toISOString()); + /** + * Users without a OneWorld license do not have access to subsidiaries. + */ + if (subsidiaryId !== undefined && subsidiaryId !== '') { + const subsidiary = await axios.post( + `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, + { + q: `SELECT * FROM Subsidiary WHERE id = ${user?.platformAdditionalInfo?.subsidiaryId}` + }, + { + headers: { 'Authorization': authHeader, 'Content-Type': 'application/json', 'Prefer': 'transient' } + }); + const timeZone = getTimeZone(subsidiary.data.items[0]?.country, subsidiary.data.items[0]?.state); + callStartTime = moment(moment(callLog.startTime).toISOString()).tz(timeZone); + } const callEndTime = moment(callStartTime).add(callLog.duration, 'seconds'); const formatedStartTime = callStartTime.format('YYYY-MM-DD HH:mm:ss'); const formatedEndTime = callEndTime.format('YYYY-MM-DD HH:mm:ss'); @@ -486,6 +493,7 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne try { const nameParts = splitName(newContactName); let contactId = 0; + const subsidiaryId = user.platformAdditionalInfo?.subsidiaryId; switch (newContactType) { case 'contact': let companyId = 0; @@ -503,12 +511,15 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne companyId = companyInfo.data.items[0].id; } else { + let companyPostBody = { + companyName: 'RingCentral_CRM_Extension_Placeholder_Company', + comments: "This company was created automatically by the RingCentral Unified CRM Extension. Feel free to edit, or associate this company's contacts to more appropriate records.", + }; + if (subsidiaryId !== undefined && subsidiaryId !== '') { + companyPostBody.subsidiary = { id: subsidiaryId }; + } const createCompany = await axios.post(`https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/record/v1/customer`, - { - companyName: 'RingCentral_CRM_Extension_Placeholder_Company', - comments: "This company was created automatically by the RingCentral Unified CRM Extension. Feel free to edit, or associate this company's contacts to more appropriate records.", - subsidiary: { id: user.platformAdditionalInfo?.subsidiaryId } - } + companyPostBody , { headers: { 'Authorization': authHeader, 'Content-Type': 'application/json' } @@ -520,9 +531,11 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne middleName: nameParts.middleName, lastName: nameParts.lastName, phone: phoneNumber || '', - company: { id: companyId }, - subsidiary: { id: user.platformAdditionalInfo?.subsidiaryId } + company: { id: companyId } }; + if (subsidiaryId !== undefined && subsidiaryId !== '') { + contactPayLoad.subsidiary = { id: subsidiaryId }; + } const createContactRes = await axios.post( `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/record/v1/contact`, contactPayLoad @@ -551,10 +564,12 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne middleName: nameParts.middleName, lastName: nameParts.lastName.length > 0 ? nameParts.lastName : nameParts.firstName, phone: phoneNumber || '', - isPerson: true, - subsidiary: { id: user.platformAdditionalInfo?.subsidiaryId } + isPerson: true }; + if (subsidiaryId !== undefined && subsidiaryId !== '') { + customerPayLoad.subsidiary = { id: subsidiaryId }; + } try { const createCustomerRes = await axios.post( `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/record/v1/customer`, From 5732a35d532619a1493eac54fa680d790488291e Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Thu, 17 Oct 2024 19:03:21 +0530 Subject: [PATCH 2/3] oneWOrld License check in netsuite --- src/adapters/netsuite/index.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index f5b4737e..602e31d4 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -33,6 +33,15 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { const timezoneOffset = employeResponse.data.time_zone_offset ?? null; const location = employeResponse.data.location ?? ''; const subsidiaryId = employeResponse.data.subsidiary?.id ?? ''; + let oneWorldLicenseResponse; + try { + const checkOneWorldLicenseUrl = `https://${query.hostname.split(".")[0]}.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=customscript_getoneworldlicense_scriptid&deploy=customdeploy_getoneworldlicense_deployid`; + oneWorldLicenseResponse = await axios.get(checkOneWorldLicenseUrl, { + headers: { 'Authorization': authHeader } + }); + } catch (e) { + console.log({ message: "Error in getting OneWorldLicense" }); + } return { successful: true, platformUserInfo: { @@ -44,6 +53,7 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { email: employeResponse.data.email, name: name, subsidiaryId, + oneWorldEnabled: oneWorldLicenseResponse?.data?.oneWorldEnabled, }, }, @@ -177,12 +187,12 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) async function createCallLog({ user, contactInfo, authHeader, callLog, note, additionalSubmission }) { try { const title = callLog.customSubject ?? `${callLog.direction} Call ${callLog.direction === 'Outbound' ? 'to' : 'from'} ${contactInfo.name}`; - const subsidiaryId = user?.platformAdditionalInfo?.subsidiaryId; + const oneWorldEnabled = user?.platformAdditionalInfo?.oneWorldEnabled; let callStartTime = moment(moment(callLog.startTime).toISOString()); /** * Users without a OneWorld license do not have access to subsidiaries. */ - if (subsidiaryId !== undefined && subsidiaryId !== '') { + if (oneWorldEnabled !== undefined && oneWorldEnabled === true) { const subsidiary = await axios.post( `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql`, { @@ -494,6 +504,7 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne const nameParts = splitName(newContactName); let contactId = 0; const subsidiaryId = user.platformAdditionalInfo?.subsidiaryId; + const oneWorldEnabled = user?.platformAdditionalInfo?.oneWorldEnabled; switch (newContactType) { case 'contact': let companyId = 0; @@ -515,7 +526,7 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne companyName: 'RingCentral_CRM_Extension_Placeholder_Company', comments: "This company was created automatically by the RingCentral Unified CRM Extension. Feel free to edit, or associate this company's contacts to more appropriate records.", }; - if (subsidiaryId !== undefined && subsidiaryId !== '') { + if (oneWorldEnabled !== undefined && oneWorldEnabled === true) { companyPostBody.subsidiary = { id: subsidiaryId }; } const createCompany = await axios.post(`https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/record/v1/customer`, @@ -533,7 +544,7 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne phone: phoneNumber || '', company: { id: companyId } }; - if (subsidiaryId !== undefined && subsidiaryId !== '') { + if (oneWorldEnabled !== undefined && oneWorldEnabled === true) { contactPayLoad.subsidiary = { id: subsidiaryId }; } const createContactRes = await axios.post( @@ -567,7 +578,7 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne isPerson: true }; - if (subsidiaryId !== undefined && subsidiaryId !== '') { + if (oneWorldEnabled !== undefined && oneWorldEnabled === true) { customerPayLoad.subsidiary = { id: subsidiaryId }; } try { From 7b771a6e66f9160930d1b6ba9ea3def226343d36 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Fri, 18 Oct 2024 11:59:30 +0530 Subject: [PATCH 3/3] Update logic --- src/adapters/netsuite/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index 602e31d4..8955bb3b 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -33,14 +33,18 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { const timezoneOffset = employeResponse.data.time_zone_offset ?? null; const location = employeResponse.data.location ?? ''; const subsidiaryId = employeResponse.data.subsidiary?.id ?? ''; - let oneWorldLicenseResponse; + let oneWorldEnabled; try { const checkOneWorldLicenseUrl = `https://${query.hostname.split(".")[0]}.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=customscript_getoneworldlicense_scriptid&deploy=customdeploy_getoneworldlicense_deployid`; - oneWorldLicenseResponse = await axios.get(checkOneWorldLicenseUrl, { + const oneWorldLicenseResponse = await axios.get(checkOneWorldLicenseUrl, { headers: { 'Authorization': authHeader } }); + oneWorldEnabled = oneWorldLicenseResponse?.data?.oneWorldEnabled; } catch (e) { console.log({ message: "Error in getting OneWorldLicense" }); + if (subsidiaryId !== undefined && subsidiaryId !== '') { + oneWorldEnabled = true; + } } return { successful: true, @@ -53,7 +57,7 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { email: employeResponse.data.email, name: name, subsidiaryId, - oneWorldEnabled: oneWorldLicenseResponse?.data?.oneWorldEnabled, + oneWorldEnabled: oneWorldEnabled, }, },