Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createParticipant, fetchInboundCall, fetchPhoneCall } from '../phone-call-actions';
- import { getBaseUrl } from '../../../shared/utils';
- import { twilio } from '../../../shared/twilio';
- import { fetchAgencySID } from '../../../shared/users/users-actions';
- import * as qs from 'querystring';
- /**
- * Transfer call resolver.
- *
- * @param {object} event - The cloud function's event object.
- * @param {object} ctx - The cloud function's context api.
- *
- * @returns {Promise} The resolver result.
- */
- export default async (event, ctx) => {
- /**
- * Data.users: The list of users to transfer the call to
- * data.phones: The list of phones to transfer the call
- * data.call: The id of the call beign transfered
- * data.callType: If the call is either inbound or outbound.
- */
- const { data } = event;
- let call = null;
- let agencySID;
- try {
- agencySID = await fetchAgencySID(ctx, {}, { checkPermissions: true });
- } catch (error) {
- return {
- statusCode: 400,
- body: {
- message: 'Unable to get Agency SID',
- },
- };
- }
- let twilioClient = await twilio(agencySID);
- try {
- call =
- data.callType === 'INBOUND'
- ? await fetchInboundCall(data.call, ctx)
- : await fetchPhoneCall(data.call, ctx);
- console.log('call: transfer-call Fetched call:', JSON.stringify(call));
- } catch (e) {
- console.log('call: transfer-call Error fetching call:', e.message);
- throw e;
- }
- const participants = [
- ...data.users.map((user) => ({ type: 'USER', user })),
- ...data.phones.map((phone) => ({ type: 'PHONE', phone })),
- ];
- try {
- await Promise.all(
- participants.map(async (part) => {
- const participant = await createParticipant(
- {
- ...(part.type === 'USER' ? { user: part.user } : { phone: part.phone }),
- ...(data.callType === 'INBOUND'
- ? {
- inboundCall: data.call,
- }
- : {
- outboundCall: data.call,
- }),
- isInitialParticipant: true,
- },
- ctx,
- { checkPermissions: false },
- );
- console.log('call: Created participant:', JSON.stringify(participant));
- const from =
- data.callType === 'INBOUND'
- ? `+${call.to.number.code}${call.to.number.number}`
- : `+${call.from.number.code}${call.from.number.number}`;
- const to =
- part.type === 'USER'
- ? `client:${part.user}?${qs.stringify({ id: participant.id })}`
- : `+${part.phone.code}${part.phone.number}`;
- console.log('call: transfer-call from', from);
- console.log('call: transfer-call to', to);
- await twilioClient.conferences(call.conferenceSid).participants.create({
- beep: true,
- earlyMedia: false,
- from: from,
- to: to,
- statusCallback: `${getBaseUrl(ctx)}/webhook/participant/${participant.id}/status`,
- statusCallbackMethod: 'POST',
- statusCallbackEvent: ['ringing', 'answered', 'completed'],
- });
- console.log('call: Added participant to conference');
- }),
- );
- } catch (e) {
- console.log('call: transfer-call Error creating participant:', e.message);
- throw e;
- }
- return {
- data: {
- success: true,
- },
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment