diff --git a/.gitignore b/.gitignore index d9acf6d4..6931d3e7 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ client/src/manifest.json /devFlow.puml /devGuideDraft.md /build/* +/src/test.js diff --git a/src/adapters/bullhorn/index.js b/src/adapters/bullhorn/index.js index b2c83295..a356a3f3 100644 --- a/src/adapters/bullhorn/index.js +++ b/src/adapters/bullhorn/index.js @@ -98,7 +98,7 @@ async function findContact({ user, phoneNumber }) { }); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); commentActionListResponse = await axios.get(`${user.platformAdditionalInfo.restUrl}settings/commentActionList`, { @@ -264,7 +264,9 @@ async function createCallLog({ user, contactInfo, authHeader, callLog, note, add personReference: { id: contactInfo.id }, - dateAdded: callLog.startTime + dateAdded: callLog.startTime, + externalID: callLog.sessionId, + minutesSpent: callLog.duration / 60 } let addLogRes; try { @@ -279,7 +281,7 @@ async function createCallLog({ user, contactInfo, authHeader, callLog, note, add ); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); addLogRes = await axios.put( `${user.platformAdditionalInfo.restUrl}entity/Note`, @@ -315,7 +317,7 @@ async function updateCallLog({ user, existingCallLog, authHeader, recordingLink, }); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); getLogRes = await axios.get( `${user.platformAdditionalInfo.restUrl}entity/Note/${existingBullhornLogId}?fields=comments`, @@ -377,7 +379,7 @@ async function createMessageLog({ user, contactInfo, authHeader, message, additi }); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); userInfoResponse = await axios.get(`${user.platformAdditionalInfo.restUrl}query/CorporateUser?fields=id,name&where=id=${user.id.replace('-bullhorn', '')}`, { @@ -462,7 +464,7 @@ async function updateMessageLog({ user, contactInfo, existingMessageLog, message }); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); userInfoResponse = await axios.get(`${user.platformAdditionalInfo.restUrl}query/CorporateUser?fields=id,name&where=id=${user.id.replace('-bullhorn', '')}`, { @@ -520,7 +522,7 @@ async function getCallLog({ user, callLogId, authHeader }) { }); } catch (e) { - if (e.response.status === 401) { + if (isAuthError(e.response.status)) { user = await refreshSessionToken(user); getLogRes = await axios.get( `${user.platformAdditionalInfo.restUrl}entity/Note/${callLogId}?fields=comments,candidates,clientContacts`, @@ -563,6 +565,10 @@ async function refreshSessionToken(user) { return user; } +function isAuthError(statusCode) { + return statusCode >= 400 && statusCode < 500; +} + exports.getAuthType = getAuthType; exports.getOauthInfo = getOauthInfo; exports.getOverridingOAuthOption = getOverridingOAuthOption; diff --git a/src/adapters/insightly/index.js b/src/adapters/insightly/index.js index 8557b97d..0039b6cc 100644 --- a/src/adapters/insightly/index.js +++ b/src/adapters/insightly/index.js @@ -45,7 +45,7 @@ async function getUserInfo({ authHeader, additionalInfo }) { successful: false, returnMessage: { messageType: 'warning', - message: 'Failed to get user info.', + message: 'Failed to get user info. Please check your API key and try again.', ttl: 3000 } } diff --git a/src/adapters/pipedrive/index.js b/src/adapters/pipedrive/index.js index d2a43543..0c60a406 100644 --- a/src/adapters/pipedrive/index.js +++ b/src/adapters/pipedrive/index.js @@ -95,7 +95,14 @@ async function findContact({ user, authHeader, phoneNumber, overridingFormat }) phoneNumber = phoneNumber.replace(' ', '+') // without + is an extension, we don't want to search for that if (!phoneNumber.includes('+')) { - return null; + return { + matchedContactInfo: null, + returnMessage: { + message: 'Logging against internal extension number is not supported.', + messageType: 'warning', + ttl: 3000 + } + }; } const phoneNumberObj = parsePhoneNumber(phoneNumber); let phoneNumberWithoutCountryCode = phoneNumber; diff --git a/src/adapters/redtail/index.js b/src/adapters/redtail/index.js index 0b8f0f91..fd35b87b 100644 --- a/src/adapters/redtail/index.js +++ b/src/adapters/redtail/index.js @@ -30,7 +30,7 @@ async function getUserInfo({ authHeader, additionalInfo }) { const timezoneName = ''; const timezoneOffset = null; return { - successful: false, + successful: true, platformUserInfo: { id, name, @@ -51,7 +51,7 @@ async function getUserInfo({ authHeader, additionalInfo }) { successful: false, returnMessage: { messageType: 'warning', - message: 'Failed to get user info.', + message: 'Failed to get user info. Please check your credentials.', ttl: 3000 } } diff --git a/src/core/auth.js b/src/core/auth.js index 7721272a..96fac1ca 100644 --- a/src/core/auth.js +++ b/src/core/auth.js @@ -53,17 +53,25 @@ async function onOAuthCallback({ platform, hostname, tokenUrl, callbackUri, apiU async function onApiKeyLogin({ platform, hostname, apiKey, additionalInfo }) { const platformModule = require(`../adapters/${platform}`); const basicAuth = platformModule.getBasicAuth({ apiKey }); - const { platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader: `Basic ${basicAuth}`, hostname, additionalInfo }); - const userInfo = await saveUserInfo({ - platformUserInfo, - platform, - hostname, - accessToken: platformUserInfo.overridingApiKey ?? apiKey - }); - return { - userInfo, - returnMessage - }; + const { successful, platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader: `Basic ${basicAuth}`, hostname, additionalInfo }); + if (successful) { + const userInfo = await saveUserInfo({ + platformUserInfo, + platform, + hostname, + accessToken: platformUserInfo.overridingApiKey ?? apiKey + }); + return { + userInfo, + returnMessage + }; + } + else { + return { + userInfo: null, + returnMessage + } + } } async function saveUserInfo({ platformUserInfo, platform, hostname, accessToken, refreshToken, tokenExpiry }) { diff --git a/src/core/log.js b/src/core/log.js index cc532cf1..9c3c1f20 100644 --- a/src/core/log.js +++ b/src/core/log.js @@ -84,7 +84,7 @@ async function createCallLog({ platform, userId, incomingData }) { if (e.response?.status === 429) { return { successful: false, returnMessage: { message: `${platform} rate limit reached. Please try again the next minute.`, messageType: 'warning', ttl: 5000 } }; } - return { successful: false }; + return { successful: false, returnMessage: { message: `Failed to create call log.`, messageType: 'warning', ttl: 5000 } }; } } @@ -188,7 +188,13 @@ async function updateCallLog({ platform, userId, incomingData }) { if (e.response?.status === 429) { return { successful: false, returnMessage: { message: `${platform} rate limit reached. Please try again the next minute.`, messageType: 'warning', ttl: 5000 } }; } - return { successful: false }; + if(!!incomingData.recordingLink) + { + return { successful: false, returnMessage: { message: `Failed to upload call recording link.`, messageType: 'warning', ttl: 5000 } }; + } + else{ + return { successful: false, returnMessage: { message: `Failed to update call log. Please check if the log entity still exist on ${platform}`, messageType: 'warning', ttl: 5000 } }; + } } } @@ -314,7 +320,7 @@ async function createMessageLog({ platform, userId, incomingData }) { if (e.response?.status === 429) { return { successful: false, returnMessage: { message: `${platform} rate limit reached. Please try again the next minute.`, messageType: 'warning', ttl: 5000 } }; } - return { successful: false }; + return { successful: false, returnMessage: { message: `Failed to create message log.`, messageType: 'warning', ttl: 5000 } }; } } diff --git a/src/index.js b/src/index.js index dc4b77e5..875d54b6 100644 --- a/src/index.js +++ b/src/index.js @@ -215,12 +215,18 @@ app.post('/apiKeyLogin', async function (req, res) { 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; + if (!!userInfo) { + const jwtToken = jwt.generateJwt({ + id: userInfo.id.toString(), + platform: platform + }); + res.status(200).send({ jwtToken, name: userInfo.name, returnMessage }); + success = true; + } + else { + res.status(400).send({ returnMessage }); + success = false; + } } catch (e) { console.log(`platform: ${platformName} \n${e.stack}`); @@ -314,8 +320,7 @@ app.get('/contact', async function (req, res) { platformName = platform; const { successful, returnMessage, contact } = await contactCore.findContact({ platform, userId, phoneNumber: req.query.phoneNumber, overridingFormat: req.query.overridingFormat }); res.status(200).send({ successful, returnMessage, contact }); - if(successful) - { + if (successful) { const nonNewContact = contact.filter(c => !c.isNewContact); resultCount = nonNewContact.length; success = true;