diff --git a/src/adapters/bullhorn/index.js b/src/adapters/bullhorn/index.js index 18e89f34..f07d126d 100644 --- a/src/adapters/bullhorn/index.js +++ b/src/adapters/bullhorn/index.js @@ -32,6 +32,7 @@ async function getUserInfo({ authHeader, tokenUrl, apiUrl, username }) { bhRestToken } return { + successful: true, platformUserInfo: { id, name, @@ -49,6 +50,7 @@ async function getUserInfo({ authHeader, tokenUrl, apiUrl, username }) { } catch (e) { return { + successful: false, returnMessage: { messageType: 'warning', message: 'Failed to get user info.', diff --git a/src/adapters/clio/index.js b/src/adapters/clio/index.js index a074eb8a..6bb50f76 100644 --- a/src/adapters/clio/index.js +++ b/src/adapters/clio/index.js @@ -17,29 +17,42 @@ function getOauthInfo() { } async function getUserInfo({ authHeader }) { - const userInfoResponse = await axios.get('https://app.clio.com/api/v4/users/who_am_i.json?fields=id,name,time_zone', { - headers: { - 'Authorization': authHeader - } - }); - const id = userInfoResponse.data.data.id.toString(); - const name = userInfoResponse.data.data.name; - const timezoneName = userInfoResponse.data.data.time_zone; - const timezoneOffset = 0; - return { - platformUserInfo: { - id, - name, - timezoneName, - timezoneOffset, - platformAdditionalInfo: {} - }, - returnMessage: { - messageType: 'success', - message: 'Successfully connceted to Clio.', - ttl: 3000 + try { + const userInfoResponse = await axios.get('https://app.clio.com/api/v4/users/who_am_i.json?fields=id,name,time_zone', { + headers: { + 'Authorization': authHeader + } + }); + const id = userInfoResponse.data.data.id.toString(); + const name = userInfoResponse.data.data.name; + const timezoneName = userInfoResponse.data.data.time_zone; + const timezoneOffset = 0; + return { + successful: true, + platformUserInfo: { + id, + name, + timezoneName, + timezoneOffset, + platformAdditionalInfo: {} + }, + returnMessage: { + messageType: 'success', + message: 'Successfully connceted to Clio.', + ttl: 3000 + } + }; + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'Failed to get user info.', + ttl: 3000 + } } - }; + } } async function unAuthorize({ user }) { const revokeUrl = 'https://app.clio.com/oauth/deauthorize'; diff --git a/src/adapters/insightly/index.js b/src/adapters/insightly/index.js index bb6e510e..2f99e666 100644 --- a/src/adapters/insightly/index.js +++ b/src/adapters/insightly/index.js @@ -11,32 +11,45 @@ function getBasicAuth({ apiKey }) { } async function getUserInfo({ authHeader, additionalInfo }) { - additionalInfo.apiUrl = additionalInfo.apiUrl.split('/v')[0]; - const userInfoResponse = await axios.get(`${additionalInfo.apiUrl}/${process.env.INSIGHTLY_API_VERSION}/users/me`, { - headers: { - 'Authorization': authHeader - } - });; - // Insightly timezone = server location + non-standard tz area id (eg.'Central Standard Time') - // We use UTC here for now - const id = userInfoResponse.data.USER_ID.toString(); - const name = `${userInfoResponse.data.FIRST_NAME} ${userInfoResponse.data.LAST_NAME}`; - const timezoneOffset = null; - const timezoneName = userInfoResponse.data.TIMEZONE_ID; - return { - platformUserInfo: { - id, - name, - timezoneName, - timezoneOffset, - platformAdditionalInfo: additionalInfo - }, - returnMessage: { - messageType: 'success', - message: 'Successfully connceted to Insightly.', - ttl: 3000 + try { + additionalInfo.apiUrl = additionalInfo.apiUrl.split('/v')[0]; + const userInfoResponse = await axios.get(`${additionalInfo.apiUrl}/${process.env.INSIGHTLY_API_VERSION}/users/me`, { + headers: { + 'Authorization': authHeader + } + });; + // Insightly timezone = server location + non-standard tz area id (eg.'Central Standard Time') + // We use UTC here for now + const id = userInfoResponse.data.USER_ID.toString(); + const name = `${userInfoResponse.data.FIRST_NAME} ${userInfoResponse.data.LAST_NAME}`; + const timezoneOffset = null; + const timezoneName = userInfoResponse.data.TIMEZONE_ID; + return { + successful: true, + platformUserInfo: { + id, + name, + timezoneName, + timezoneOffset, + platformAdditionalInfo: additionalInfo + }, + returnMessage: { + messageType: 'success', + message: 'Successfully connceted to Insightly.', + ttl: 3000 + } + }; + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'Failed to get user info.', + ttl: 3000 + } } - }; + } } async function unAuthorize({ user }) { diff --git a/src/adapters/pipedrive/index.js b/src/adapters/pipedrive/index.js index 4ef32f0d..1146f302 100644 --- a/src/adapters/pipedrive/index.js +++ b/src/adapters/pipedrive/index.js @@ -17,34 +17,47 @@ function getOauthInfo() { } async function getUserInfo({ authHeader, hostname }) { - const userInfoResponse = await axios.get('https://api.pipedrive.com/v1/users/me', { - headers: { - 'Authorization': authHeader - } - }); - const id = userInfoResponse.data.data.id.toString(); - const name = userInfoResponse.data.data.name; - const timezoneName = userInfoResponse.data.data.timezone_name; - const timezoneOffset = userInfoResponse.data.data.timezone_offset; - return { - platformUserInfo: { - id, - name, - timezoneName, - timezoneOffset, - platformAdditionalInfo: { - companyId: userInfoResponse.data.data.company_id, - companyName: userInfoResponse.data.data.company_name, - companyDomain: userInfoResponse.data.data.company_domain, + try { + const userInfoResponse = await axios.get('https://api.pipedrive.com/v1/users/me', { + headers: { + 'Authorization': authHeader + } + }); + const id = userInfoResponse.data.data.id.toString(); + const name = userInfoResponse.data.data.name; + const timezoneName = userInfoResponse.data.data.timezone_name; + const timezoneOffset = userInfoResponse.data.data.timezone_offset; + return { + successful: true, + platformUserInfo: { + id, + name, + timezoneName, + timezoneOffset, + platformAdditionalInfo: { + companyId: userInfoResponse.data.data.company_id, + companyName: userInfoResponse.data.data.company_name, + companyDomain: userInfoResponse.data.data.company_domain, + }, + overridingHostname: hostname == 'temp' ? `${userInfoResponse.data.data.company_domain}.pipedrive.com` : null }, - overridingHostname: hostname == 'temp' ? `${userInfoResponse.data.data.company_domain}.pipedrive.com` : null - }, - returnMessage: { - messageType: 'success', - message: 'Successfully connceted to Pipedrive.', - ttl: 3000 + returnMessage: { + messageType: 'success', + message: 'Successfully connceted to Pipedrive.', + ttl: 3000 + } + }; + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'Failed to get user info.', + ttl: 3000 + } } - }; + } } async function unAuthorize({ user }) { diff --git a/src/adapters/redtail/index.js b/src/adapters/redtail/index.js index 30518447..edae2dc8 100644 --- a/src/adapters/redtail/index.js +++ b/src/adapters/redtail/index.js @@ -15,32 +15,45 @@ function getAuthHeader({ userKey }) { } async function getUserInfo({ authHeader, additionalInfo }) { - const overrideAPIKey = `${process.env.REDTAIL_API_KEY}:${additionalInfo.username}:${additionalInfo.password}`; - const overrideAuthHeader = `Basic ${getBasicAuth({ apiKey: overrideAPIKey })}`; - const authResponse = await axios.get(`${process.env.REDTAIL_API_SERVER}/authentication`, { - headers: { - 'Authorization': overrideAuthHeader + try { + const overrideAPIKey = `${process.env.REDTAIL_API_KEY}:${additionalInfo.username}:${additionalInfo.password}`; + const overrideAuthHeader = `Basic ${getBasicAuth({ apiKey: overrideAPIKey })}`; + const authResponse = await axios.get(`${process.env.REDTAIL_API_SERVER}/authentication`, { + headers: { + 'Authorization': overrideAuthHeader + } + }); + additionalInfo['userResponse'] = authResponse.data.authenticated_user; + delete additionalInfo.password; + const id = additionalInfo.username; + const name = additionalInfo.username; + const timezoneName = ''; + const timezoneOffset = null; + return { + successful: false, + platformUserInfo: { + id, + name, + timezoneName, + timezoneOffset, + overridingApiKey: additionalInfo.userResponse.user_key, + platformAdditionalInfo: additionalInfo + }, + returnMessage: { + messageType: 'success', + message: 'Successfully connceted to Redtail.', + ttl: 3000 + } } - }); - additionalInfo['userResponse'] = authResponse.data.authenticated_user; - delete additionalInfo.password; - const id = additionalInfo.username; - const name = additionalInfo.username; - const timezoneName = ''; - const timezoneOffset = null; - return { - platformUserInfo: { - id, - name, - timezoneName, - timezoneOffset, - overridingApiKey: additionalInfo.userResponse.user_key, - platformAdditionalInfo: additionalInfo - }, - returnMessage: { - messageType: 'success', - message: 'Successfully connceted to Redtail.', - ttl: 3000 + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'Failed to get user info.', + ttl: 3000 + } } } } diff --git a/src/adapters/testCRM/index.js b/src/adapters/testCRM/index.js index de9bcf4f..e51a14b1 100644 --- a/src/adapters/testCRM/index.js +++ b/src/adapters/testCRM/index.js @@ -61,41 +61,52 @@ async function getUserInfo({ authHeader, additionalInfo }) { // ------------------------------------------------------ // ---TODO.1: Implement API call to retrieve user info--- // ------------------------------------------------------ - - // API call to get logged in user info - // const userInfoResponse = await axios.get('https://api.crm.com/user/me', { - // headers: { - // 'Authorization': authHeader - // } - // }); - const mockUserInfoResponse = { - data: { - id: 'testUserId', - name: 'Test User', - time_zone: 'America/Los_Angeles', - time_zone_offset: 0 + try { + // API call to get logged in user info + // const userInfoResponse = await axios.get('https://api.crm.com/user/me', { + // headers: { + // 'Authorization': authHeader + // } + // }); + const mockUserInfoResponse = { + data: { + id: 'testUserId', + name: 'Test User', + time_zone: 'America/Los_Angeles', + time_zone_offset: 0 + } } - } - const id = mockUserInfoResponse.data.id; - const name = mockUserInfoResponse.data.name; - const timezoneName = mockUserInfoResponse.data.time_zone ?? ''; // Optional. Whether or not you want to log with regards to the user's timezone - const timezoneOffset = mockUserInfoResponse.data.time_zone_offset ?? null; // Optional. Whether or not you want to log with regards to the user's timezone. It will need to be converted to a format that CRM platform uses, - return { - platformUserInfo: { - id, - name, - timezoneName, - timezoneOffset, - platformAdditionalInfo: {} // this should save whatever extra info you want to save against the user - }, - returnMessage: { - messageType: 'success', - message: 'Successfully connected to TestCRM.', - ttl: 3000 + const id = mockUserInfoResponse.data.id; + const name = mockUserInfoResponse.data.name; + const timezoneName = mockUserInfoResponse.data.time_zone ?? ''; // Optional. Whether or not you want to log with regards to the user's timezone + const timezoneOffset = mockUserInfoResponse.data.time_zone_offset ?? null; // Optional. Whether or not you want to log with regards to the user's timezone. It will need to be converted to a format that CRM platform uses, + return { + successful: true, + platformUserInfo: { + id, + name, + timezoneName, + timezoneOffset, + platformAdditionalInfo: {} // this should save whatever extra info you want to save against the user + }, + returnMessage: { + messageType: 'success', + message: 'Successfully connected to TestCRM.', + ttl: 3000 + } + }; + } + catch (e) { + return { + successful: false, + returnMessage: { + messageType: 'warning', + message: 'Failed to get user info.', + ttl: 3000 + } } - }; - + } //--------------------------------------------------------------------------------------------------- //---CHECK.1: Open db.sqlite (might need to install certain viewer) to check if user info is saved--- //--------------------------------------------------------------------------------------------------- diff --git a/src/core/auth.js b/src/core/auth.js index 813bf32c..eda3d46e 100644 --- a/src/core/auth.js +++ b/src/core/auth.js @@ -14,22 +14,27 @@ async function onOAuthCallback({ platform, hostname, tokenUrl, callbackUri, apiU const oauthApp = oauth.getOAuthApp(oauthInfo); const { accessToken, refreshToken, expires } = await oauthApp.code.getToken(callbackUri, overridingOAuthOption); const authHeader = `Bearer ${accessToken}`; - const { platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader, tokenUrl, apiUrl, hostname, username, callbackUri, query }); - const userInfo = await saveUserInfo({ - platformUserInfo, - platform, - tokenUrl, - apiUrl, - username, - hostname: !!platformUserInfo?.overridingHostname ? platformUserInfo.overridingHostname : hostname, - accessToken, - refreshToken, - tokenExpiry: expires - }); - return { - userInfo, + const { successful, platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader, tokenUrl, apiUrl, hostname, username, callbackUri, query }); + if (successful) { + const userInfo = await saveUserInfo({ + platformUserInfo, + platform, + tokenUrl, + apiUrl, + username, + hostname: !!platformUserInfo?.overridingHostname ? platformUserInfo.overridingHostname : hostname, + accessToken, + refreshToken, + tokenExpiry: expires + }); + return { + userInfo, + returnMessage + }; + } + else { returnMessage - }; + } } async function onApiKeyLogin({ platform, hostname, apiKey, additionalInfo }) { diff --git a/src/index.js b/src/index.js index f392eab7..a73ae0c3 100644 --- a/src/index.js +++ b/src/index.js @@ -53,7 +53,7 @@ app.get('/crmManifest', (req, res) => { } const crmManifest = require(`./adapters/${req.query.platformName}/manifest.json`); if (!!crmManifest) { - if(!!!crmManifest.author?.name){ + if (!!!crmManifest.author?.name) { throw 'author name is required'; } res.json(crmManifest); @@ -163,12 +163,19 @@ app.get('/oauth-callback', async function (req, res) { id: userInfo.id.toString(), platform: platform }); - res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); - success = true; + if (!!userInfo) { + res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); + success = true; + } + else { + res.status(500).send({ returnMessage }); + success = false; + } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -212,6 +219,7 @@ app.post('/apiKeyLogin', async function (req, res) { catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -254,11 +262,13 @@ app.post('/unAuthorize', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -301,11 +311,13 @@ app.get('/contact', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -337,11 +349,13 @@ app.post('/contact', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -373,11 +387,13 @@ app.get('/callLog', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -408,11 +424,13 @@ app.post('/callLog', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -444,11 +462,13 @@ app.patch('/callLog', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -480,11 +500,13 @@ app.post('/messageLog', async function (req, res) { } else { res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; } } catch (e) { console.log(e); res.status(400).send(e); + success = false; } const requestEndTime = new Date().getTime(); analytics.track({ @@ -518,143 +540,147 @@ function getAnalyticsVariablesInReqHeaders({ headers }) { // Legacy endpoints for backward compatibility -// app.get('/oauth-callbackV2', async function (req, res) { -// const requestStartTime = new Date().getTime(); -// let platformName = null; -// let success = false; -// const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) -// try { -// if (!!!req.query?.callbackUri || req.query.callbackUri === 'undefined') { -// throw 'Missing callbackUri'; -// } -// platformName = platform = req.query.state ? -// req.query.state.split('platform=')[1] : -// decodeURIComponent(req.originalUrl).split('state=')[1].split('&')[0].split('platform=')[1]; -// const hostname = req.query.hostname; -// const tokenUrl = req.query.tokenUrl; -// if (!platform) { -// throw 'Missing platform name'; -// } -// const hasAuthCodeInCallbackUri = req.query.callbackUri.includes('code='); -// if (!hasAuthCodeInCallbackUri) { -// req.query.callbackUri = `${req.query.callbackUri}&code=${req.query.code}`; -// } -// const { userInfo, returnMessage } = await authCore.onOAuthCallback({ -// platform, -// hostname, -// tokenUrl, -// callbackUri: req.query.callbackUri, -// apiUrl: req.query.apiUrl, -// username: req.query.username, -// query: req.query -// }); -// const jwtToken = jwt.generateJwt({ -// id: userInfo.id.toString(), -// platform: platform -// }); -// res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); -// success = true; -// } -// catch (e) { -// console.log(e); -// res.status(400).send(e); -// } -// const requestEndTime = new Date().getTime(); -// analytics.track({ -// eventName: 'OAuth Callback', -// interfaceName: 'onOAuthCallback', -// adapterName: platformName, -// accountId: hashedAccountId, -// extensionId: hashedExtensionId, -// success, -// requestDuration: (requestEndTime - requestStartTime) / 1000, -// userAgent, -// ip, -// author, -// isLegacy: true -// }); -// }) +app.get('/oauth-callbackV2', async function (req, res) { + const requestStartTime = new Date().getTime(); + let platformName = null; + let success = false; + const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) + try { + if (!!!req.query?.callbackUri || req.query.callbackUri === 'undefined') { + throw 'Missing callbackUri'; + } + platformName = platform = req.query.state ? + req.query.state.split('platform=')[1] : + decodeURIComponent(req.originalUrl).split('state=')[1].split('&')[0].split('platform=')[1]; + const hostname = req.query.hostname; + const tokenUrl = req.query.tokenUrl; + if (!platform) { + throw 'Missing platform name'; + } + const hasAuthCodeInCallbackUri = req.query.callbackUri.includes('code='); + if (!hasAuthCodeInCallbackUri) { + req.query.callbackUri = `${req.query.callbackUri}&code=${req.query.code}`; + } + const { userInfo, returnMessage } = await authCore.onOAuthCallback({ + platform, + hostname, + tokenUrl, + callbackUri: req.query.callbackUri, + apiUrl: req.query.apiUrl, + username: req.query.username, + query: req.query + }); + const jwtToken = jwt.generateJwt({ + id: userInfo.id.toString(), + platform: platform + }); + res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); + success = true; + } + catch (e) { + console.log(e); + res.status(400).send(e); + success = false; + } + const requestEndTime = new Date().getTime(); + analytics.track({ + eventName: 'OAuth Callback', + interfaceName: 'onOAuthCallback', + adapterName: platformName, + accountId: hashedAccountId, + extensionId: hashedExtensionId, + success, + requestDuration: (requestEndTime - requestStartTime) / 1000, + userAgent, + ip, + author, + isLegacy: true + }); +}) -// app.post('/apiKeyLoginV2', async function (req, res) { -// const requestStartTime = new Date().getTime(); -// let platformName = null; -// let success = false; -// const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) -// try { -// const platform = req.body.platform; -// platformName = platform; -// const apiKey = req.body.apiKey; -// const hostname = req.body.hostname; -// const additionalInfo = req.body.additionalInfo; -// if (!platform) { -// throw 'Missing platform name'; -// } -// if (!apiKey) { -// throw 'Missing api key'; -// } -// const { userInfo, returnMessage } = await authCore.onApiKeyLogin({ platform, hostname, apiKey, additionalInfo }); -// const jwtToken = jwt.generateJwt({ -// id: userInfo.id.toString(), -// platform: platform -// }); -// res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); -// success = true; -// } -// catch (e) { -// console.log(e); -// res.status(400).send(e); -// } -// const requestEndTime = new Date().getTime(); -// analytics.track({ -// eventName: 'API Key Login', -// interfaceName: 'onApiKeyLogin', -// adapterName: platformName, -// accountId: hashedAccountId, -// extensionId: hashedExtensionId, -// success, -// requestDuration: (requestEndTime - requestStartTime) / 1000, -// userAgent, -// ip, -// author, -// isLegacy: true -// }); -// }) +app.post('/apiKeyLoginV2', async function (req, res) { + const requestStartTime = new Date().getTime(); + let platformName = null; + let success = false; + const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) + try { + const platform = req.body.platform; + platformName = platform; + const apiKey = req.body.apiKey; + const hostname = req.body.hostname; + const additionalInfo = req.body.additionalInfo; + if (!platform) { + throw 'Missing platform name'; + } + if (!apiKey) { + throw 'Missing api key'; + } + const { userInfo, returnMessage } = await authCore.onApiKeyLogin({ platform, hostname, apiKey, additionalInfo }); + const jwtToken = jwt.generateJwt({ + id: userInfo.id.toString(), + platform: platform + }); + res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); + success = true; + } + catch (e) { + console.log(e); + res.status(400).send(e); + success = false; + } + const requestEndTime = new Date().getTime(); + analytics.track({ + eventName: 'API Key Login', + interfaceName: 'onApiKeyLogin', + adapterName: platformName, + accountId: hashedAccountId, + extensionId: hashedExtensionId, + success, + requestDuration: (requestEndTime - requestStartTime) / 1000, + userAgent, + ip, + author, + isLegacy: true + }); +}) -// app.get('/contactV2', async function (req, res) { -// const requestStartTime = new Date().getTime(); -// let platformName = null; -// let success = false; -// const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) -// try { -// const jwtToken = req.query.jwtToken; -// if (!!jwtToken) { -// const { id: userId, platform } = jwt.decodeJwt(jwtToken); -// platformName = platform; -// const { successful, returnMessage, contact } = await contactCore.findContact({ platform, userId, phoneNumber: req.query.phoneNumber, overridingFormat: req.query.overridingFormat }); -// res.status(200).send({ successful, message: '', contact: contact.filter(c => c.id != 'createNewContact') }); -// success = true; -// } -// else { -// res.status(400).send('Please go to Settings and authorize CRM platform'); -// } -// } -// catch (e) { -// console.log(e); -// res.status(400).send(e); -// } -// const requestEndTime = new Date().getTime(); -// analytics.track({ -// eventName: 'Find contact', -// interfaceName: 'findContact', -// adapterName: platformName, -// accountId: hashedAccountId, -// extensionId: hashedExtensionId, -// success, -// requestDuration: (requestEndTime - requestStartTime) / 1000, -// userAgent, -// ip, -// author, -// isLegacy: true -// }); -// }); +app.get('/contactV2', async function (req, res) { + const requestStartTime = new Date().getTime(); + let platformName = null; + let success = false; + const { hashedExtensionId, hashedAccountId, userAgent, ip, author } = getAnalyticsVariablesInReqHeaders({ headers: req.headers }) + try { + const jwtToken = req.query.jwtToken; + if (!!jwtToken) { + const { id: userId, platform } = jwt.decodeJwt(jwtToken); + platformName = platform; + const { successful, returnMessage, contact } = await contactCore.findContact({ platform, userId, phoneNumber: req.query.phoneNumber, overridingFormat: req.query.overridingFormat }); + res.status(200).send({ successful, message: '', contact: contact.filter(c => c.id != 'createNewContact') }); + success = true; + } + else { + res.status(400).send('Please go to Settings and authorize CRM platform'); + success = false; + } + } + catch (e) { + console.log(e); + res.status(400).send(e); + success = false; + } + const requestEndTime = new Date().getTime(); + analytics.track({ + eventName: 'Find contact', + interfaceName: 'findContact', + adapterName: platformName, + accountId: hashedAccountId, + extensionId: hashedExtensionId, + success, + requestDuration: (requestEndTime - requestStartTime) / 1000, + userAgent, + ip, + author, + isLegacy: true + }); +}); exports.server = app; \ No newline at end of file