Skip to content

Commit

Permalink
Merge pull request #44 from ringcentral/subsidiaryId
Browse files Browse the repository at this point in the history
Add Subsidiary Id and made token url generic
  • Loading branch information
DaKingKong authored Jul 31, 2024
2 parents 5d47d86 + 299a0d3 commit 3dd7767
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
17 changes: 12 additions & 5 deletions src/adapters/netsuite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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: {
Expand All @@ -39,6 +42,7 @@ async function getUserInfo({ authHeader, additionalInfo, query }) {
platformAdditionalInfo: {
email: employeResponse.data.email,
name: name,
subsidiaryId,
},

},
Expand Down Expand Up @@ -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 }
}
,
{
Expand All @@ -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`,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/core/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3dd7767

Please sign in to comment.