UNPKG

@twilio/flex-plugins-library-utils

Version:

Flex Plugins Library Utils

195 lines (169 loc) 7.76 kB
import { Twilio } from 'twilio'; import { retryHandler } from '../../common/retryHandler'; import { PluginsUtilsMetaData } from '../../types'; import { ApiReturnType } from '../../types'; import { PluginUtils } from '../../common/BaseClasses/PluginUtils'; import { HttpErrorCode } from '../../constants/statusCodes'; import { PluginUtilsErrorManager } from '../../common/PluginUtilsErrorManager'; import { InteractionChannelInviteInstance } from 'twilio/lib/rest/flexApi/v1/interaction/interactionChannel/interactionChannelInvite'; import { ChannelUpdateParams, ParticipantCreateInviteParams, ParticipantUpdateParams } from './types'; import { isString } from 'lodash'; import { InteractionChannelParticipantInstance } from 'twilio/lib/rest/flexApi/v1/interaction/interactionChannel/interactionChannelParticipant'; import { InteractionChannelInstance } from 'twilio/lib/rest/flexApi/v1/interaction/interactionChannel'; import { RETRY_STRATEGY } from 'src/constants/api'; export default class InteractionUtils extends PluginUtils { private config: PluginsUtilsMetaData; /** * Class for Twilio's Interaction API related utility functions * @param {Twilio} client Twilio client * @param {PluginsUtilsMetaData} config.maxBackoff Twilio service backoff max timeout * @param {PluginsUtilsMetaData} config.minBackoff Twilio service backoff min timeout * @param {PluginsUtilsMetaData} config.retryLimit Service retry limit * @param {PluginsUtilsMetaData} config.accountSid Twilio account sid * @param {PluginsUtilsMetaData} config.authToken Twilio auth token * */ constructor(client: Twilio, config: PluginsUtilsMetaData) { super(client); this.config = { ...config, maxBackoff: config.maxBackoff || RETRY_STRATEGY.MAX_BACKOFF, minBackoff: config.minBackoff || RETRY_STRATEGY.MIN_BACKOFF, retryLimit: config.retryLimit || RETRY_STRATEGY.RETRY_LIMIT, }; } /** * @param {string} parameters.interactionSid the Interaction Sid for this channel * @param {string} parameters.channelSid the sid of the channel * @param {object} parameters.routing the interactions routing logic * @returns {object} An object containing details about the interaction channel invite * @description the following method is used to create an Interaction Channel Invite */ public async participantCreateInvite( parameters: ParticipantCreateInviteParams, ): Promise<ApiReturnType & { participantInvite?: InteractionChannelInviteInstance }> { const { interactionSid, channelSid, routing, attempts } = parameters; const parameterChecks = { interactionSid: 'string', channelSid: 'string', routing: 'object', }; try { const inputError = PluginUtilsErrorManager.checkInvalidParameters<ParticipantCreateInviteParams>( parameters, parameterChecks, ); if (inputError) { throw new PluginUtilsErrorManager(inputError, HttpErrorCode.BadRequest); } const participantInvite = await this.client.flexApi.v1 .interaction(interactionSid) .channels(channelSid) .invites.create({ routing: routing, }); return { success: true, status: HttpErrorCode.OK, participantInvite }; } catch (error) { if (error.code === HttpErrorCode.BadRequest) { return { success: false, status: HttpErrorCode.BadRequest, message: error.message, }; } return retryHandler(this.config, error,{ ...parameters , attempts: !attempts ? 0 : attempts},this.participantCreateInvite); } } /** * @param {string} parameters.interactionSid the Interaction Sid for this channel * @param {string} parameters.channelSid The Channel Sid for this Participant * @param {string} parameters.participantSid the unique string created by Twilio to identify an Interaction Channel resource * @param {string} parameters.status the Participant's status - can be: closed or wrapup. Participant must be an agent. * @returns {object} An object containing the updated Participant * @description the following method is used to update/modify a channel participant */ public async participantUpdate( parameters: ParticipantUpdateParams, ): Promise<ApiReturnType & { updatedParticipant?: InteractionChannelParticipantInstance }> { const { interactionSid, channelSid, participantSid, status, attempts } = parameters; const parameterChecks = { interactionSid: 'string', channelSid: 'string', participantSid: 'string', status: 'string', }; try { const inputError = PluginUtilsErrorManager.checkInvalidParameters<ParticipantUpdateParams>( parameters, parameterChecks, ); if (inputError) { throw new PluginUtilsErrorManager(inputError, HttpErrorCode.BadRequest); } if (!isString(status) || (status != 'closed' && status != 'wrapup')) throw new PluginUtilsErrorManager( 'Invalid parameters object passed. Parameters must contain status to update the participant to and it must be one of "closed" or "wrapup"', HttpErrorCode.BadRequest, ); const updatedParticipant = await this.client.flexApi.v1 .interaction(interactionSid) .channels(channelSid) .participants(participantSid) .update({ status: status }); return { success: true, status: HttpErrorCode.OK, updatedParticipant }; } catch (error) { if (error.code === HttpErrorCode.BadRequest) { return { success: false, status: HttpErrorCode.BadRequest, message: error.message, }; } return retryHandler(this.config, error,{ ...parameters , attempts: !attempts ? 0 : attempts},this.participantUpdate); } } /** * @param {string} parameters.interactionSid the Interaction Sid for this channel * @param {string} parameters.channelSid The Channel Sid for this Participant * @param {string} parameters.status the channel status - can be: closed or wrapup * @returns {object} An object containing the modified channel * @description the following method is used to update/modify a channel */ public async channelUpdate( parameters: ChannelUpdateParams, ): Promise<ApiReturnType & { updatedChannel?: InteractionChannelInstance }> { const { interactionSid, channelSid, status, attempts } = parameters; const parameterChecks = { interactionSid: 'string', channelSid: 'string', status: 'string', }; try { const inputError = PluginUtilsErrorManager.checkInvalidParameters<ChannelUpdateParams>( parameters, parameterChecks, ); if (inputError) { throw new PluginUtilsErrorManager(inputError, HttpErrorCode.BadRequest); } if (!isString(status) || (status != 'closed' && status != 'inactive')) throw new PluginUtilsErrorManager( 'Invalid parameters object passed. Parameters must contain status to update the channel to and it must be one of "closed" or "wrapup"', HttpErrorCode.BadRequest, ); const updatedChannel = await this.client.flexApi.v1 .interaction(interactionSid) .channels(channelSid) .update({ status: status }); return { success: true, status: HttpErrorCode.OK, updatedChannel }; } catch (error) { if (error.code === HttpErrorCode.BadRequest) { return { success: false, status: HttpErrorCode.BadRequest, message: error.message, }; } return retryHandler(this.config, error,{ ...parameters , attempts: !attempts ? 0 : attempts}, this.channelUpdate); } } }