Skip to content

Commit

Permalink
Queue Transfer Capacity workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Jul 25, 2024
1 parent f218b09 commit ee7be6a
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions functions/taskrouterListeners/transfersListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ const updateWarmVoiceTransferAttributes = async (
*/
export const shouldHandle = (event: EventFields) => eventTypes.includes(event.EventType);

const decreaseChatCapacity = async (context: Context<EnvVars>, workerSid: string) => {
const serviceConfig = await context.getTwilioClient().flexApi.configuration.get().fetch();
const {
feature_flags: { enable_backend_manual_pulling: enableBackendManualPulling },
} = serviceConfig.attributes;
if (enableBackendManualPulling) {
const { path } = Runtime.getFunctions().adjustChatCapacity;
// eslint-disable-next-line global-require,import/no-dynamic-require,prefer-destructuring
const adjustChatCapacity: AdjustChatCapacityType = require(path).adjustChatCapacity;

const body = {
workerSid,
adjustment: 'decrease',
} as const;

await adjustChatCapacity(context, body);
}
};

export const handleEvent = async (context: Context<EnvVars>, event: EventFields) => {
try {
const {
Expand Down Expand Up @@ -215,22 +234,11 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

if (isChatTransferToQueueComplete(eventType, taskChannelUniqueName, taskAttributes)) {
console.log('Handling chat transfer to queue entering target queue...');
await decreaseChatCapacity(context, workerSid);

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

const { path } = Runtime.getFunctions().adjustChatCapacity;

// eslint-disable-next-line global-require,import/no-dynamic-require,prefer-destructuring
const adjustChatCapacity: AdjustChatCapacityType = require(path).adjustChatCapacity;

const body = {
workerSid,
adjustment: 'decrease',
} as const;

await adjustChatCapacity(context, body);

await client.taskrouter
.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(originalTaskSid)
Expand Down

0 comments on commit ee7be6a

Please sign in to comment.