Home Reference Source

src/support/SupportService.ts

import {ContextInfo} from '..';
import {ConferenceManager} from '../conferences/ConferenceManager';
import {MediaTypes} from '../types';
import {Conference} from '../conferences/Conference';
import {v4 as uuid} from 'uuid';
/**
 * This class provides a wrapper over the ConferenceManager class that allows to
 * call an agent.
 */
export class SupportService {
	/** @private */
	private conferenceManager: ConferenceManager;

	/**
	 * @private
	 */
	private constructor(conferenceManager: ConferenceManager) {
		/** @private */
		this.conferenceManager = conferenceManager;
	}

	/** @private */
	public static of(conferenceManager: ConferenceManager): SupportService {
		return new SupportService(conferenceManager);
	}

	/**
	 * @desc Invites an agent to a conference
	 * @param {MediaTypes} mediaTypes the media types that will be used to call an agent
	 * @param {Object} [contextInfo={}] arbitrary context that can be sent to an agent
	 * @return {Promise<Conference>}
	 */
	public async callAgent(mediaTypes: MediaTypes, contextInfo?: ContextInfo): Promise<Conference> {
		const conference = await this.conferenceManager.createConference();
		await conference.join();
		const agent = this.generateWacAgentUri();
		const response = await conference.inviteParticipant(
			agent,
			undefined,
			mediaTypes,
			undefined,
			undefined,
			undefined,
			contextInfo,
		);
		if (response.code < 200 || response.code >= 300) {
			throw new Error('Error calling an agent');
		}
		return conference;
	}

	private generateWacAgentUri(): string {
		return `wac-agent:${uuid()}`;
	}
}