From b0575b271f4a229adf90a7c7718b01a88021ae10 Mon Sep 17 00:00:00 2001 From: Kelley R Date: Thu, 12 Dec 2024 16:21:08 -0500 Subject: [PATCH] Use Lookup V2, improve code commenting --- .../functions/forward-call.protected.js | 22 +++++++++--- .../tests/forward-call.test.js | 36 +++++++++---------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/caller-id-forwarding/functions/forward-call.protected.js b/caller-id-forwarding/functions/forward-call.protected.js index a6afb74e7..4ee7237a5 100644 --- a/caller-id-forwarding/functions/forward-call.protected.js +++ b/caller-id-forwarding/functions/forward-call.protected.js @@ -1,18 +1,30 @@ +/** + * Handles incoming calls, performs a lookup for caller information, sends a message with the caller details, and forwards the call to a specified number. + * + * @param {Object} context - The context object containing environment variables and Twilio client. + * @param {Object} event - The event object containing details of the incoming call. + * @param {Function} callback - The callback function to return the TwiML response. + */ exports.handler = async function (context, event, callback) { try { const client = context.getTwilioClient(); - const lookup = await client.lookups.v1 + /* + * Lookup the incoming phone number + * Request both the "caller_name" and "line_type_intelligence" data packages + */ + const lookup = await client.lookups.v2 .phoneNumbers(event.From) - .fetch({ type: ['carrier', 'caller-name'] }); + .fetch({ fields: 'caller_name,line_type_intelligence' }); const callerName = lookup.callerName.caller_name; - const carrierName = lookup.carrier.name; + const carrierName = lookup.lineTypeIntelligence.carrier_name; const body = `Incoming call from ${event.From} Name: ${callerName === null ? 'Unknown' : callerName} -Carrier: ${carrierName} (${lookup.carrier.type})`; +Carrier: ${carrierName} (${lookup.lineTypeIntelligence.type})`; + // Send a message to your phone number with the caller details await client.messages.create({ body, from: context.TWILIO_PHONE_NUMBER, @@ -21,7 +33,7 @@ Carrier: ${carrierName} (${lookup.carrier.type})`; } catch (error) { console.error(error); } finally { - // forward the call even if caller ID fails + // Forward the call even if caller ID fails const twiml = new Twilio.twiml.VoiceResponse(); twiml.dial(context.MY_PHONE_NUMBER); return callback(null, twiml); diff --git a/caller-id-forwarding/tests/forward-call.test.js b/caller-id-forwarding/tests/forward-call.test.js index c141ec235..d21cb770e 100644 --- a/caller-id-forwarding/tests/forward-call.test.js +++ b/caller-id-forwarding/tests/forward-call.test.js @@ -1,20 +1,20 @@ +/* eslint-disable camelcase */ const helpers = require('../../test/test-helper'); const forwardCall = require('../functions/forward-call.protected').handler; const mockFetch = jest.fn().mockResolvedValue({ - carrier: { - name: 'Verizon', + lineTypeIntelligence: { + carrier_name: 'Verizon', type: 'mobile', }, callerName: { - // eslint-disable-next-line camelcase caller_name: 'Lottie Matthews', }, }); const mockClient = { lookups: { - v1: { + v2: { phoneNumbers: jest.fn(() => ({ fetch: mockFetch, })), @@ -56,11 +56,11 @@ test('forwards the call to the number from the context', (done) => { test('looks up the incoming phone number', (done) => { const expectedParams = { - type: ['carrier', 'caller-name'], + fields: 'caller_name,line_type_intelligence', }; const callback = (_err, result) => { expect(result).toBeDefined(); - expect(mockClient.lookups.v1.phoneNumbers).toHaveBeenCalledWith(event.From); + expect(mockClient.lookups.v2.phoneNumbers).toHaveBeenCalledWith(event.From); expect(mockFetch).toHaveBeenCalledWith(expectedParams); done(); }; @@ -69,28 +69,28 @@ test('looks up the incoming phone number', (done) => { }); test('sets the callername to Unknown if not returned', (done) => { - const callback = (_err, result) => { - expect(result).toBeDefined(); - const expectedParams = { - body: `Incoming call from 54321 + const expectedParams = { + body: `Incoming call from 54321 Name: Unknown Carrier: Verizon (mobile)`, - to: context.MY_PHONE_NUMBER, - from: context.TWILIO_PHONE_NUMBER, - }; + to: context.MY_PHONE_NUMBER, + from: context.TWILIO_PHONE_NUMBER, + }; + + const callback = (_err, result) => { + expect(result).toBeDefined(); expect(mockClient.messages.create).toHaveBeenCalledWith(expectedParams); done(); }; mockFetch.mockResolvedValueOnce({ + lineTypeIntelligence: { + carrier_name: 'Verizon', + type: 'mobile', + }, callerName: { - // eslint-disable-next-line camelcase caller_name: null, }, - carrier: { - name: 'Verizon', - type: 'mobile', - }, }); forwardCall(context, event, callback);