From 4158954d96b5883fd79f9a58452b8ddfaec62724 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Wed, 31 Jul 2024 00:05:47 +0530 Subject: [PATCH 1/2] Genric hosturl and add subsidiary id in contact --- src/adapters/netsuite/index.js | 18 +++++++++++++----- src/core/auth.js | 2 +- src/core/contact.js | 4 ++-- src/core/log.js | 8 ++++---- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index ac0a6461..e526e268 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,9 @@ 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 ?? ''; + console.log({ message: "User Info", id, name, timezoneName, timezoneOffset, location }); return { successful: true, platformUserInfo: { @@ -39,6 +43,7 @@ async function getUserInfo({ authHeader, additionalInfo, query }) { platformAdditionalInfo: { email: employeResponse.data.email, name: name, + subsidiaryId, }, }, @@ -531,7 +536,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 +550,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 +582,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; From 299a0d3f76393799b374ff0b761c08a7073cdadf Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Wed, 31 Jul 2024 00:08:49 +0530 Subject: [PATCH 2/2] remove log --- src/adapters/netsuite/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/adapters/netsuite/index.js b/src/adapters/netsuite/index.js index e526e268..06b2383e 100644 --- a/src/adapters/netsuite/index.js +++ b/src/adapters/netsuite/index.js @@ -32,7 +32,6 @@ 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 ?? ''; - console.log({ message: "User Info", id, name, timezoneName, timezoneOffset, location }); return { successful: true, platformUserInfo: {