Skip to content

Commit 80e39c9

Browse files
author
Daniel Tolbert
authored
Merge pull request #6 from Bandwidth/DX-948_csr-support
Dx 948 csr support
2 parents 28a337d + 0630ee0 commit 80e39c9

File tree

8 files changed

+304
-24
lines changed

8 files changed

+304
-24
lines changed

README.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ NodeJs Client library for Bandwidth Numbers API
99
## Supported Versions
1010
This SDK stable for node versions 7 and above
1111

12-
| Version | Support Level |
13-
|:-------------------------------|:-------------------------|
14-
| <7 | Unsupported |
15-
| 7.* | Supported |
16-
| 8.* | Supported |
17-
| 9.* | Supported |
18-
| 10.4.1 | Supported |
12+
| Version | Support Level | |
13+
|:--------|:--------------|:----------|
14+
| <7 | Unsupported | |
15+
| > 7 | | Supported |
1916

2017
## Release Notes
21-
| Version | Notes |
22-
|:--|:--|
23-
| 1.1.0| Added import tn functionality, added promise based `Async` functions |
18+
| Version | Notes |
19+
|:--------|:---------------------------------------------------------------------|
20+
| 1.1.0 | Added import tn functionality, added promise based `Async` functions |
21+
| 1.2.0 | Added CSR lookup functionality |
2422

2523

2624
## Install
@@ -59,6 +57,12 @@ Each API Call also contains an async method that returns a promise for use with
5957

6058
The async method is the original method name with `Async` added.
6159

60+
### Some Examples
61+
62+
* `numbers.Site.create` : `numbers.Site.createAsync`
63+
* `numbers.AvailableNumbers.list` : `numbers.AvailableNumbers.listAsync`
64+
* `numbers.Order.create`: `numbers.Order.createAsync`
65+
6266
### Example for listing Available Numbers
6367

6468
#### Callbacks
@@ -859,4 +863,63 @@ catch (e) {
859863
}
860864
```
861865

866+
## CSR Lookup
867+
868+
### Create CSR Order
869+
870+
```js
871+
const data = {
872+
customerOrderId: "MyId5",
873+
WorkingOrBillingTelephoneNumber:"9198675309",
874+
accountNumber:"123463",
875+
endUserPIN:"1231"
876+
};
877+
878+
try {
879+
const csrOrderResponse = await CsrOrder.createAsync(data);
880+
console.log(csrOrderResponse.orderId);
881+
//31e0b16b-4720-4f2e-bb99-1399eeb2ff9e
882+
}
883+
catch (e) {
884+
console.log(e);
885+
}
886+
```
887+
888+
### Fetch Existing CSR Order
889+
890+
If the CSR order is in an FAILED state, the SDK will throw an error
891+
892+
#### COMPLETE or PROCESSING resposne
893+
894+
```js
895+
const csrOrderId = "1234-abc"
896+
897+
try {
898+
const csrOrderData = await CsrOrder.getAsync(csrOrderId);
899+
console.log(csrOrderData.status);
900+
//COMPLETE
901+
}
902+
catch (e) {
903+
console.log(e);
904+
}
905+
```
906+
907+
#### FAILED response
908+
909+
```js
910+
const csrOrderId = "1234-abc"
862911

