Skip to content

Commit

Permalink
feat(resources): Messaging Profile Metrics and Short Codes (#43)
Browse files Browse the repository at this point in the history
* add Short Codes Resource

* add Messaging Profile Metrics Resource

* add Messaging Profile Metrics Methods
  • Loading branch information
lucasassisrosa authored Aug 7, 2020
1 parent 5e686a9 commit e4097e3
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/resources/MessagingProfileMetrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = require('../TelnyxResource').extend({
path: 'messaging_profile_metrics',
includeBasic: ['list'],
});

8 changes: 8 additions & 0 deletions lib/resources/MessagingProfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ACTIONS = [
'phone_numbers',
'alphanumeric_sender_ids',
'short_codes',
'metrics'
];

function getSpec(messagingProfileId) {
Expand Down Expand Up @@ -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',
}),
});

39 changes: 39 additions & 0 deletions lib/resources/ShortCodes.js
Original file line number Diff line number Diff line change
@@ -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,
}),
});
2 changes: 2 additions & 0 deletions lib/telnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
28 changes: 28 additions & 0 deletions test/resources/MessagingProfileMetrics.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
44 changes: 44 additions & 0 deletions test/resources/MessagingProfiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
});
})
})
});
2 changes: 1 addition & 1 deletion test/resources/OtaUpdates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
80 changes: 80 additions & 0 deletions test/resources/ShortCodes.spec.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
});
})
});

0 comments on commit e4097e3

Please sign in to comment.