Skip to content

Commit

Permalink
Ensure new contacts are not created for transfer tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Dec 17, 2024
1 parent 217c503 commit 91f0d91
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions functions/taskrouterListeners/createHrmContactListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,35 @@ const BLANK_CONTACT: HrmContact = {
* 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.
*/
export const shouldHandle = (event: EventFields) => eventTypes.includes(event.EventType);
export const shouldHandle = ({
TaskAttributes: taskAttributesString,
TaskSid: taskSid,
EventType: eventType,
}: EventFields) => {
if (!eventTypes.includes(eventType)) return false;

const { isContactlessTask, transferTargetType } = JSON.parse(taskAttributesString ?? '{}');

if (isContactlessTask) {
console.debug(`Task ${taskSid} is a contactless task, contact was already created in Flex.`);
return false;
}

if (transferTargetType) {
console.debug(
`Task ${taskSid} was created to receive a ${transferTargetType} transfer. The original contact will be used so a new one will not be created.`,
);
return false;
}
return true;
};

export const handleEvent = async (
{ getTwilioClient, HRM_STATIC_KEY, TWILIO_WORKSPACE_SID }: Context<EnvVars>,
event: EventFields,
{ TaskAttributes: taskAttributesString, TaskSid: taskSid, WorkerSid: workerSid }: EventFields,
) => {
const { TaskAttributes: taskAttributesString, TaskSid: taskSid, WorkerSid: workerSid } = event;
const taskAttributes = taskAttributesString ? JSON.parse(taskAttributesString) : {};
const { isContactlessTask, channelSid } = taskAttributes;

if (isContactlessTask) {
console.debug(`Task ${taskSid} is a contactless task, skipping contact creation.`);
return;
}
const { channelSid } = taskAttributes;

const client = getTwilioClient();
const serviceConfig = await client.flexApi.configuration.get().fetch();
Expand Down Expand Up @@ -162,9 +177,9 @@ export const handleEvent = async (
* The taskrouter callback expects that all taskrouter listeners return
* a default object of type TaskrouterListener.
*/
const transfersListener: TaskrouterListener = {
const createHrmContactListener: TaskrouterListener = {
shouldHandle,
handleEvent,
};

export default transfersListener;
export default createHrmContactListener;

0 comments on commit 91f0d91

Please sign in to comment.