912+
try {
913+
const csrOrderData = await CsrOrder.getAsync(csrOrderId);
914+
console.log(csrOrderData.status);
915+
//Won't fire, as request is failed
916+
}
917+
catch (e) {
918+
console.log(e);
919+
// [BandwidthError: CSR is not available for this TN] {
920+
// code: 26500,
921+
// message: 'CSR is not available for this TN',
922+
// httpStatus: 200
923+
// }
924+
}
925+
```

lib/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ function findDescendants(obj, name){
106106
function createPostOrPutRequest(method, path, data){
107107
var request = this.prepareRequest(superagent[method](this.prepareUrl(path)));
108108
if(data){
109-
return request.send(this.buildXml(data)).type("application/xml");
109+
var requestBody = this.buildXml(data);
110+
return request.send(requestBody).type("application/xml");
110111
}
111112
return request;
112113
}

lib/csrOrder.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var Client = require("./client");
2+
var CSR_ORDER_PATH = "csrs";
3+
4+
function CsrOrder() {
5+
}
6+
7+
CsrOrder.get = function(client, id, callback){
8+
if(arguments.length === 2){
9+
callback = id;
10+
id = client;
11+
client = new Client();
12+
}
13+
client.makeRequest("get", client.concatAccountPath(CSR_ORDER_PATH), null, id, function(err,res){
14+
if(err){
15+
return callback(err);
16+
}
17+
var item = res;
18+
item.client = client;
19+
item.__proto__ = CsrOrder.prototype;
20+
callback(null, item);
21+
});
22+
};
23+
24+
// Coming soon
25+
// CsrOrder.list = function(client,query, callback){
26+
// if(arguments.length === 2){
27+
// callback = query;
28+
// query = client;
29+
// client = new Client();
30+
// }
31+
// client.makeRequest("get", client.concatAccountPath(CSR_ORDER_PATH), query, function(err,res){
32+
// if(err){
33+
// return callback(err);
34+
// }
35+
// callback(null, res);
36+
// });
37+
// };
38+
39+
CsrOrder.create = function(client, data, callback){
40+
if(arguments.length === 2){
41+
callback = data;
42+
data = client;
43+
client = new Client();
44+
}
45+
client.makeRequest("post", client.concatAccountPath(CSR_ORDER_PATH), {Csr: data}, function(err, item){
46+
if(err){
47+
return callback(err, item);
48+
}
49+
item.client = client;
50+
item.__proto__ = CsrOrder.prototype;
51+
callback(null, item);
52+
});
53+
};
54+
55+
CsrOrder.prototype.update = function(data, callback){
56+
this.client.makeRequest("put", this.client.concatAccountPath(CSR_ORDER_PATH) + "/" + this.id, {Csr: data}, callback);
57+
};
58+
59+
CsrOrder.prototype.getNotes = function(callback){
60+
this.client.makeRequest("get", this.client.concatAccountPath(CSR_ORDER_PATH) + "/" + this.id + "/notes", function(err, notes){
61+
if(err){
62+
return callback(err);
63+
}
64+
var items = notes.note;
65+
callback(null, Array.isArray(items)?items:[items]);
66+
});
67+
};
68+
69+
CsrOrder.prototype.addNote = function(note, callback){
70+
var self = this;
71+
var request = this.client.createPostRequest(this.client.concatAccountPath(CSR_ORDER_PATH + "/" + this.id + "/notes"), {note: note});
72+
request.buffer().then(res =>{
73+
if(res.ok && res.headers.location){
74+
Client.getIdFromLocationHeader(res.headers.location, function(err, id){
75+
if(err){
76+
return callback(err);
77+
}
78+
self.getNotes(function(err, notes){
79+
if(err){
80+
return callback(err);
81+
}
82+
callback(null, notes.filter(function(n){ return n.id == id;})[0]);
83+
});
84+
});
85+
}
86+
else{
87+
self.client.checkResponse(res, callback);
88+
}
89+
});
90+
};
91+
92+
module.exports = CsrOrder;

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = {
2727
Dlda: require("./dlda"),
2828
ImportTnChecker: require("./importTnChecker"),
2929
ImportTnOrder: require("./importTnOrder"),
30-
RemoveImportedTnOrder: require("./removeImportedTnOrder")
30+
RemoveImportedTnOrder: require("./removeImportedTnOrder"),
31+
CsrOrder: require("./csrOrder")
3132
};
3233

3334
for (const property in module.exports) {

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bandwidth/numbers",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "NodeJs Client library for Bandwidth Numbers API",
55
"main": "index.js",
66
"scripts": {

test/csrOrder.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
var lib = require("../");
2+
var helper = require("./helper");
3+
var nock = require("nock");
4+
var CsrOrder = lib.CsrOrder;
5+
6+
describe("Csr", function(){
7+
before(function(){
8+
nock.disableNetConnect();
9+
helper.setupGlobalOptions();
10+
});
11+
after(function(){
12+
nock.cleanAll();
13+
nock.enableNetConnect();
14+
});
15+
// Coming Soon
16+
// describe("#list", function(){
17+
// it("should return a list of Csrs ", function(done){
18+
// helper.nock().get("/accounts/FakeAccountId/csrs").reply(200, helper.xml.csrOrders, {"Content-Type":"application/xml"});
19+
// CsrOrder.list(helper.createClient(), {}, function(err,list){
20+
// if(err){
21+
// return done(err);
22+
// }
23+
// list.should.be.ok;
24+
// done();
25+
// });
26+
// });
27+
// });
28+
describe("#get", function(){
29+
it("should get Csr successfully", function(done){
30+
helper.nock().get("/accounts/FakeAccountId/csrs/1").reply(200, helper.xml.csrOrder, {"Content-Type": "application/xml"});
31+
CsrOrder.get(helper.createClient(), "1", function(err, csrOrder){
32+
if(err){
33+
return done(err);
34+
}
35+
csrOrder.should.be.ok;
36+
csrOrder.csrData.serviceAddress.unparsedAddress.should.eql("1234 Main ST Durham NC 27707");
37+
csrOrder.csrData.workingTelephoneNumber.should.eql("9198675309");
38+
done();
39+
});
40+
});
41+
});
42+
describe("#create", function(){
43+
it("should create Csr successfully", function(done){
44+
var data = {
45+
customerOrderId: "MyId5",
46+
WorkingOrBillingTelephoneNumber:"9198675309",
47+
accountNumber:"123463",
48+
endUserPIN:"1231"
49+
};
50+
helper.nock().post("/accounts/FakeAccountId/csrs", helper.buildXml({Csr: data})).reply(201, helper.xml.csrResponse, {"Location": "/accounts/FakeAccountId/csrs/1"});
51+
CsrOrder.create(helper.createClient(), data, function(err, csrOrder){
52+
if(err){
53+
return done(err);
54+
}
55+
csrOrder.should.be.ok;
56+
csrOrder.orderId.should.eql("218a295f-4f8a-4d1a-ba55-3e0aac6207cb");
57+
done();
58+
});
59+
});
60+
});
61+
describe("#update", function(){
62+
it("should update successfully", function(done){
63+
var data = {endUserPIN:"1234"};
64+
helper.nock().put("/accounts/FakeAccountId/csrs/1", helper.buildXml({Csr: data})).reply(200);
65+
var order = new CsrOrder();
66+
order.id = "1";
67+
order.client = helper.createClient();
68+
order.endUserPIN = "1234";
69+
order.update(data,done);
70+
})
71+
});
72+
describe("#getNotes", function(){
73+
it("should return notes", function(done){
74+
helper.nock().get("/accounts/FakeAccountId/csrs/1/notes").reply(200, helper.xml.notes, {"Content-Type": "application/xml"});
75+
var order = new CsrOrder();
76+
order.id = 1;
77+
order.client = helper.createClient();
78+
order.getNotes(function(err, notes){
79+
if(err){
80+
return done(err);
81+
}
82+
notes.length.should.equal(2);
83+
notes[0].id.should.equal(11299);
84+
notes[0].userId.should.equal("customer");
85+
notes[0].description.should.equal("Test");
86+
done();
87+
});
88+
});
89+
it("should fail for error status code", function(done){
90+
helper.nock().get("/accounts/FakeAccountId/orders/1/notes").reply(400);
91+
var order = new CsrOrder();
92+
order.id = 1;
93+
order.client = helper.createClient();
94+
order.getNotes(function(err, notes){
95+
if(err){
96+
return done();
97+
}
98+
done(new Error("An error is estimated"));
99+
});
100+
});
101+
});
102+
describe("#addNote", function(){
103+
it("should add new note", function(done){
104+
var data = {userId: "customer", description: "Test"};
105+
helper.nock().post("/accounts/FakeAccountId/csrs/1/notes", helper.buildXml({note: data})).reply(200, "", {"Location": "/accounts/FakeAccountId/csrs/1/notes/11299"});
106+
helper.nock().get("/accounts/FakeAccountId/csrs/1/notes").reply(200, helper.xml.notes, {"Content-Type": "application/xml"});
107+
var order = new CsrOrder();
108+
order.id = 1;
109+
order.client = helper.createClient();
110+
order.addNote(data, function(err, note){
111+
if(err){
112+
return done(err);
113+
}
114+
note.id.should.equal(11299);
115+
note.userId.should.equal("customer");
116+
note.description.should.equal("Test");
117+
done();
118+
});
119+
});
120+
});
121+
});

0 commit comments

Comments
 (0)