Skip to content

Commit

Permalink
Add contactId to task on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Dec 16, 2024
1 parent a8d4b84 commit 217c503
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions functions/taskrouterListeners/createHrmContactListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const BLANK_CONTACT: HrmContact = {
export const shouldHandle = (event: EventFields) => eventTypes.includes(event.EventType);

export const handleEvent = async (
{ getTwilioClient, HRM_STATIC_KEY }: Context<EnvVars>,
{ getTwilioClient, HRM_STATIC_KEY, TWILIO_WORKSPACE_SID }: Context<EnvVars>,
event: EventFields,
) => {
const { TaskAttributes: taskAttributesString, TaskSid: taskSid, WorkerSid: workerSid } = event;
Expand All @@ -89,7 +89,8 @@ export const handleEvent = async (
return;
}

const serviceConfig = await getTwilioClient().flexApi.configuration.get().fetch();
const client = getTwilioClient();
const serviceConfig = await client.flexApi.configuration.get().fetch();

const {
definitionVersion,
Expand Down Expand Up @@ -147,6 +148,14 @@ export const handleEvent = async (
}
const { id }: HrmContact = await response.json();
console.info(`Created HRM contact with id ${id} for task ${taskSid}`);

const taskContext = client.taskrouter.v1.workspaces.get(TWILIO_WORKSPACE_SID).tasks.get(taskSid);
const currentTaskAttributes = (await taskContext.fetch()).attributes; // Less chance of race conditions if we fetch the task attributes again, still not the best...
const updatedAttributes = {
...JSON.parse(currentTaskAttributes),
contactId: id.toString(),
};
await taskContext.update({ attributes: JSON.stringify(updatedAttributes) });
};

/**
Expand Down

0 comments on commit 217c503

Please sign in to comment.