Skip to content

Commit

Permalink
Attempt to add external id on task updated if it hasn't already been …
Browse files Browse the repository at this point in the history
…added on task.created
  • Loading branch information
stephenhand committed Jan 15, 2025
1 parent 8b6030e commit f7ef066
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
27 changes: 23 additions & 4 deletions functions/taskrouterListeners/createContactListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import {
EventFields,
EventType,
TASK_CREATED,
TASK_UPDATED,
} from '@tech-matters/serverless-helpers/taskrouter';

import type { AddCustomerExternalId } from '../helpers/addCustomerExternalId.private';
import type { AddTaskSidToChannelAttributes } from '../helpers/addTaskSidToChannelAttributes.private';

export const eventTypes: EventType[] = [TASK_CREATED];
export const eventTypes: EventType[] = [TASK_CREATED, TASK_UPDATED];

type EnvVars = {
TWILIO_WORKSPACE_SID: string;
Expand All @@ -41,6 +42,14 @@ const isCreateContactTask = (
taskAttributes: { isContactlessTask?: boolean },
) => eventType === TASK_CREATED && !taskAttributes.isContactlessTask;

const isTaskRequiringExternalId = ({
isContactlessTask,
customers,
}: {
isContactlessTask?: boolean;
customers?: { external_id?: string };
}) => !isContactlessTask && !customers?.external_id;

/**
* Checks the event type to determine if the listener should handle the event or not.
* If it returns true, the taskrouter will invoke this listener.
Expand All @@ -50,15 +59,25 @@ export const shouldHandle = (event: EventFields) => eventTypes.includes(event.Ev
export const handleEvent = async (context: Context<EnvVars>, event: EventFields) => {
const { EventType: eventType, TaskAttributes: taskAttributesString } = event;
const taskAttributes = JSON.parse(taskAttributesString);

if (isCreateContactTask(eventType, taskAttributes)) {
console.log('Handling create contact...');
if (isTaskRequiringExternalId(taskAttributes)) {
if (eventType === TASK_CREATED) {
console.debug(
`Task ${event.TaskSid} requires an external_id but doesn't have one on event: ${eventType}`,
);
} else {
console.warn(
`Task ${event.TaskSid} still requires an external_id but doesn't have one on event: ${eventType}, meaning one wasn't assigned on creation. Attempting to assign now`,
);
}

// For offline contacts, this is already handled when the task is created in /assignOfflineContact function
const handlerPath = Runtime.getFunctions()['helpers/addCustomerExternalId'].path;
const addCustomerExternalId = require(handlerPath)
.addCustomerExternalId as AddCustomerExternalId;
await addCustomerExternalId(context, event);
}
if (isCreateContactTask(eventType, taskAttributes)) {
console.log('Handling create contact...');

if ((taskAttributes.customChannelType || taskAttributes.channelType) === 'web') {
// Add task sid to tasksSids channel attr so we can end the chat from webchat client (see endChat function)
Expand Down
2 changes: 1 addition & 1 deletion functions/webhooks/taskrouterCallback.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const runTaskrouterListeners = async (
};
console.info('Forwarding to delegate webhook:', delegateUrl);
console.info('event:', event);
console.debug('headers:', delegateHeaders);
console.debug('headers:', JSON.stringify(request.headers));
// Fire and forget
delegatePromise = fetch(delegateUrl, {
method: 'POST',
Expand Down
1 change: 0 additions & 1 deletion tech-matters-serverless-helpers-lib/src/tokenValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export const functionValidator = <
try {
const tokenResult: TokenValidatorResponse = await validator(token, accountSid, authToken)
const isGuestToken = !isWorker(tokenResult) || isGuest(tokenResult);

if (isGuestToken && !options.allowGuestToken) {
return failedResponse('Unauthorized: endpoint not open to guest tokens.');
}
Expand Down

0 comments on commit f7ef066

Please sign in to comment.