Skip to content

Commit

Permalink
[Bug fix] Make channel capture cleanup function compatible with chann…
Browse files Browse the repository at this point in the history
…el not captured (#511)
  • Loading branch information
GPaoloni authored Sep 8, 2023
1 parent 466f3f9 commit 2e2dc5e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
11 changes: 6 additions & 5 deletions functions/channelCapture/channelCaptureHandlers.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ const triggerWithUserMessage = async (
inputText,
});

if (lexResult.status === 'failure') {
throw lexResult.error;
}

const chatbotCallbackWebhook = await channel.webhooks().create({
type: 'webhook',
configuration: {
Expand All @@ -186,6 +182,11 @@ const triggerWithUserMessage = async (
chatbotCallbackWebhookSid: chatbotCallbackWebhook.sid,
});

// Bubble exception after the channel is updated because capture attributes are needed for the cleanup
if (lexResult.status === 'failure') {
throw lexResult.error;
}

const { lexResponse } = lexResult;

// Send message to trigger the recently created chatbot integration
Expand Down Expand Up @@ -506,7 +507,7 @@ const handlePostSurveyComplete = async (
: !postSurveyConfigJson
? `No postSurveyConfigJson found for definitionVersion ${definitionVersion}.`
: `postSurveyConfigJson for definitionVersion ${definitionVersion} is not a Twilio asset as expected`; // This should removed when if we move definition versions to an external source.
console.error(`Error accessing to the post survey form definitions: ${errorMEssage}`);
console.info(`Error accessing to the post survey form definitions: ${errorMEssage}`);
}
};

Expand Down
44 changes: 27 additions & 17 deletions functions/channelCapture/lexClient.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export type AWSCredentials = {
AWS_REGION: string;
};

export type BotType = 'pre_survey' | 'post_survey';

export type LexMemory = { [q: string]: string | number };

export const postText = async (
Expand Down Expand Up @@ -55,14 +53,17 @@ export const postText = async (

return { status: 'success', lexResponse } as const;
} catch (error) {
return { status: 'failure', error } as { status: 'failure'; error: Error };
return {
status: 'failure',
error: error instanceof Error ? error : new Error(String(error)),
} as const;
}
};

export const isEndOfDialog = (dialogState: string | undefined) =>
dialogState === 'Fulfilled' || dialogState === 'Failed';

export const deleteSession = (
export const deleteSession = async (
credentials: AWSCredentials,
{
botName,
Expand All @@ -74,21 +75,30 @@ export const deleteSession = (
userId: string;
},
) => {
AWS.config.update({
credentials: {
accessKeyId: credentials.ASELO_APP_ACCESS_KEY,
secretAccessKey: credentials.ASELO_APP_SECRET_KEY,
},
region: credentials.AWS_REGION,
});
try {
AWS.config.update({
credentials: {
accessKeyId: credentials.ASELO_APP_ACCESS_KEY,
secretAccessKey: credentials.ASELO_APP_SECRET_KEY,
},
region: credentials.AWS_REGION,
});

const Lex = new AWS.LexRuntime();

const Lex = new AWS.LexRuntime();
const lexResponse = await Lex.deleteSession({
botName,
botAlias,
userId,
}).promise();

return Lex.deleteSession({
botName,
botAlias,
userId,
}).promise();
return { status: 'success', lexResponse } as const;
} catch (error) {
return {
status: 'failure',
error: error instanceof Error ? error : new Error(String(error)),
} as const;
}
};

export type LexClient = {
Expand Down
2 changes: 1 addition & 1 deletion functions/helpers/addCustomerExternalId.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const logAndReturnError = (
errorInstance: unknown,
) => {
const errorMessage = `Error at addCustomerExternalId: task with sid ${taskSid} does not exists in workspace ${workspaceSid} when trying to ${step} it.`;
console.error(errorMessage, errorInstance);
console.info(errorMessage, errorInstance);
return { message: errorMessage };
};

Expand Down
4 changes: 2 additions & 2 deletions functions/postSurveyComplete.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const getPostSurveyCompleteMessage = async (

if (translation.postSurveyCompleteMessage) return translation.postSurveyCompleteMessage;
} catch {
console.error(
console.info(
`Couldn't retrieve postSurveyCompleteMessage translation for ${taskLanguage}, neither default (en-US).`,
);
}
Expand Down Expand Up @@ -196,7 +196,7 @@ export const handler: ServerlessFunctionSignature<EnvVars, Event> = async (
: !postSurveyConfigJson
? `No postSurveyConfigJson found for definitionVersion ${definitionVersion}.`
: `postSurveyConfigJson for definitionVersion ${definitionVersion} is not a Twilio asset as expected`; // This should removed when if we move definition versions to an external source.
console.error(`Error accessing to the post survey form definitions: ${errorMEssage}`);
console.info(`Error accessing to the post survey form definitions: ${errorMEssage}`);
}

// As survey tasks will never be assigned to a worker, they'll be kept in pending state. A pending can't transition to completed state, so we cancel them here to raise a task.canceled taskrouter event.
Expand Down
2 changes: 1 addition & 1 deletion functions/postSurveyInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const getTriggerMessage = async (

if (translation.triggerMessage) return translation.triggerMessage;
} catch {
console.error(`Couldn't retrieve triggerMessage translation for ${taskLanguage}`);
console.info(`Couldn't retrieve triggerMessage translation for ${taskLanguage}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/addCustomerExternalId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const baseContext = {

const liveAttributes = { some: 'some', customers: { other: 1 } };

const logSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const logSpy = jest.spyOn(console, 'info').mockImplementation(() => {});

beforeAll(() => {
helpers.setup({});
Expand Down

0 comments on commit 2e2dc5e

Please sign in to comment.