From e4097e34f17e99cb76cb67e65afd3b4650691813 Mon Sep 17 00:00:00 2001 From: Lucas Rosa Date: Fri, 7 Aug 2020 14:48:11 -0300 Subject: [PATCH] feat(resources): Messaging Profile Metrics and Short Codes (#43) * add Short Codes Resource * add Messaging Profile Metrics Resource * add Messaging Profile Metrics Methods --- lib/resources/MessagingProfileMetrics.js | 7 ++ lib/resources/MessagingProfiles.js | 8 ++ lib/resources/ShortCodes.js | 39 +++++++++ lib/telnyx.js | 2 + .../resources/MessagingProfileMetrics.spec.js | 28 +++++++ test/resources/MessagingProfiles.spec.js | 44 ++++++++++ test/resources/OtaUpdates.spec.js | 2 +- test/resources/ShortCodes.spec.js | 80 +++++++++++++++++++ 8 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 lib/resources/MessagingProfileMetrics.js create mode 100644 lib/resources/ShortCodes.js create mode 100644 test/resources/MessagingProfileMetrics.spec.js create mode 100644 test/resources/ShortCodes.spec.js diff --git a/lib/resources/MessagingProfileMetrics.js b/lib/resources/MessagingProfileMetrics.js new file mode 100644 index 0000000..c09127f --- /dev/null +++ b/lib/resources/MessagingProfileMetrics.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = require('../TelnyxResource').extend({ + path: 'messaging_profile_metrics', + includeBasic: ['list'], +}); + diff --git a/lib/resources/MessagingProfiles.js b/lib/resources/MessagingProfiles.js index 0950dbf..3b3aeff 100644 --- a/lib/resources/MessagingProfiles.js +++ b/lib/resources/MessagingProfiles.js @@ -8,6 +8,7 @@ var ACTIONS = [ 'phone_numbers', 'alphanumeric_sender_ids', 'short_codes', + 'metrics' ]; function getSpec(messagingProfileId) { @@ -98,5 +99,12 @@ module.exports = require('../TelnyxResource').extend({ path: '/{messagingProfileId}/alphanumeric_sender_ids', urlParams: ['messagingProfileId'], }), + + retrieveMetrics: telnyxMethod({ + method: 'GET', + path: '/{messagingProfileId}/metrics', + urlParams: ['messagingProfileId'], + methodType: 'retrieve', + }), }); diff --git a/lib/resources/ShortCodes.js b/lib/resources/ShortCodes.js new file mode 100644 index 0000000..dc1b916 --- /dev/null +++ b/lib/resources/ShortCodes.js @@ -0,0 +1,39 @@ +'use strict'; + +var TelnyxResource = require('../TelnyxResource'); +var utils = require('../utils'); +var telnyxMethod = TelnyxResource.method; + +function transformResponseData(response, telnyx) { + return utils.addResourceToResponseData( + response, + telnyx, + 'shortCodes', + { + update: telnyxMethod({ + method: 'PATCH', + path: '/{shortCodeId}', + urlParams: ['shortCodeId'], + paramsValues: [response.data.id], + paramsNames: ['id'], + }), + } + ); +} + +module.exports = TelnyxResource.extend({ + path: 'short_codes', + + list: telnyxMethod({ + method: 'GET', + methodType: 'list', + }), + + retrieve: telnyxMethod({ + method: 'GET', + path: '/{id}', + urlParams: ['id'], + + transformResponseData: transformResponseData, + }), +}); diff --git a/lib/telnyx.js b/lib/telnyx.js index b893e0c..86235e5 100644 --- a/lib/telnyx.js +++ b/lib/telnyx.js @@ -69,6 +69,8 @@ var resources = { Balance: require('./resources/Balance'), Addresses: require('./resources/Addresses'), Faxes: require('./resources/Faxes'), + ShortCodes: require('./resources/ShortCodes'), + MessagingProfileMetrics: require('./resources/MessagingProfileMetrics'), }; Telnyx.TelnyxResource = require('./TelnyxResource'); diff --git a/test/resources/MessagingProfileMetrics.spec.js b/test/resources/MessagingProfileMetrics.spec.js new file mode 100644 index 0000000..3e90cde --- /dev/null +++ b/test/resources/MessagingProfileMetrics.spec.js @@ -0,0 +1,28 @@ +'use strict'; + +var utils = require('../../testUtils'); +var telnyx = utils.getTelnyxMock(); +var expect = require('chai').expect; + +var TEST_AUTH_KEY = utils.getUserTelnyxKey(); + +describe('MessagingProfileMetrics Resource', function() { + describe('list', function() { + function responseFn(response) { + expect(response.data[0]).to.have.property('inbound'); + expect(response.data[0]).to.have.property('outbound'); + expect(response.data[0]).to.have.property('phone_numbers'); + expect(response.data[0]).to.have.property('messaging_profile_id'); + expect(response.data[0]).to.include({record_type: 'messaging_profile_metrics'}); + } + + it('Sends the correct request', function() { + return telnyx.messagingProfileMetrics.list() + .then(responseFn); + }); + it('Sends the correct request [with specified auth]', function() { + return telnyx.messagingProfileMetrics.list(TEST_AUTH_KEY) + .then(responseFn); + }); + }); +}); diff --git a/test/resources/MessagingProfiles.spec.js b/test/resources/MessagingProfiles.spec.js index 23a6c82..e4f98bd 100644 --- a/test/resources/MessagingProfiles.spec.js +++ b/test/resources/MessagingProfiles.spec.js @@ -170,5 +170,49 @@ describe('MessagingProfiles Resource', function() { }); }); }); + + describe('Metrics methods', function() { + function metricsNestedResponseFn(response) { + expect(response.data).to.have.property('detailed'); + expect(response.data).to.have.property('overview'); + expect(response.data.overview).to.have.property('inbound'); + expect(response.data.overview).to.have.property('outbound'); + expect(response.data.overview).to.have.property('phone_numbers'); + expect(response.data.overview).to.have.property('messaging_profile_id'); + expect(response.data.overview).to.include({record_type: 'messaging_profile_metrics'}); + expect(response.data.detailed[0]).to.have.property('metrics'); + } + + describe('retrieveMetrics', function() { + it('Sends the correct request', function() { + return telnyx.messagingProfiles.retrieveMetrics('123') + .then(metricsNestedResponseFn); + }); + + it('Sends the correct request [with specified auth]', function() { + return telnyx.messagingProfiles.retrieveMetrics('123', TEST_AUTH_KEY) + .then(metricsNestedResponseFn); + }); + }); + + describe('nested metrics', function() { + it('Sends the correct request', function() { + return telnyx.messagingProfiles.create({name: 'Summer Campaign'}) + .then(function(response) { + const mp = response.data; + return mp.metrics() + .then(metricsNestedResponseFn); + }) + }); + it('Sends the correct request [with specified auth]', function() { + return telnyx.messagingProfiles.retrieve('123') + .then(function(response) { + const mp = response.data; + return mp.metrics(TEST_AUTH_KEY) + .then(metricsNestedResponseFn); + }) + }); + }); + }) }) }); diff --git a/test/resources/OtaUpdates.spec.js b/test/resources/OtaUpdates.spec.js index 800842d..95a6e0d 100644 --- a/test/resources/OtaUpdates.spec.js +++ b/test/resources/OtaUpdates.spec.js @@ -7,7 +7,7 @@ var expect = require('chai').expect; var TEST_AUTH_KEY = utils.getUserTelnyxKey(); describe('OtaUpdates Resource', function() { - describe.skip('retrieve', function() { + describe('retrieve', function() { function responseFn(response) { expect(response.data).to.include.keys(['id', 'sim_card_id', 'status', 'settings']); expect(response.data).to.include({ diff --git a/test/resources/ShortCodes.spec.js b/test/resources/ShortCodes.spec.js new file mode 100644 index 0000000..e3ca560 --- /dev/null +++ b/test/resources/ShortCodes.spec.js @@ -0,0 +1,80 @@ +'use strict'; + +var utils = require('../../testUtils'); +var telnyx = utils.getTelnyxMock(); +var expect = require('chai').expect; + +var TEST_AUTH_KEY = utils.getUserTelnyxKey(); + +describe('ShortCodes Resource', function() { + describe('retrieve', function() { + function responseFn(response) { + expect(response.data).to.have.property('id'); + expect(response.data).to.have.property('short_code'); + expect(response.data).to.have.property('country_code'); + expect(response.data).to.have.property('messaging_profile_id'); + expect(response.data).to.include({record_type: 'short_code'}); + } + + it('Sends the correct request', function() { + return telnyx.shortCodes.retrieve('123').then(responseFn); + }) + + it('Sends the correct request [with specified auth]', function() { + return telnyx.shortCodes.retrieve('123', TEST_AUTH_KEY) + .then(responseFn); + }); + }); + + describe('list', function() { + function responseFn(response) { + expect(response.data[0]).to.have.property('id'); + expect(response.data[0]).to.have.property('short_code'); + expect(response.data[0]).to.have.property('country_code'); + expect(response.data[0]).to.have.property('messaging_profile_id'); + expect(response.data[0]).to.include({record_type: 'short_code'}); + + } + + it('Sends the correct request', function() { + return telnyx.shortCodes.list() + .then(responseFn); + }); + + it('Sends the correct request [with specified auth]', function() { + return telnyx.shortCodes.list(TEST_AUTH_KEY) + .then(responseFn); + }); + }); + + describe('Nested', function() { + function responseFn(response) { + if (response.data) { + expect(response.data).to.have.property('id'); + expect(response.data).to.have.property('short_code'); + expect(response.data).to.have.property('country_code'); + expect(response.data).to.have.property('messaging_profile_id'); + expect(response.data).to.include({record_type: 'short_code'}); + } + } + + describe('update', function() { + it('Sends the correct request', function() { + return telnyx.shortCodes.retrieve('123') + .then(function(response) { + const mp = response.data; + return mp.update({messaging_profile_id: 'uuid'}) + .then(responseFn); + }) + }); + it('Sends the correct request [with specified auth]', function() { + return telnyx.shortCodes.retrieve('123') + .then(function(response) { + const mp = response.data; + return mp.update({messaging_profile_id: 'uuid'}, TEST_AUTH_KEY) + .then(responseFn); + }) + }); + }); + }) +});