Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chi 1959 duplicated line contact created transfer rejected #491

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9358650
log task and workspace IDs
acedeywin Jul 11, 2023
04b92bf
fx: update isChatTransferToQueueComplete to pending
acedeywin Jul 12, 2023
de6efbe
ch: add testClient
acedeywin Jul 12, 2023
054895e
ch: add transferStatus to isChatTransferToWorkerRejected
acedeywin Jul 12, 2023
8d420dc
ch: update log
acedeywin Jul 12, 2023
3f09081
ft: modify TaskrouterCallback
acedeywin Jul 12, 2023
70c8b20
ft: modify TaskrouterCallback-2
acedeywin Jul 12, 2023
c01cf7f
ft: modify TaskrouterCallback-3
acedeywin Jul 12, 2023
5c1dc16
ch: update isChatTransferToQueueComplete
acedeywin Jul 12, 2023
dbbb103
ft: modify TaskrouterCallback-4
acedeywin Jul 12, 2023
0d5b563
ft: modify TaskrouterCallback-5
acedeywin Jul 12, 2023
44af053
ft: isChatTransferToWorkerRejected
acedeywin Jul 12, 2023
96a9d03
ft: isChatTransferToWorkerRejected-2
acedeywin Jul 12, 2023
ea36600
ch: add isChatTransferToQueueWorker
acedeywin Jul 12, 2023
b770cfa
ch: add isChatTransferToQueueWorker-2
acedeywin Jul 12, 2023
a056be2
fx: add taskSid
acedeywin Jul 12, 2023
b34dfd4
fx: add taskSid-2
acedeywin Jul 12, 2023
0f87df2
log taskSid
acedeywin Jul 13, 2023
2c2bc9a
log taskSid-2
acedeywin Jul 13, 2023
2b9861a
log taskSid-3
acedeywin Jul 13, 2023
db2bed2
log taskSid-4
acedeywin Jul 13, 2023
f0cce41
remove assignmentStatus: 'canceled'
acedeywin Jul 13, 2023
5845d3e
ch: update taskSid to originalTaskSid
acedeywin Jul 13, 2023
116cf82
ch: update taskSid to originalTaskSid-2
acedeywin Jul 13, 2023
c965acb
ft: implement hasTaskControl
acedeywin Jul 14, 2023
df06960
ft: remove hasTaskControl
acedeywin Jul 14, 2023
aff1343
ft: add hasTaskControl-2
acedeywin Jul 14, 2023
504b426
ch: move transfer to janitor
acedeywin Jul 14, 2023
d0e736c
log hasTaskControl
acedeywin Jul 14, 2023
e10249e
log hasTaskControl-2
acedeywin Jul 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion functions/taskrouterListeners/janitorListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {

import type { ChatChannelJanitor } from '../helpers/chatChannelJanitor.private';
import type { ChannelToFlex } from '../helpers/customChannels/customChannelToFlex.private';
import type { HasTaskControl, Attributes } from '../transfer/helpers.private';

export const eventTypes: EventType[] = [
TASK_CANCELED,
Expand All @@ -47,7 +48,11 @@ type EnvVars = {
const isCleanupPostSurvey = (eventType: EventType, taskAttributes: { isSurveyTask?: boolean }) =>
(eventType === TASK_CANCELED || eventType === TASK_WRAPUP) && taskAttributes.isSurveyTask;

const isCleanupCustomChannel = (eventType: EventType, taskAttributes: { channelType?: string }) => {
const handleTaskControl = Runtime.getFunctions()['transfer/helpers'].path;
const taskControl = require(handleTaskControl) as HasTaskControl;

const isCleanupCustomChannel = (eventType: EventType, taskAttributes: Attributes) => {
console.log('hasTaskControl(taskAttributes) 1', taskControl.hasTaskControl(taskAttributes));
if (
!(
eventType === TASK_DELETED ||
Expand All @@ -58,6 +63,10 @@ const isCleanupCustomChannel = (eventType: EventType, taskAttributes: { channelT
return false;
}

console.log('hasTaskControl(taskAttributes) 2', taskControl.hasTaskControl(taskAttributes));

if (!taskControl.hasTaskControl(taskAttributes)) return false;

const handlerPath = Runtime.getFunctions()['helpers/customChannels/customChannelToFlex'].path;
const channelToFlex = require(handlerPath) as ChannelToFlex;

Expand Down
85 changes: 78 additions & 7 deletions functions/taskrouterListeners/transfersListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
RESERVATION_REJECTED,
RESERVATION_TIMEOUT,
RESERVATION_WRAPUP,
// RESERVATION_CANCELED,
TASK_CANCELED,
TASK_QUEUE_ENTERED,
} from '@tech-matters/serverless-helpers/taskrouter';
Expand Down Expand Up @@ -93,6 +94,15 @@ const isChatTransferToQueueComplete = (
isChatTransfer(taskChannelUniqueName, taskAttributes) &&
taskAttributes.transferTargetType === 'queue';

// const isChatTransferToQueueWorker = (
// eventType: EventType,
// taskChannelUniqueName: string,
// taskAttributes: ChatTransferTaskAttributes,
// ) =>
// eventType === TASK_QUEUE_ENTERED &&
// isChatTransfer(taskChannelUniqueName, taskAttributes) &&
// taskAttributes.transferTargetType === 'worker';

const isWarmVoiceTransferRejected = (
eventType: EventType,
taskChannelUniqueName: string,
Expand Down Expand Up @@ -167,18 +177,25 @@ const updateWarmVoiceTransferAttributes = async (
export const shouldHandle = (event: EventFields) => eventTypes.includes(event.EventType);

export const handleEvent = async (context: Context<EnvVars>, event: EventFields) => {
const {
EventType: eventType,
TaskChannelUniqueName: taskChannelUniqueName,
TaskSid: taskSid,
TaskAttributes: taskAttributesString,
} = event;

const clients = context.getTwilioClient();
const testClient = await clients.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(taskSid)
.fetch();
try {
const {
EventType: eventType,
TaskChannelUniqueName: taskChannelUniqueName,
TaskSid: taskSid,
TaskAttributes: taskAttributesString,
} = event;

console.log(`===== Executing TransfersListener for event: ${eventType} =====`);

const taskAttributes = JSON.parse(taskAttributesString);

console.log('testClient here', testClient.assignmentStatus, taskAttributes.isInMyBehalf);

/**
* If a chat transfer gets accepted, it should:
* 1) Complete the original task
Expand All @@ -189,6 +206,15 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
const { originalTask: originalTaskSid } = taskAttributes.transferMeta;
const client = context.getTwilioClient();

console.log('testClient here 1', testClient.assignmentStatus);
console.log('originalTaskSid 1:', originalTaskSid, 'taskSid: 1', taskSid);

console.log(
'isChatTransferToWorkerAccepted',
originalTaskSid,
taskAttributes.transferTargetType,
);

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(originalTaskSid)
Expand All @@ -211,6 +237,14 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

const { originalTask: originalTaskSid } = taskAttributes.transferMeta;
const client = context.getTwilioClient();
console.log('testClient here 2', testClient.assignmentStatus);
console.log('originalTaskSid: 2', originalTaskSid, 'taskSid: 2', taskSid);

console.log(
'isChatTransferToQueueComplete',
originalTaskSid,
taskAttributes.transferTargetType,
);

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
Expand All @@ -224,6 +258,30 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
return;
}

// if (isChatTransferToQueueWorker(eventType, taskChannelUniqueName, taskAttributes)) {
// console.log('Handling chat transfer to queue entering target queue...');

// const { originalTask: originalTaskSid } = taskAttributes.transferMeta;
// const client = context.getTwilioClient();

// console.log(
// 'isChatTransferToQueueComplete',
// originalTaskSid,
// taskAttributes.transferTargetType,
// );

// await client.taskrouter
// .workspaces(context.TWILIO_WORKSPACE_SID)
// .tasks(originalTaskSid)
// .update({
// assignmentStatus: 'pending',
// reason: 'task transferred into queue',
// });

// console.log('Finished handling chat queue transfer.');
// return;
// }

/**
* If a chat transfer gets rejected, it should:
* 1) Adjust original task attributes:
Expand All @@ -237,6 +295,14 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
const { originalTask: originalTaskSid } = taskAttributes.transferMeta;
const client = context.getTwilioClient();

console.log(
'isChatTransferToWorkerRejected',
originalTaskSid,
taskAttributes.transferTargetType,
);
console.log('testClient here 3', testClient.assignmentStatus);
console.log('originalTaskSid: 3', originalTaskSid, 'taskSid: 3', taskSid);

const originalTask = await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(originalTaskSid)
Expand All @@ -252,6 +318,7 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
transferMeta: {
...originalAttributes.transferMeta,
sidWithTaskControl: originalAttributes.transferMeta.originalReservation,
transferStatus: 'rejected',
},
};

Expand All @@ -268,6 +335,9 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
}),
]);

console.log('testClient here 4', testClient.assignmentStatus);
console.log('originalTaskSid: 4', originalTaskSid, 'taskSid: 4', taskSid);

console.log('Finished handling chat transfer rejected.');
return;
}
Expand Down Expand Up @@ -308,6 +378,7 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
} catch (err) {
console.log('===== TransfersListener has failed =====');
console.log(String(err));
console.log('testClient here 5', testClient.assignmentStatus);
throw err;
}
};
Expand Down
53 changes: 53 additions & 0 deletions functions/transfer/helpers.private.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */

export type TransferMeta = {
mode: 'COLD' | 'WARM';
transferStatus: 'transferring' | 'accepted' | 'rejected';
sidWithTaskControl: string;
};

export type Attributes = {
transferMeta?: TransferMeta;
isContactlessTask?: true;
isInMyBehalf?: true;
taskSid: string;
channelType?: string;
};

export const offlineContactTaskSid = 'offline-contact-task-sid';

export const isInMyBehalfITask = (task: Attributes) =>
task && task.isContactlessTask && task.isInMyBehalf;

export const isOfflineContactTask = (task: Attributes) => task.taskSid === offlineContactTaskSid;

export const isTwilioTask = (task: Attributes) =>
task && !isOfflineContactTask(task) && !isInMyBehalfITask(task);

export const hasTransferStarted = (task: Attributes) => Boolean(task && task.transferMeta);

export const hasTaskControl = (task: Attributes) =>
!isTwilioTask(task) ||
!hasTransferStarted(task) ||
task.transferMeta?.sidWithTaskControl === task.taskSid;

export type HasTaskControl = {
hasTaskControl: typeof hasTaskControl;
};
1 change: 1 addition & 0 deletions functions/webhooks/line/FlexToLine.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const handler = async (
switch (result.status) {
case 'sent':
resolve(success(result.response));
console.log('result.response 1', result.response);
return;
case 'ignored':
resolve(success('Ignored event.'));
Expand Down
1 change: 1 addition & 0 deletions functions/webhooks/line/LineToFlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const handler = async (
switch (result.status) {
case 'sent':
responses.push(result.response);
console.log('result.response 2', result.response, responses);
break;
case 'ignored':
responses.push('Ignored event.');
Expand Down