eliecerthoms1

(resolver) transfer-call.js

Oct 20th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createParticipant, fetchInboundCall, fetchPhoneCall } from '../phone-call-actions';
  2. import { getBaseUrl } from '../../../shared/utils';
  3. import { twilio } from '../../../shared/twilio';
  4. import { fetchAgencySID } from '../../../shared/users/users-actions';
  5. import * as qs from 'querystring';
  6.  
  7. /**
  8.  * Transfer call resolver.
  9.  *
  10.  * @param {object} event - The cloud function's event object.
  11.  * @param {object} ctx - The cloud function's context api.
  12.  *
  13.  * @returns {Promise} The resolver result.
  14.  */
  15. export default async (event, ctx) => {
  16.   /**
  17.    * Data.users: The list of users to transfer the call to
  18.    * data.phones: The list of phones to transfer the call
  19.    * data.call: The id of the call beign transfered
  20.    * data.callType: If the call is either inbound or outbound.
  21.    */
  22.   const { data } = event;
  23.   let call = null;
  24.  
  25.   let agencySID;
  26.  
  27.   try {
  28.     agencySID = await fetchAgencySID(ctx, {}, { checkPermissions: true });
  29.   } catch (error) {
  30.     return {
  31.       statusCode: 400,
  32.       body: {
  33.         message: 'Unable to get Agency SID',
  34.       },
  35.     };
  36.   }
  37.  
  38.   let twilioClient = await twilio(agencySID);
  39.  
  40.   try {
  41.     call =
  42.       data.callType === 'INBOUND'
  43.         ? await fetchInboundCall(data.call, ctx)
  44.         : await fetchPhoneCall(data.call, ctx);
  45.     console.log('call: transfer-call Fetched call:', JSON.stringify(call));
  46.   } catch (e) {
  47.     console.log('call: transfer-call Error fetching call:', e.message);
  48.  
  49.     throw e;
  50.   }
  51.  
  52.   const participants = [
  53.     ...data.users.map((user) => ({ type: 'USER', user })),
  54.     ...data.phones.map((phone) => ({ type: 'PHONE', phone })),
  55.   ];
  56.  
  57.   try {
  58.     await Promise.all(
  59.       participants.map(async (part) => {
  60.         const participant = await createParticipant(
  61.           {
  62.             ...(part.type === 'USER' ? { user: part.user } : { phone: part.phone }),
  63.             ...(data.callType === 'INBOUND'
  64.               ? {
  65.                 inboundCall: data.call,
  66.               }
  67.               : {
  68.                 outboundCall: data.call,
  69.               }),
  70.             isInitialParticipant: true,
  71.           },
  72.           ctx,
  73.           { checkPermissions: false },
  74.         );
  75.         console.log('call: Created participant:', JSON.stringify(participant));
  76.  
  77.         const from =
  78.           data.callType === 'INBOUND'
  79.             ? `+${call.to.number.code}${call.to.number.number}`
  80.             : `+${call.from.number.code}${call.from.number.number}`;
  81.  
  82.         const to =
  83.           part.type === 'USER'
  84.             ? `client:${part.user}?${qs.stringify({ id: participant.id })}`
  85.             : `+${part.phone.code}${part.phone.number}`;
  86.  
  87.         console.log('call: transfer-call from', from);
  88.         console.log('call: transfer-call to', to);
  89.  
  90.         await twilioClient.conferences(call.conferenceSid).participants.create({
  91.           beep: true,
  92.           earlyMedia: false,
  93.           from: from,
  94.           to: to,
  95.           statusCallback: `${getBaseUrl(ctx)}/webhook/participant/${participant.id}/status`,
  96.           statusCallbackMethod: 'POST',
  97.           statusCallbackEvent: ['ringing', 'answered', 'completed'],
  98.         });
  99.         console.log('call: Added participant to conference');
  100.       }),
  101.     );
  102.   } catch (e) {
  103.     console.log('call: transfer-call Error creating participant:', e.message);
  104.     throw e;
  105.   }
  106.  
  107.   return {
  108.     data: {
  109.       success: true,
  110.     },
  111.   };
  112. };
  113.  
Advertisement
Add Comment
Please, Sign In to add comment