From 601daf48886625cd6cbd3c371893989335183ddc Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Fri, 13 Sep 2019 23:47:26 +0530 Subject: [PATCH 01/11] powerpack api change --- lib/resources/powerpacks.js | 409 ++++++++++++++++++++++++++++++++++++ lib/rest/client.js | 4 +- test/powerpacks.js | 21 ++ 3 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 lib/resources/powerpacks.js create mode 100644 test/powerpacks.js diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js new file mode 100644 index 00000000..e5bbe885 --- /dev/null +++ b/lib/resources/powerpacks.js @@ -0,0 +1,409 @@ + +import { extend, validate } from '../utils/common.js'; +import { PlivoResource, PlivoResourceInterface } from '../base'; +import * as _ from "lodash"; + +const action = 'Powerpack/'; +const idField = 'uuid'; + +/** + * Represents a Powerpack + * @constructor + * @param {function} client - make api call + * @param {object} [data] - data of call + */ +export class Powerpack extends PlivoResource { + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + + if (idField in data) { + this.uuid = data[idField]; + } + + extend(this, data); + } + +/** + * update powerpack + * @method + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ +update(params) { + return super.update(params); +} + +/** + * delete powerpack + * @method + * @param {boolean} unrent_numbers - + * @promise response + * @fail {Error} return Error + */ +delete( unrent_numbers=false) { + let params = {}; + + if (typeof unrent_numbers === 'boolean') { + params.unrent_numbers = unrent_numbers.toString(); + } + + return super.delete(params); +} + + +} +export class NumberpoolResource extends PlivoResource { + constructor(client, data = {}) { + super(action, NumberpoolResource, idField, client); + + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; + } +} + +export class ShortcodeResource extends PlivoResource { + constructor(client, data = {}) { + super(action, ShortcodeResource, idField, client); + + if (idField in data) { + this.id = data[idField]; + } + + extend(this, data); + this[clientKey] = client; + } +} + +class ShortcodeInterface extends PlivoResourceInterface { + constructor(client, data = {}) { + super(action, ShortcodeResource, idField, client); + extend(this, data); + + this[clientKey] = client; + } + listshortcode(number_pool_uuid, params) { + let client = this[clientKey]; + if (params === undefined){ + params = {} + } + + return new Promise((resolve, reject) => { + client('GET', 'NumberPool/'+number_pool_uuid+'/Shortcode/', params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { value: response.body.meta }); + response.body.objects.forEach(item => { + objects.push(new Klass(client, item)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); + } + + findshortcode(number_pool_uuid, shortcode){ + let client = this[clientKey]; + try { + return new Promise((resolve, reject) => { + client('GET', 'NumberPool/' + number_pool_uuid + '/Shortcode/' + shortcode + '/', params) + .then(response => { + resolve(new Klass(client, response.body)); + }); + }); + } + catch (error) { + reject(error); + } + } +} + +class NumberpoolInterface extends PlivoResourceInterface { + constructor(client, data = {}) { + super(action, NumberpoolResource, idField, client); + extend(this, data); + + this[clientKey] = client; + } + + listnumbers(number_pool_uuid, params) { + let client = this[clientKey]; + if (params === undefined){ + params = {} + } + + return new Promise((resolve, reject) => { + client('GET', 'NumberPool/'+number_pool_uuid+'/Number/', params) + .then(response => { + let objects = []; + Object.defineProperty(objects, 'meta', { value: response.body.meta }); + response.body.objects.forEach(item => { + objects.push(new Klass(client, item)); + }); + resolve(objects); + }) + .catch(error => { + reject(error); + }); + }); + } + + findnumbers(number_pool_uuid, number){ + params = {} + return new Promise((resolve, reject) => { + client('GET', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) + .then(response => { + resolve(new Klass(client, response.body)); + }) + .catch(error => { + reject(error); + }); + }); + } + + addnumbers(number_pool_uuid, number){ + params = {} + return new Promise((resolve, reject) => { + client('POST', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) + .then(response => { + resolve(new Klass(client, response.body)); + }) + }) + .catch(error => { + reject(error); + + }); + } + removeumbers(number_pool_uuid, number, params){ + return new Promise((resolve, reject) => { + client('DELETE', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) + .then(response => { + resolve(new Klass(client, response.body)); + }) + .catch(error => { + reject(error); + }); + }); + } + + countnumbers(number_pool_uuid, params) { + let client = this[clientKey]; + if (params === undefined){ + params = {} + } + + return new Promise((resolve, reject) => { + client('GET', 'NumberPool/'+number_pool_uuid+'/Number/', params) + .then(response => { + let count = 0; + count= response.body.meta.total_count + resolve(count); + }) + .catch(error => { + reject(error); + }); + }); + } + + buyandadd(number_pool_uuid, params){ + number = params.number + if (params.number == undefined){ + number = new Promise((resolve, reject) => { + client('GET', 'PhoneNumber/', params) + .then(response => { + phonenumber = response.body.objects[0].number + resolve(phonenumber); + }) + .catch(error => { + reject(error); + }); + }); + } + return new Promise((resolve, reject) => { + client('POST', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) + .then(response => { + resolve(new Klass(client, response.body)); + }) + }) + .catch(error => { + reject(error); + + }); + + } + + + + +} +/** + * Represents a Powerpack interface + * @constructor + * @param {function} client - make api call + * @param {object} [data] - data of call + */ +export class PowerpackInterface extends PlivoResourceInterface { + + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + extend(this, data); + + this[clientKey] = client; + this[NumberpoolInterfaceKey] = new NumberpoolInterface(client); + this[ShortcodeInterfaceKey] = new ShortcodeInterface(client); + } + +/** + * get Powerpack by given id + * @method + * @param {string} uuid - id of Powerpack + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + get(uuid) { + return super.get(uuid); + } + +/** + * create Powerpack + * @method + * @param {string} name - name of Powerpack + * @param {object} params - params to create Powerpack + * @param {string} [params.sticky_sender] - + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(name, params = {}) { + + let errors = validate([ + {field: 'name', value: name, validators: ['isRequired', 'isString']} + ]); + + if (errors) { + return errors; + } + + params.name = name; + + return super.create(params); + } + +/** + * update Powerpack + * @method + * @param {string} uuid - id of Powerpack + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(uuid, params) { + let errors = validate([ + {field: 'uuid', value: uuid, validators: ['isRequired']} + ]); + + if (errors) { + return errors; + } + return new Powerpack(this[clientKey], { + uuid: uuid + }).update(params); + } + +/** + * delete Powerpack + * @method + * @param {string} id - id of Powerpack + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(uuid, unrent_numbers=false) { + return new Powerpack(this[clientKey], { + uuid: uuid + }).delete(unrent_numbers); + } + + /** + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ +list(params) { + return super.list(params); +} +get_numberpooluuid(uuid){ + numberpool_path = new Promise((resolve, reject) => { + client('GET', 'Powerpack/', params) + .then(response => { + phonenumber = response.body.number_pool + resolve(phonenumber); + }) + .catch(error => { + reject(error); + }); + }); + numberpool = numberpool_path.split('/') + return numberpool[5] +} + +listnumbers(uuid, params) { + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].listnumbers(number_pool_uuid, params); +} +findnumbers(uuid, number){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].findnumbers(number_pool_uuid, number); + +} + +addnumbers(uuid, number){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].addnumbers(number_pool_uuid, number); +} +removeumbers(uuid, number, params){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].removeumbers(number_pool_uuid, number, params); +} + +countnumbers(uuid, params){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].countnumbers(number_pool_uuid, params); +} + +buyandadd(uuid, param){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[NumberpoolInterfaceKey].buyandadd(number_pool_uuid, params); +} + +listshortcode(uuid){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[ShortcodeInterface].listshortcode(number_pool_uuid); +} + +findshortcode(uuid,shortcode){ + number_pool_uuid = get_numberpooluuid(uuid) + return this[ShortcodeInterface].findshortcode(number_pool_uuid, shortcode); +} + +} diff --git a/lib/rest/client.js b/lib/rest/client.js index 870cbc97..4817c369 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -8,6 +8,7 @@ import { ApplicationInterface } from "../resources/applications"; import { ConferenceInterface } from "../resources/conferences"; import { EndpointInterface } from "../resources/endpoints"; import { MessageInterface } from "../resources/messages"; +import { PowerpackInterface } from "../resources/powerpacks"; import { NumberInterface } from "../resources/numbers"; import { PricingInterface } from "../resources/pricings"; import { RecordingInterface } from "../resources/recordings"; @@ -60,6 +61,7 @@ export class Client { this.conferences = new ConferenceInterface(client); this.endpoints = new EndpointInterface(client); this.messages = new MessageInterface(client); + this,powerpacks = new PowerpackInterface(client); this.numbers = new NumberInterface(client); this.pricings = new PricingInterface(client); this.recordings = new RecordingInterface(client); @@ -121,4 +123,4 @@ export class PhloClient { } } -} \ No newline at end of file +} diff --git a/test/powerpacks.js b/test/powerpacks.js new file mode 100644 index 00000000..af3c7daa --- /dev/null +++ b/test/powerpacks.js @@ -0,0 +1,21 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import {Client} from '../lib/rest/client-test'; +import {PlivoGenericResponse} from '../lib/base.js'; + +let client = new Client('sampleid', 'sammpletoken', 'sampleproxy'); + +describe('PowerpackInterface', function () { + it('Get Details of a Powerpack', function () { + return client.powerpacks.get('5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') + .then(function(powerpack) { + assert.equal(powerpack.uuid, '5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') + }) + }); + it('should create powerpack via interface', function () { + return client.powerpacks.create(name='hello') + .then(function(powerpack){ + assert.equal(powerpack.name, 'hello') + }) + }); +}); From 1c18f011a886bf4bffc8bbc47792d20986d67c2a Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Thu, 14 Nov 2019 01:13:43 +0530 Subject: [PATCH 02/11] powerpack v2 change --- lib/resources/powerpacks.js | 519 ++++++++++++++---------------------- test/powerpacks.js | 82 +++++- 2 files changed, 282 insertions(+), 319 deletions(-) diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js index e5bbe885..7f7983f2 100644 --- a/lib/resources/powerpacks.js +++ b/lib/resources/powerpacks.js @@ -1,10 +1,17 @@ - -import { extend, validate } from '../utils/common.js'; -import { PlivoResource, PlivoResourceInterface } from '../base'; +import { + extend, + validate +} from '../utils/common.js'; +import { + PlivoResource, + PlivoResourceInterface +} from '../base'; import * as _ from "lodash"; const action = 'Powerpack/'; const idField = 'uuid'; +const numberpoolIdField = 'numberPool'; +const clientKey = Symbol(); /** * Represents a Powerpack @@ -13,238 +20,191 @@ const idField = 'uuid'; * @param {object} [data] - data of call */ export class Powerpack extends PlivoResource { + constructor(client, data = {}) { super(action, Powerpack, idField, client); if (idField in data) { this.uuid = data[idField]; } - + if (numberpoolIdField in data) { + this.number_pool_id = data[numberpoolIdField].split('/')[5]; + } + this.number_pool = new NumberPool(client, { + number_pool_id: this.number_pool_id + }); extend(this, data); } -/** - * update powerpack - * @method - * @param {object} params - to update Powerpack - * @param {string} [params.name] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @param {string} [params.sticky_sender] - * @param {string} [params.local_connect] - - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ -update(params) { - return super.update(params); -} - -/** - * delete powerpack - * @method - * @param {boolean} unrent_numbers - - * @promise response - * @fail {Error} return Error - */ -delete( unrent_numbers=false) { - let params = {}; - - if (typeof unrent_numbers === 'boolean') { - params.unrent_numbers = unrent_numbers.toString(); + list_numbers(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/'; + return super.customexecuteAction(path.toString().trim(), "GET", params); + } + count_numbers(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/'; + return super.getMetaResponse(path.toString().trim(), "GET", params); } - return super.delete(params); -} - + find_number(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "GET"); + } + add_number(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "POST"); + } + remove_number(number, unrent = false) { + var params; + params['unrent'] = unrent; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "DELETE", params); + } + list_shortcodes(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), "GET", params); + } + find_shortcode(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), "GET"); + } + buy_add_number(params) { + var number = params.number + if (params.number == undefined) { + try { + var response = super.customexecuteAction('PhoneNumber/', "GET", params); + number = response.body.objects[0].number + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "POST"); + } -} -export class NumberpoolResource extends PlivoResource { - constructor(client, data = {}) { - super(action, NumberpoolResource, idField, client); + /** + * update powerpack + * @method + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(params) { + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), "POST", params); + } - if (idField in data) { - this.id = data[idField]; + /** + * delete Powerpack + * @method + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(unrent_numbers = false) { + let params = {}; + if (typeof unrent_numbers === 'boolean') { + params.unrent_numbers = unrent_numbers.toString(); } - - extend(this, data); - this[clientKey] = client; + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), "DELETE", params); } } - -export class ShortcodeResource extends PlivoResource { +const numberPoolField = "number_pool_id" +export class NumberPool extends PlivoResource { constructor(client, data = {}) { - super(action, ShortcodeResource, idField, client); - - if (idField in data) { - this.id = data[idField]; - } + super(action, NumberPool, numberPoolField, client); + this.numbers = new Numbers(client, { + number_pool_id: data.number_pool_id + }); + this.shortcodes = new Shortcode(client, { + number_pool_id: data.number_pool_id + }); extend(this, data); - this[clientKey] = client; } } -class ShortcodeInterface extends PlivoResourceInterface { +export class Numbers extends PlivoResource { constructor(client, data = {}) { - super(action, ShortcodeResource, idField, client); + super(action, Numbers, numberPoolField, client); extend(this, data); - - this[clientKey] = client; } - listshortcode(number_pool_uuid, params) { - let client = this[clientKey]; - if (params === undefined){ - params = {} + buy_add_number(params) { + var number = params.number + if (params.number == undefined) { + try { + var response = super.customexecuteAction('PhoneNumber/', "GET", params); + number = response.body.objects[0].number + } catch (err) { + return err.message; + } } - - return new Promise((resolve, reject) => { - client('GET', 'NumberPool/'+number_pool_uuid+'/Shortcode/', params) - .then(response => { - let objects = []; - Object.defineProperty(objects, 'meta', { value: response.body.meta }); - response.body.objects.forEach(item => { - objects.push(new Klass(client, item)); - }); - resolve(objects); - }) - .catch(error => { - reject(error); - }); - }); + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "POST"); } - findshortcode(number_pool_uuid, shortcode){ - let client = this[clientKey]; - try { - return new Promise((resolve, reject) => { - client('GET', 'NumberPool/' + number_pool_uuid + '/Shortcode/' + shortcode + '/', params) - .then(response => { - resolve(new Klass(client, response.body)); - }); - }); - } - catch (error) { - reject(error); + list(params) { + if (params === undefined) { + params = {}; } + let path = 'NumberPool/' + this.number_pool_id + '/Number/'; + return super.customexecuteAction(path.toString().trim(), "GET", params); } -} - -class NumberpoolInterface extends PlivoResourceInterface { - constructor(client, data = {}) { - super(action, NumberpoolResource, idField, client); - extend(this, data); - - this[clientKey] = client; - } - - listnumbers(number_pool_uuid, params) { - let client = this[clientKey]; - if (params === undefined){ - params = {} + count(params) { + if (params === undefined) { + params = {}; } - - return new Promise((resolve, reject) => { - client('GET', 'NumberPool/'+number_pool_uuid+'/Number/', params) - .then(response => { - let objects = []; - Object.defineProperty(objects, 'meta', { value: response.body.meta }); - response.body.objects.forEach(item => { - objects.push(new Klass(client, item)); - }); - resolve(objects); - }) - .catch(error => { - reject(error); - }); - }); + let path = 'NumberPool/' + this.number_pool_id + '/Number/'; + return super.getMetaResponse(path.toString().trim(), "GET", params); } - findnumbers(number_pool_uuid, number){ - params = {} - return new Promise((resolve, reject) => { - client('GET', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) - .then(response => { - resolve(new Klass(client, response.body)); - }) - .catch(error => { - reject(error); - }); - }); + find(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "GET"); } - - addnumbers(number_pool_uuid, number){ - params = {} - return new Promise((resolve, reject) => { - client('POST', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) - .then(response => { - resolve(new Klass(client, response.body)); - }) - }) - .catch(error => { - reject(error); - - }); + add(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "POST"); } - removeumbers(number_pool_uuid, number, params){ - return new Promise((resolve, reject) => { - client('DELETE', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) - .then(response => { - resolve(new Klass(client, response.body)); - }) - .catch(error => { - reject(error); - }); - }); + remove(number, unrent = false) { + var params; + params['unrent'] = unrent; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), "DELETE", params); } - countnumbers(number_pool_uuid, params) { - let client = this[clientKey]; - if (params === undefined){ - params = {} - } - - return new Promise((resolve, reject) => { - client('GET', 'NumberPool/'+number_pool_uuid+'/Number/', params) - .then(response => { - let count = 0; - count= response.body.meta.total_count - resolve(count); - }) - .catch(error => { - reject(error); - }); - }); +} +export class Shortcode extends PlivoResource { + constructor(client, data = {}) { + super(action, Shortcode, numberPoolField, client); + extend(this, data); + this.number_pool_id = data.number_pool_id; } - - buyandadd(number_pool_uuid, params){ - number = params.number - if (params.number == undefined){ - number = new Promise((resolve, reject) => { - client('GET', 'PhoneNumber/', params) - .then(response => { - phonenumber = response.body.objects[0].number - resolve(phonenumber); - }) - .catch(error => { - reject(error); - }); - }); + list(params) { + if (params === undefined) { + params = {}; } - return new Promise((resolve, reject) => { - client('POST', 'NumberPool/'+number_pool_uuid+'/Number/'+number+'/', params) - .then(response => { - resolve(new Klass(client, response.body)); - }) - }) - .catch(error => { - reject(error); - - }); - + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), "GET", params); + } + find(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), "GET"); } - - } @@ -259,40 +219,38 @@ export class PowerpackInterface extends PlivoResourceInterface { constructor(client, data = {}) { super(action, Powerpack, idField, client); extend(this, data); - - this[clientKey] = client; - this[NumberpoolInterfaceKey] = new NumberpoolInterface(client); - this[ShortcodeInterfaceKey] = new ShortcodeInterface(client); } -/** - * get Powerpack by given id - * @method - * @param {string} uuid - id of Powerpack - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ + /** + * get Powerpack by given id + * @method + * @param {string} uuid - id of Powerpack + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ get(uuid) { return super.get(uuid); } -/** - * create Powerpack - * @method - * @param {string} name - name of Powerpack - * @param {object} params - params to create Powerpack - * @param {string} [params.sticky_sender] - - * @param {string} [params.local_connect] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @promise {object} return {@link PlivoGenericResponse} object - * @fail {Error} return Error - */ + /** + * create Powerpack + * @method + * @param {string} name - name of Powerpack + * @param {object} params - params to create Powerpack + * @param {string} [params.sticky_sender] - + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ create(name, params = {}) { - let errors = validate([ - {field: 'name', value: name, validators: ['isRequired', 'isString']} - ]); + let errors = validate([{ + field: 'name', + value: name, + validators: ['isRequired', 'isString'] + }]); if (errors) { return errors; @@ -303,23 +261,25 @@ export class PowerpackInterface extends PlivoResourceInterface { return super.create(params); } -/** - * update Powerpack - * @method - * @param {string} uuid - id of Powerpack - * @param {object} params - to update Powerpack - * @param {string} [params.name] - * @param {string} [params.sticky_sender] - * @param {string} [params.local_connect] - * @param {string} [params.application_type] - * @param {string} [params.application_id] - * @promise {object} return {@link Powerpack} object - * @fail {Error} return Error - */ + /** + * update Powerpack + * @method + * @param {string} uuid - id of Powerpack + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ update(uuid, params) { - let errors = validate([ - {field: 'uuid', value: uuid, validators: ['isRequired']} - ]); + let errors = validate([{ + field: 'uuid', + value: uuid, + validators: ['isRequired'] + }]); if (errors) { return errors; @@ -329,81 +289,14 @@ export class PowerpackInterface extends PlivoResourceInterface { }).update(params); } -/** - * delete Powerpack - * @method - * @param {string} id - id of Powerpack - * @promise {object} return true on success - * @fail {Error} return Error - */ - delete(uuid, unrent_numbers=false) { - return new Powerpack(this[clientKey], { - uuid: uuid - }).delete(unrent_numbers); - } - /** - * Get All Call Detail - * @method - * @param {object} params - params to get all call details. - * @promise {object[]} returns list of Call Object - * @fail {Error} returns Error - */ -list(params) { - return super.list(params); -} -get_numberpooluuid(uuid){ - numberpool_path = new Promise((resolve, reject) => { - client('GET', 'Powerpack/', params) - .then(response => { - phonenumber = response.body.number_pool - resolve(phonenumber); - }) - .catch(error => { - reject(error); - }); - }); - numberpool = numberpool_path.split('/') - return numberpool[5] -} - -listnumbers(uuid, params) { - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].listnumbers(number_pool_uuid, params); -} -findnumbers(uuid, number){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].findnumbers(number_pool_uuid, number); - -} - -addnumbers(uuid, number){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].addnumbers(number_pool_uuid, number); -} -removeumbers(uuid, number, params){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].removeumbers(number_pool_uuid, number, params); -} - -countnumbers(uuid, params){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].countnumbers(number_pool_uuid, params); -} - -buyandadd(uuid, param){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[NumberpoolInterfaceKey].buyandadd(number_pool_uuid, params); -} - -listshortcode(uuid){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[ShortcodeInterface].listshortcode(number_pool_uuid); -} - -findshortcode(uuid,shortcode){ - number_pool_uuid = get_numberpooluuid(uuid) - return this[ShortcodeInterface].findshortcode(number_pool_uuid, shortcode); -} - + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ + list(params) { + return super.list(params); + } } diff --git a/test/powerpacks.js b/test/powerpacks.js index af3c7daa..99566464 100644 --- a/test/powerpacks.js +++ b/test/powerpacks.js @@ -1,21 +1,91 @@ import assert from 'assert'; import sinon from 'sinon'; -import {Client} from '../lib/rest/client-test'; -import {PlivoGenericResponse} from '../lib/base.js'; +import { + Client +} from '../lib/rest/client-test'; +import { + PlivoGenericResponse +} from '../lib/base.js'; let client = new Client('sampleid', 'sammpletoken', 'sampleproxy'); describe('PowerpackInterface', function () { it('Get Details of a Powerpack', function () { return client.powerpacks.get('5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') - .then(function(powerpack) { + .then(function (powerpack) { assert.equal(powerpack.uuid, '5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') }) }); it('should create powerpack via interface', function () { - return client.powerpacks.create(name='hello') - .then(function(powerpack){ - assert.equal(powerpack.name, 'hello') + return client.powerpacks.create("node sdk test") + .then(function (powerpack) { + assert.equal(powerpack.name, 'node sdk test') }) }); + + it('list powerpacks numbers via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.list_numbers() + }) + .then(function (result) { + assert.notEqual(result.length, 0) + }) + }); + + it('delete powerpacks via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.delete() + }) + .then(function (result) { + assert.notEqual(result.response, "success") + }) + }); + it('list powerpacks numbers via interface', function () { + return client.powerpacks.list() + .then(function (powerpack) { + assert.notEqual(powerpack.length, 0) + }) + }); + + it('add numbers to powerpack via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.add_number('14845733595') + }) + .then(function (result) { + assert.Equal(result.number, "14845733595") + }) + }); + + it('find numbers to powerpack via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.find_number('14845733595') + }) + .then(function (result) { + assert.Equal(result.number, "14845733595") + }) + }); + + it('find shortcode via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.find_shortcode('4444444') + }) + .then(function (result) { + assert.Equal(result.shortcode, "4444444") + }) + }); + it('list shortcode via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.list_shortcode('4444444') + }) + .then(function (result) { + assert.notEqual(result.length, 0) + }) + }); + }); From 35326c8b316331a1f66dd3e4a71c84520218bdfa Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Thu, 14 Nov 2019 01:15:59 +0530 Subject: [PATCH 03/11] Update base.js --- lib/base.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/base.js b/lib/base.js index aa7701bf..7d613f9b 100644 --- a/lib/base.js +++ b/lib/base.js @@ -75,6 +75,34 @@ export class PlivoResource { }); }); } + customexecuteAction(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + getMetaResponse(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + let count = 0; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + count = response.body.meta.totalCount; + resolve(count); + }) + .catch(error => { + reject(error); + }); + }); + } } export class PlivoResourceInterface { From 708048964195a0e69f1fdad9b24d5fc075932fad Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Thu, 14 Nov 2019 01:19:41 +0530 Subject: [PATCH 04/11] Update client-test.js --- lib/rest/client-test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/rest/client-test.js b/lib/rest/client-test.js index 03620873..ef44b03d 100644 --- a/lib/rest/client-test.js +++ b/lib/rest/client-test.js @@ -7,6 +7,7 @@ import { ApplicationInterface } from '../resources/applications.js'; import { ConferenceInterface } from '../resources/conferences.js'; import { EndpointInterface } from '../resources/endpoints.js'; import { MessageInterface } from '../resources/messages.js'; +import { PowerpackInterface } from '../resources/powerpacks.js'; import { NumberInterface } from '../resources/numbers.js'; import { PricingInterface } from '../resources/pricings.js'; import { RecordingInterface } from '../resources/recordings.js'; @@ -47,6 +48,7 @@ export class Client { this.conferences = new ConferenceInterface(client); this.endpoints = new EndpointInterface(client); this.messages = new MessageInterface(client); + this.powerpacks = new PowerpackInterface(client); this.numbers = new NumberInterface(client); this.pricings = new PricingInterface(client); this.recordings = new RecordingInterface(client); From 23a9692d88974325d89dc39e174081f15e21ad80 Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Thu, 14 Nov 2019 01:27:30 +0530 Subject: [PATCH 05/11] Create powerpacks.js --- examples/powerpacks.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/powerpacks.js diff --git a/examples/powerpacks.js b/examples/powerpacks.js new file mode 100644 index 00000000..9f53d57e --- /dev/null +++ b/examples/powerpacks.js @@ -0,0 +1,39 @@ +var Plivo = require('../dist/rest/client.js'); +var client = new Plivo.Client(); + +var params = { + sticky_sender: true, + local_connect: true +}; +// create powerpack +client.powerpacks.create("node sdk test", params).then(function (result) { + console.log(result) +}); + +// get powerpack +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (result) { + console.log(result) + }); + +// list powerpack +client.powerpacks.list().then( + function (result) { + console.log(result) + }); +// list numbers +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (powerpack) { + return powerpack.list_numbers() + }) + .then(function (result) { + console.log("\n============ list numbers ===========\n", result) + }) +// find number +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (powerpack) { + return powerpack.find_number('1234') + }) + .then(function (result) { + console.log("\n============ list numbers ===========\n", result) + }) From bfae85cc8819d97b0b9dc502e4c2cd354a171c3e Mon Sep 17 00:00:00 2001 From: Vishnukumarpg <33220068+Vishnukumarpg@users.noreply.github.com> Date: Thu, 14 Nov 2019 18:07:23 +0530 Subject: [PATCH 06/11] Update client.js --- lib/rest/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rest/client.js b/lib/rest/client.js index 4817c369..ba04f678 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -61,7 +61,7 @@ export class Client { this.conferences = new ConferenceInterface(client); this.endpoints = new EndpointInterface(client); this.messages = new MessageInterface(client); - this,powerpacks = new PowerpackInterface(client); + this.powerpacks = new PowerpackInterface(client); this.numbers = new NumberInterface(client); this.pricings = new PricingInterface(client); this.recordings = new RecordingInterface(client); From ae1c0ac9bf0bea11202363663af3417fb5b748ed Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Mon, 18 Nov 2019 17:45:51 +0530 Subject: [PATCH 07/11] Update base.js --- lib/base.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/base.js b/lib/base.js index 7d613f9b..a83e621d 100644 --- a/lib/base.js +++ b/lib/base.js @@ -88,6 +88,19 @@ export class PlivoResource { }); }); } + customexecuteGetNumberAction(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + let number = ""; + let promise = client(method, url, params).then(response => { + number = response.body.objects[0].number; + return number; + }).catch(error => { + console.log(error); + return error; + }); + return promise; + } getMetaResponse(url, method = 'GET', params = {}) { let client = this[clientKey]; let idField = this[idKey]; From e3aa4eb598e8a36e8796e4238f8fea7fff85a473 Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Wed, 20 Nov 2019 12:59:04 +0530 Subject: [PATCH 08/11] Update powerpacks.js --- lib/resources/powerpacks.js | 505 +++++++++++++++++++++--------------- 1 file changed, 302 insertions(+), 203 deletions(-) diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js index 7f7983f2..cca22cef 100644 --- a/lib/resources/powerpacks.js +++ b/lib/resources/powerpacks.js @@ -1,12 +1,6 @@ -import { - extend, - validate -} from '../utils/common.js'; -import { - PlivoResource, - PlivoResourceInterface -} from '../base'; -import * as _ from "lodash"; +import { extend, validate } from '../utils/common.js'; +import { PlivoResource, PlivoResourceInterface } from '../base'; +import * as _ from 'lodash'; const action = 'Powerpack/'; const idField = 'uuid'; @@ -20,77 +14,130 @@ const clientKey = Symbol(); * @param {object} [data] - data of call */ export class Powerpack extends PlivoResource { + constructor(client, data = {}) { + super(action, Powerpack, idField, client); - constructor(client, data = {}) { - super(action, Powerpack, idField, client); + if (idField in data) { + this.uuid = data[idField]; + } + if (numberpoolIdField in data) { + this.number_pool_id = data[numberpoolIdField].split('/')[5]; + } + this.number_pool = new NumberPool(client, { + number_pool_id: this.number_pool_id + }); + extend(this, data); + } - if (idField in data) { - this.uuid = data[idField]; - } - if (numberpoolIdField in data) { - this.number_pool_id = data[numberpoolIdField].split('/')[5]; - } - this.number_pool = new NumberPool(client, { - number_pool_id: this.number_pool_id - }); - extend(this, data); - } + list_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); + } - list_numbers(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/'; - return super.customexecuteAction(path.toString().trim(), "GET", params); - } - count_numbers(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/'; - return super.getMetaResponse(path.toString().trim(), "GET", params); - } + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } - find_number(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "GET"); - } - add_number(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "POST"); - } - remove_number(number, unrent = false) { - var params; - params['unrent'] = unrent; - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "DELETE", params); - } - list_shortcodes(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; - return super.customexecuteAction(path.toString().trim(), "GET", params); - } - find_shortcode(shortcode) { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - return super.customexecuteAction(path.toString().trim(), "GET"); - } - buy_add_number(params) { - var number = params.number - if (params.number == undefined) { - try { - var response = super.customexecuteAction('PhoneNumber/', "GET", params); - number = response.body.objects[0].number - } catch (err) { - return err.message; - } - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "POST"); - } + query = query + '&'; - /** + return query; + } + + count_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } + + find_number(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + add_number(number) { + var params = {}; + params['rent'] = 'false'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove_number(number, unrent = false) { + var params; + params['unrent'] = unrent; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } + list_shortcodes(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find_shortcode(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } + + /** * update powerpack * @method * @param {object} params - to update Powerpack @@ -103,110 +150,160 @@ export class Powerpack extends PlivoResource { * @promise {object} return {@link Powerpack} object * @fail {Error} return Error */ - update(params) { - let path = 'Powerpack/' + this.uuid + '/'; - return super.customexecuteAction(path.toString().trim(), "POST", params); - } + update(params) { + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } - /** + /** * delete Powerpack * @method * @promise {object} return true on success * @fail {Error} return Error */ - delete(unrent_numbers = false) { - let params = {}; - if (typeof unrent_numbers === 'boolean') { - params.unrent_numbers = unrent_numbers.toString(); - } - let path = 'Powerpack/' + this.uuid + '/'; - return super.customexecuteAction(path.toString().trim(), "DELETE", params); - } + delete(unrent_numbers = false) { + let params = {}; + if (typeof unrent_numbers === 'boolean') { + params.unrent_numbers = unrent_numbers.toString(); + } + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } } -const numberPoolField = "number_pool_id" +const numberPoolField = 'number_pool_id'; export class NumberPool extends PlivoResource { - constructor(client, data = {}) { - super(action, NumberPool, numberPoolField, client); - this.numbers = new Numbers(client, { - number_pool_id: data.number_pool_id - }); - this.shortcodes = new Shortcode(client, { - number_pool_id: data.number_pool_id - }); + constructor(client, data = {}) { + super(action, NumberPool, numberPoolField, client); + this.numbers = new Numbers(client, { + number_pool_id: data.number_pool_id + }); + this.shortcodes = new Shortcode(client, { + number_pool_id: data.number_pool_id + }); - extend(this, data); - } + extend(this, data); + } } export class Numbers extends PlivoResource { - constructor(client, data = {}) { - super(action, Numbers, numberPoolField, client); - extend(this, data); - } - buy_add_number(params) { - var number = params.number - if (params.number == undefined) { - try { - var response = super.customexecuteAction('PhoneNumber/', "GET", params); - number = response.body.objects[0].number - } catch (err) { - return err.message; - } - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "POST"); - } + constructor(client, data = {}) { + super(action, Numbers, numberPoolField, client); + extend(this, data); + } + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } - list(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/'; - return super.customexecuteAction(path.toString().trim(), "GET", params); - } - count(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Number/'; - return super.getMetaResponse(path.toString().trim(), "GET", params); - } + list(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); + } + count(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } - find(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "GET"); - } - add(number) { - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "POST"); - } - remove(number, unrent = false) { - var params; - params['unrent'] = unrent; - let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; - return super.customexecuteAction(path.toString().trim(), "DELETE", params); - } + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } -} -export class Shortcode extends PlivoResource { - constructor(client, data = {}) { - super(action, Shortcode, numberPoolField, client); - extend(this, data); - this.number_pool_id = data.number_pool_id; - } - list(params) { - if (params === undefined) { - params = {}; - } - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; - return super.customexecuteAction(path.toString().trim(), "GET", params); - } - find(shortcode) { - let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; - return super.customexecuteAction(path.toString().trim(), "GET"); - } + query = query + '&'; + return query; + } + find(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + add(number) { + var params = {}; + params['rent'] = 'false'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove(number, unrent = false) { + var params; + params['unrent'] = unrent; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } +} +export class Shortcode extends PlivoResource { + constructor(client, data = {}) { + super(action, Shortcode, numberPoolField, client); + extend(this, data); + this.number_pool_id = data.number_pool_id; + } + list(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } } /** * Represents a Powerpack interface @@ -215,24 +312,23 @@ export class Shortcode extends PlivoResource { * @param {object} [data] - data of call */ export class PowerpackInterface extends PlivoResourceInterface { + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + extend(this, data); + } - constructor(client, data = {}) { - super(action, Powerpack, idField, client); - extend(this, data); - } - - /** + /** * get Powerpack by given id * @method * @param {string} uuid - id of Powerpack * @promise {object} return {@link Powerpack} object * @fail {Error} return Error */ - get(uuid) { - return super.get(uuid); - } + get(uuid) { + return super.get(uuid); + } - /** + /** * create Powerpack * @method * @param {string} name - name of Powerpack @@ -244,24 +340,25 @@ export class PowerpackInterface extends PlivoResourceInterface { * @promise {object} return {@link PlivoGenericResponse} object * @fail {Error} return Error */ - create(name, params = {}) { - - let errors = validate([{ - field: 'name', - value: name, - validators: ['isRequired', 'isString'] - }]); + create(name, params = {}) { + let errors = validate([ + { + field: 'name', + value: name, + validators: [ 'isRequired', 'isString' ] + } + ]); - if (errors) { - return errors; - } + if (errors) { + return errors; + } - params.name = name; + params.name = name; - return super.create(params); - } + return super.create(params); + } - /** + /** * update Powerpack * @method * @param {string} uuid - id of Powerpack @@ -274,29 +371,31 @@ export class PowerpackInterface extends PlivoResourceInterface { * @promise {object} return {@link Powerpack} object * @fail {Error} return Error */ - update(uuid, params) { - let errors = validate([{ - field: 'uuid', - value: uuid, - validators: ['isRequired'] - }]); + update(uuid, params) { + let errors = validate([ + { + field: 'uuid', + value: uuid, + validators: [ 'isRequired' ] + } + ]); - if (errors) { - return errors; - } - return new Powerpack(this[clientKey], { - uuid: uuid - }).update(params); - } + if (errors) { + return errors; + } + return new Powerpack(this[clientKey], { + uuid: uuid + }).update(params); + } - /** + /** * Get All Call Detail * @method * @param {object} params - params to get all call details. * @promise {object[]} returns list of Call Object * @fail {Error} returns Error */ - list(params) { - return super.list(params); - } + list(params) { + return super.list(params); + } } From 6b91a08c366e495006750cda4b30260c698e7d02 Mon Sep 17 00:00:00 2001 From: Narayana Shanubhogh <40889842+narayana-plivo@users.noreply.github.com> Date: Wed, 20 Nov 2019 14:12:35 +0530 Subject: [PATCH 09/11] Update powerpacks.js --- lib/resources/powerpacks.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js index cca22cef..71c9cac4 100644 --- a/lib/resources/powerpacks.js +++ b/lib/resources/powerpacks.js @@ -99,8 +99,12 @@ export class Powerpack extends PlivoResource { return super.customexecuteAction(path.toString().trim(), 'POST', params); } remove_number(number, unrent = false) { - var params; - params['unrent'] = unrent; + var params = {}; + if (typeof unrent === 'boolean') { + params['unrent'] = unrent.toString(); + } else { + params['unrent'] = unrent; + } let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; return super.customexecuteAction(path.toString().trim(), 'DELETE', params); } @@ -281,8 +285,12 @@ export class Numbers extends PlivoResource { return super.customexecuteAction(path.toString().trim(), 'POST', params); } remove(number, unrent = false) { - var params; - params['unrent'] = unrent; + var params = {}; + if (typeof unrent === 'boolean') { + params['unrent'] = unrent.toString(); + } else { + params['unrent'] = unrent; + } let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; return super.customexecuteAction(path.toString().trim(), 'DELETE', params); } From 4ca6ee3e6b8e237120933b97049fd1be55af4817 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Tue, 17 Dec 2019 16:06:37 +0530 Subject: [PATCH 10/11] removed get media --- lib/resources/messages.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/resources/messages.js b/lib/resources/messages.js index e0d53cd8..d674f1cf 100644 --- a/lib/resources/messages.js +++ b/lib/resources/messages.js @@ -35,9 +35,7 @@ export class Message extends PlivoResource { deleteMedia() { return super.executeAction(this.id + '/Media/', 'Delete', {}); } - getMedia(mediaID) { - return super.executeAction(this.id + '/Media/' + mediaID + '/', 'Get', {}); - } + } /** * Represents a Message Interface @@ -151,11 +149,7 @@ export class MessageInterface extends PlivoResourceInterface { id: messageUUID }).listMedia(); } - getMedia(messageUUID, mediaID) { - return new Message(this[clientKey], { - id: messageUUID - }).getMedia(mediaID); - } + deleteMedia(messageUUID) { return new Message(this[clientKey], { id: messageUUID From 2a8f0bbdb942479bdec31897ecc8a8ea6036709a Mon Sep 17 00:00:00 2001 From: nixonsamuel Date: Fri, 20 Dec 2019 16:23:42 +0530 Subject: [PATCH 11/11] bump version to 4.1.8 --- CHANGELOG.md | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f80ce24..1db41f38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## [4.1.8](https://github.com/plivo/plivo-node/releases/tag/v4.1.8)(2019-12-20) +- Add Powerpack support. + ## [4.1.7](https://github.com/plivo/plivo-node/releases/tag/v4.1.7)(2019-12-04) - Add MMS support. diff --git a/package.json b/package.json index 8003c30a..27f0b5c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plivo", - "version": "4.1.7", + "version": "4.1.8", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", "files": [