eliecerthoms1

(task) hangup-initial-calls.js

Oct 20th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { fetchParticipantsToHangup } from '../phone-call-actions';
  2. import { twilio } from '../../../shared/twilio';
  3. import { fetchAgencySID } from '../../../shared/users/users-actions';
  4.  
  5. /**
  6.  * Task handler that fetches the available users
  7.  * in an agency and calls them.
  8.  *
  9.  * @param {object} event - The cloud function's event.
  10.  * @param {object} ctx - The cloud function's context api.
  11.  *
  12.  * @returns {Promise<void>} The task result.
  13.  */
  14. export default async function(event, ctx) {
  15.   let agencySID;
  16.  
  17.   try {
  18.     agencySID = await fetchAgencySID(ctx, {}, { checkPermissions: true });
  19.   } catch (error) {
  20.     return {
  21.       statusCode: 400,
  22.       body: {
  23.         message: error,
  24.       },
  25.     };
  26.   }
  27.  
  28.   let twilioClient = await twilio(agencySID);
  29.   const {
  30.     callSid, // The sid of the call that started everything
  31.     participantCallSid, // The call sid of the participant that answered the call
  32.   } = event.data;
  33.  
  34.   let calls = [];
  35.  
  36.   try {
  37.     calls = await fetchParticipantsToHangup(callSid, participantCallSid, ctx, {
  38.       checkPermissions: false,
  39.     });
  40.     console.log('call: hang-up-initial-calls hung up on participants:', JSON.stringify(calls));
  41.   } catch (e) {
  42.     console.log('call: hang-up-initial-calls Could not fetch participants of the call:', e.message);
  43.  
  44.     return event;
  45.   }
  46.  
  47.   try {
  48.     await Promise.all(
  49.       calls.map((call) => {
  50.         return twilioClient.calls(call.callSid).update({ status: 'canceled' });
  51.       }),
  52.       console.log('call: hang-up-initial-calls calls promise.all:', JSON.stringify(calls)),
  53.     );
  54.   } catch (e) {
  55.     console.log('call: hang-up-initial-calls Could not cancel calls:', e.message);
  56.  
  57.     return event;
  58.   }
  59.  
  60.   return event;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment