Skip to content

Commit

Permalink
change: updates on error handling and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DaKingKong committed Oct 16, 2024
1 parent fbc637a commit ef4b9b1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ client/src/manifest.json
/devFlow.puml
/devGuideDraft.md
/build/*
/src/test.js
20 changes: 13 additions & 7 deletions src/adapters/bullhorn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
{
Expand Down Expand Up @@ -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 {
Expand All @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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', '')}`,
{
Expand Down Expand Up @@ -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', '')}`,
{
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/insightly/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/adapters/pipedrive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/redtail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function getUserInfo({ authHeader, additionalInfo }) {
const timezoneName = '';
const timezoneOffset = null;
return {
successful: false,
successful: true,
platformUserInfo: {
id,
name,
Expand All @@ -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
}
}
Expand Down
30 changes: 19 additions & 11 deletions src/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
12 changes: 9 additions & 3 deletions src/core/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
}
}

Expand Down Expand Up @@ -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 } };
}
}
}

Expand Down Expand Up @@ -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 } };
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ef4b9b1

Please sign in to comment.