From e20cb0c97d564a9512a44822fe598573ed0cf05b Mon Sep 17 00:00:00 2001 From: Stephen Hand Date: Mon, 10 Jun 2024 14:51:03 +0100 Subject: [PATCH] Try 'parking' an interaction for post surveys --- .../channelCaptureHandlers.private.ts | 2 +- functions/postSurveyInit.ts | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/functions/channelCapture/channelCaptureHandlers.private.ts b/functions/channelCapture/channelCaptureHandlers.private.ts index 0366d427..ed7ec9a7 100644 --- a/functions/channelCapture/channelCaptureHandlers.private.ts +++ b/functions/channelCapture/channelCaptureHandlers.private.ts @@ -549,7 +549,7 @@ const createStudioFlowTrigger = async ( controlTask: TaskInstance, ) => { // Canceling tasks triggers janitor (see functions/taskrouterListeners/janitorListener.private.ts), so we remove this one since is not needed - controlTask.remove(); + await controlTask.remove(); const { isConversation } = capturedChannelAttributes; if (isConversation) { diff --git a/functions/postSurveyInit.ts b/functions/postSurveyInit.ts index 13a640c6..15ccf3af 100644 --- a/functions/postSurveyInit.ts +++ b/functions/postSurveyInit.ts @@ -85,7 +85,26 @@ export const postSurveyInitHandler = async ( context: Context, event: Omit, ) => { - const { channelSid, conversationSid, taskSid, taskLanguage } = event; + const { channelSid, conversationSid, taskSid, taskLanguage, channelType } = event; + const client = context.getTwilioClient(); + const task = await client.taskrouter + .workspaces(context.TWILIO_WORKSPACE_SID) + .tasks(taskSid) + .fetch(); + const { flexInteractionSid, flexInteractionChannelSid } = JSON.parse(task.attributes); + const interactionParticipantContext = client.flexApi.v1 + .interaction(flexInteractionSid) + .channels(flexInteractionChannelSid).participants; + const interactionAgentParticipants = (await interactionParticipantContext.list()).filter( + (p) => p.type === 'agent', + ); + + // Should only be 1, but just in case + await Promise.all( + interactionAgentParticipants.map((p) => + interactionParticipantContext(p.sid).update({ status: 'wrapup' }), + ), + ); const triggerMessage = await getTriggerMessage(event, context); @@ -109,7 +128,7 @@ export const postSurveyInitHandler = async ( language: taskLanguage, // if there's a task language, attach it to the post survey task }), controlTaskTTL: 3600, - channelType: '', + channelType, } as const; if (conversationSid) { return channelCaptureHandlers.handleChannelCapture(context, { @@ -140,7 +159,7 @@ export const handler = TokenValidator( try { const { channelSid, taskSid, taskLanguage, conversationSid } = event; - if (!channelSid && !conversationSid) return resolve(error400('channelSid')); + if (!channelSid && !conversationSid) return resolve(error400('channelSid / conversationSid')); if (!taskSid) return resolve(error400('taskSid')); if (!taskLanguage) return resolve(error400('taskLanguage'));