Update powerpacks.js

This commit is contained in:
Narayana Shanubhogh 2019-11-20 12:59:04 +05:30 committed by GitHub
parent ae1c0ac9bf
commit e3aa4eb598
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
}
}
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");
}
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 = {}) {
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;
}
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);
}
}