diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index ac0a6461..06b2383e 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -9,11 +9,12 @@ function getAuthType() { } -function getOauthInfo() { +function getOauthInfo({ hostname }) { + const tokenUrl = `https://${hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token`; return { clientId: process.env.NETSUITE_CRM_CLIENT_ID, clientSecret: process.env.NETSUITE_CRM_CLIENT_SECRET, - accessTokenUri: process.env.NETSUITE_CRM_TOKEN_URI, + accessTokenUri: tokenUrl, redirectUri: process.env.NETSUITE_CRM_REDIRECT_URI } } @@ -29,6 +30,8 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { const name = employeResponse.data.firstName + ' ' + employeResponse.data.lastName; const timezoneName = employeResponse.data.time_zone ?? ''; const timezoneOffset = employeResponse.data.time_zone_offset ?? null; + const location = employeResponse.data.location ?? ''; + const subsidiaryId = employeResponse.data.subsidiary?.id ?? ''; return { successful: true, platformUserInfo: { @@ -39,6 +42,7 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { platformAdditionalInfo: { email: employeResponse.data.email, name: name, + subsidiaryId, }, }, @@ -531,7 +535,8 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne 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." + 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 } } , { @@ -544,7 +549,8 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne middleName: nameParts.middleName, lastName: nameParts.lastName, phone: phoneNumber || '', - company: { id: companyId } + company: { id: companyId }, + subsidiary: { id: user.platformAdditionalInfo?.subsidiaryId } }; const createContactRes = await axios.post( `https://${user.hostname.split(".")[0]}.suitetalk.api.netsuite.com/services/rest/record/v1/contact`, @@ -575,7 +581,8 @@ async function createContact({ user, authHeader, phoneNumber, newContactName, ne middleName: nameParts.middleName, lastName: nameParts.lastName.length > 0 ? nameParts.lastName : nameParts.firstName, phone: phoneNumber || '', - isPerson: true + isPerson: true, + subsidiary: { id: user.platformAdditionalInfo?.subsidiaryId } }; try { diff --git a/src/core/auth.js b/src/core/auth.js index 55e0ec47..df0e3fb0 100644 --- a/src/core/auth.js +++ b/src/core/auth.js @@ -4,7 +4,7 @@ const Op = require('sequelize').Op; async function onOAuthCallback({ platform, hostname, tokenUrl, callbackUri, apiUrl, username, query }) { const platformModule = require(`../adapters/${platform}`); - const oauthInfo = platformModule.getOauthInfo({ tokenUrl }); + const oauthInfo = platformModule.getOauthInfo({ tokenUrl, hostname }); // Some platforms require different oauth queries, this won't affect normal OAuth process unless CRM module implements getOverridingOAuthOption() method let overridingOAuthOption = null; diff --git a/src/core/contact.js b/src/core/contact.js index 6cfd63b7..1bcdd9cc 100644 --- a/src/core/contact.js +++ b/src/core/contact.js @@ -24,7 +24,7 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat }) let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break; @@ -65,7 +65,7 @@ async function createContact({ platform, userId, phoneNumber, newContactName, ne let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break; diff --git a/src/core/log.js b/src/core/log.js index 2e2b3f7f..97dad7d0 100644 --- a/src/core/log.js +++ b/src/core/log.js @@ -41,7 +41,7 @@ async function createCallLog({ platform, userId, incomingData }) { let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break; @@ -107,7 +107,7 @@ async function getCallLog({ userId, sessionIds, platform, requireDetails }) { let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break; @@ -170,7 +170,7 @@ async function updateCallLog({ platform, userId, incomingData }) { let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break; @@ -230,7 +230,7 @@ async function createMessageLog({ platform, userId, incomingData }) { let authHeader = ''; switch (authType) { case 'oauth': - const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl })); + const oauthApp = oauth.getOAuthApp(platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })); user = await oauth.checkAndRefreshAccessToken(oauthApp, user); authHeader = `Bearer ${user.accessToken}`; break;