Skip to content

Commit

Permalink
implement updateWarmVoiceTransferAttributes for DRY purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
acedeywin committed Jul 5, 2023
1 parent 5ca5d1f commit 90e6f69
Showing 1 changed file with 40 additions and 46 deletions.
86 changes: 40 additions & 46 deletions functions/taskrouterListeners/transfersListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,44 @@ const isWarmVoiceTransferTimedOut = (
taskChannelUniqueName === 'voice' &&
taskAttributes.transferMeta &&
taskAttributes.transferMeta.mode === 'WARM';

/**
* updateWarmVoiceTransferAttributes is a DRY function that checks
* when warm voice transfer gets rejected or timeout
*
* If a warm voice transfer gets rejected, it should:
* 1) Adjust original task attributes:
* - transferMeta.transferStatus: 'rejected'
* - transferMeta.sidWithTaskControl: to original reservation
* Same applies to when transfer timeout
*/
const updateWarmVoiceTransferAttributes = async (
transferStatus: string,
context: Context<EnvVars>,
taskAttributes: { transferMeta: { originalReservation: string } },
taskSid: string,
) => {
console.log(`Handling warm voice transfer ${transferStatus}...`);

const client = context.getTwilioClient();

const updatedAttributes = {
...taskAttributes,
transferMeta: {
...taskAttributes.transferMeta,
sidWithTaskControl: taskAttributes.transferMeta.originalReservation,
transferStatus,
},
};

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(taskSid)
.update({ attributes: JSON.stringify(updatedAttributes) });

console.log(`Finished handling warm voice transfer ${transferStatus}.`);
};

/**
* 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 Down Expand Up @@ -236,57 +274,13 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)
return;
}

/**
* If a warm voice transfer gets rejected, it should:
* 1) Adjust original task attributes:
* - transferMeta.transferStatus: 'rejected'
* - transferMeta.sidWithTaskControl: to original reservation
*/
if (isWarmVoiceTransferRejected(eventType, taskChannelUniqueName, taskAttributes)) {
console.log('Handling warm voice transfer rejected...');

const client = context.getTwilioClient();

const updatedAttributes = {
...taskAttributes,
transferMeta: {
...taskAttributes.transferMeta,
sidWithTaskControl: taskAttributes.transferMeta.originalReservation,
transferStatus: 'rejected',
},
};

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(taskSid)
.update({
attributes: JSON.stringify(updatedAttributes),
});

console.log('Finished handling warm voice transfer rejected.');
await updateWarmVoiceTransferAttributes('rejected', context, taskAttributes, taskSid);
return;
}

if (isWarmVoiceTransferTimedOut(eventType, taskChannelUniqueName, taskAttributes)) {
console.log('Handling warm voice transfer timeout...');

const client = context.getTwilioClient();

const updatedAttributes = {
...taskAttributes,
transferMeta: {
...taskAttributes.transferMeta,
sidWithTaskControl: taskAttributes.transferMeta.originalReservation,
transferStatus: 'timeout',
},
};

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(taskSid)
.update({ attributes: JSON.stringify(updatedAttributes) });

console.log('Finished handling warm voice transfer timeout.');
await updateWarmVoiceTransferAttributes('timeout', context, taskAttributes, taskSid);
return;
}

Expand Down

0 comments on commit 90e6f69

Please sign in to comment.