Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { fetchParticipantsToHangup } from '../phone-call-actions';
- import { twilio } from '../../../shared/twilio';
- import { fetchAgencySID } from '../../../shared/users/users-actions';
- /**
- * Task handler that fetches the available users
- * in an agency and calls them.
- *
- * @param {object} event - The cloud function's event.
- * @param {object} ctx - The cloud function's context api.
- *
- * @returns {Promise<void>} The task result.
- */
- export default async function(event, ctx) {
- let agencySID;
- try {
- agencySID = await fetchAgencySID(ctx, {}, { checkPermissions: true });
- } catch (error) {
- return {
- statusCode: 400,
- body: {
- message: error,
- },
- };
- }
- let twilioClient = await twilio(agencySID);
- const {
- callSid, // The sid of the call that started everything
- participantCallSid, // The call sid of the participant that answered the call
- } = event.data;
- let calls = [];
- try {
- calls = await fetchParticipantsToHangup(callSid, participantCallSid, ctx, {
- checkPermissions: false,
- });
- console.log('call: hang-up-initial-calls hung up on participants:', JSON.stringify(calls));
- } catch (e) {
- console.log('call: hang-up-initial-calls Could not fetch participants of the call:', e.message);
- return event;
- }
- try {
- await Promise.all(
- calls.map((call) => {
- return twilioClient.calls(call.callSid).update({ status: 'canceled' });
- }),
- console.log('call: hang-up-initial-calls calls promise.all:', JSON.stringify(calls)),
- );
- } catch (e) {
- console.log('call: hang-up-initial-calls Could not cancel calls:', e.message);
- return event;
- }
- return event;
- }
Advertisement
Add Comment
Please, Sign In to add comment