From bcde3e9c0507fb9b0d85a67fdea8634049d6c1db Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Tue, 15 Oct 2024 16:26:42 +0530 Subject: [PATCH] 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`,