Skip to content

Commit c9e427f

Browse files
author
Lorna Jane Mitchell
authored
Merge pull request #10 from conshus/review-2-2020
2/2020 Review
2 parents 220b00d + a4836da commit c9e427f

File tree

6 files changed

+54
-45
lines changed

6 files changed

+54
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private.key
22
public.key
3+
.nexmo-app
34
node_modules
45
.env

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ npm start
5353

5454
To indicate that a conversation between two users should be allowed navigate to the following replacing `FROM_NUMBER` and `TO_NUMBER` with e.164 formatting international numbers (e.g. 14155550123):
5555

56-
http://localhost:5000/conversation/start/FROM_NUMBER/TO_NUMBER
56+
http://localhost:3000/conversation/start/FROM_NUMBER/TO_NUMBER
5757

5858
In a real system this conversation would be set up by an automated process.
5959

6060
To check the existing ongoing conversations navigate to:
6161

62-
http://localhost:5000/conversations
62+
http://localhost:3000/conversations
6363

6464
You should see a response such as the following:
6565

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require('dotenv').config();
44

5-
var config = {
5+
const config = {
66
NEXMO_API_KEY: process.env['NEXMO_API_KEY'],
77
NEXMO_API_SECRET: process.env['NEXMO_API_SECRET'],
88
NEXMO_DEBUG: process.env['NEXMO_DEBUG'],

lib/VoiceProxy.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"use strict";
22

3-
var Promise = require('bluebird');
4-
var Nexmo = require('nexmo');
3+
const Nexmo = require('nexmo');
54

65
/**
76
* Create a new VoiceProxy
87
*/
9-
var VoiceProxy = function(config) {
8+
const VoiceProxy = function(config) {
109
this.config = config;
1110

1211
this.nexmo = new Nexmo({
@@ -34,7 +33,7 @@ VoiceProxy.prototype.provisionVirtualNumbers = function() {
3433
console.error(err);
3534
}
3635
else {
37-
var numbers = res.numbers;
36+
const numbers = res.numbers;
3837

3938
// For demo purposes:
4039
// - Assume that at least two numbers will be available
@@ -63,7 +62,7 @@ VoiceProxy.prototype.rentNumber = function(number) {
6362
* Configure the number to be associated with the Voice Proxy application.
6463
*/
6564
VoiceProxy.prototype.configureNumber = function(number) {
66-
var options = {
65+
const options = {
6766
voiceCallbackType: 'app',
6867
voiceCallbackValue: this.config.NEXMO_APP_ID,
6968
};
@@ -104,34 +103,44 @@ VoiceProxy.prototype.createConversation = function(userANumber, userBNumber, cb)
104103
/**
105104
* Ensure the given numbers are valid and which country they are associated with.
106105
*/
107-
VoiceProxy.prototype.checkNumbers = function(userANumber, userBNumber) {
108-
var niGetPromise = Promise.promisify(this.nexmo.numberInsight.get, {context: this.nexmo.numberInsight});
109-
var userAGet = niGetPromise({level: 'basic', number: userANumber});
110-
var userBGet = niGetPromise({level: 'basic', number: userBNumber});
111-
106+
VoiceProxy.prototype.checkNumbers = function (userANumber, userBNumber) {
107+
const niGetPromise = (number) => new Promise ((resolve) => {
108+
this.nexmo.numberInsight.get(number, (error, result) => {
109+
if(error) {
110+
console.error('error',error);
111+
}
112+
else {
113+
return resolve(result);
114+
}
115+
})
116+
});
117+
118+
const userAGet = niGetPromise({level: 'basic', number: userANumber});
119+
const userBGet = niGetPromise({level: 'basic', number: userBNumber});
120+
112121
return Promise.all([userAGet, userBGet]);
113122
};
114123

115124
/**
116125
* Store the conversation information.
117126
*/
118127
VoiceProxy.prototype.saveConversation = function(results) {
119-
var userAResult = results[0];
120-
var userANumber = {
128+
const userAResult = results[0];
129+
const userANumber = {
121130
msisdn: userAResult.international_format_number,
122131
country: userAResult.country_code
123132
};
124133

125-
var userBResult = results[1];
126-
var userBNumber = {
134+
const userBResult = results[1];
135+
const userBNumber = {
127136
msisdn: userBResult.international_format_number,
128137
country: userBResult.country_code
129138
};
130139

131140
// Create conversation object - for demo purposes:
132141
// - Use first indexed LVN for user A
133142
// - Use second indexed LVN for user B
134-
var conversation = {
143+
const conversation = {
135144
userA: {
136145
realNumber: userANumber,
137146
virtualNumber: this.provisionedNumbers[0]
@@ -169,11 +178,11 @@ VoiceProxy.prototype.sendSMS = function(conversation) {
169178
return conversation;
170179
};
171180

172-
var fromUserAToUserB = function(from, to, conversation) {
181+
const fromUserAToUserB = function(from, to, conversation) {
173182
return (from === conversation.userA.realNumber.msisdn &&
174183
to === conversation.userB.virtualNumber.msisdn);
175184
};
176-
var fromUserBToUserA = function(from, to, conversation) {
185+
const fromUserBToUserA = function(from, to, conversation) {
177186
return (from === conversation.userB.realNumber.msisdn &&
178187
to === conversation.userA.virtualNumber.msisdn);
179188
};
@@ -182,14 +191,14 @@ var fromUserBToUserA = function(from, to, conversation) {
182191
* Work out real number to virual number mapping between users.
183192
*/
184193
VoiceProxy.prototype.getProxyRoute = function(from, to) {
185-
var proxyRoute = null;
186-
var conversation;
187-
for(var i = 0, l = this.conversations.length; i < l; ++i) {
194+
let proxyRoute = null;
195+
let conversation;
196+
for(let i = 0, l = this.conversations.length; i < l; ++i) {
188197
conversation = this.conversations[i];
189198

190199
// Use to and from to determine the conversation
191-
var fromUserA = fromUserAToUserB(from, to, conversation);
192-
var fromUserB = fromUserBToUserA(from, to, conversation);
200+
const fromUserA = fromUserAToUserB(from, to, conversation);
201+
const fromUserB = fromUserBToUserA(from, to, conversation);
193202

194203
if(fromUserA || fromUserB) {
195204
proxyRoute = {
@@ -209,25 +218,25 @@ VoiceProxy.prototype.getProxyRoute = function(from, to) {
209218
*/
210219
VoiceProxy.prototype.getProxyNCCO = function(from, to) {
211220
// Determine how the call should be routed
212-
var proxyRoute = this.getProxyRoute(from, to);
221+
const proxyRoute = this.getProxyRoute(from, to);
213222

214223
if(proxyRoute === null) {
215-
var errorText = 'No conversation found' +
224+
const errorText = 'No conversation found' +
216225
' from: ' + from +
217226
' to: ' + to;
218227
throw new Error(errorText);
219228
}
220229

221230
// Build the NCCO
222-
var ncco = [];
231+
let ncco = [];
223232

224-
var textAction = {
233+
const textAction = {
225234
action: 'talk',
226235
text: 'Please wait whilst we connect your call'
227236
};
228237
ncco.push(textAction);
229238

230-
var connectAction = {
239+
const connectAction = {
231240
action: 'connect',
232241
from: proxyRoute.from.virtualNumber.msisdn,
233242
endpoint: [{

lib/server.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
"use strict";
22

3-
var express = require('express');
4-
var bodyParser = require('body-parser');
3+
const express = require('express');
4+
const bodyParser = require('body-parser');
55

6-
var app = express();
7-
app.set('port', (process.env.PORT || 5000));
6+
const app = express();
7+
app.set('port', (process.env.PORT || 3000));
88
app.use(bodyParser.urlencoded({ extended: false }));
99

10-
var config = require(__dirname + '/../config');
10+
const config = require(__dirname + '/../config');
1111

12-
var VoiceProxy = require('./VoiceProxy');
13-
var voiceProxy = new VoiceProxy(config);
12+
const VoiceProxy = require('./VoiceProxy');
13+
const voiceProxy = new VoiceProxy(config);
1414

1515
app.listen(app.get('port'), function() {
1616
console.log('Voice Proxy App listening on port', app.get('port'));
1717
});
1818

1919
app.get('/proxy-call', function(req, res) {
20-
var from = req.query.from;
21-
var to = req.query.to;
20+
const from = req.query.from;
21+
const to = req.query.to;
2222

23-
var ncco = voiceProxy.getProxyNCCO(from, to);
23+
const ncco = voiceProxy.getProxyNCCO(from, to);
2424
res.json(ncco);
2525
});
2626

@@ -31,8 +31,8 @@ app.post('/event', function(req, res) {
3131
});
3232

3333
app.get('/conversation/start/:userANumber/:userBNumber', function(req, res) {
34-
var userANumber = req.params.userANumber;
35-
var userBNumber = req.params.userBNumber;
34+
const userANumber = req.params.userANumber;
35+
const userBNumber = req.params.userBNumber;
3636

3737
voiceProxy.createConversation(userANumber, userBNumber, function(err, result) {
3838
if(err) {
@@ -42,7 +42,7 @@ app.get('/conversation/start/:userANumber/:userBNumber', function(req, res) {
4242
res.json(result);
4343
}
4444
});
45-
})
45+
});
4646

4747
// Useful functions for testing out the functionality and querying bookings
4848
app.get('/', function(req, res) {
@@ -59,7 +59,7 @@ app.get('/numbers/reconfigure', function(req, res) {
5959
voiceProxy.reconfigureNumbers();
6060

6161
res.send('OK');
62-
})
62+
});
6363

6464
app.get('/numbers', function(req, res) {
6565
res.json(voiceProxy.provisionedNumbers);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"author": "Phil Leggetter <phil@leggetter.co.uk> (http://www.leggetter.co.uk)",
1919
"license": "MIT",
2020
"dependencies": {
21-
"bluebird": "^3.4.1",
2221
"body-parser": "^1.15.2",
2322
"dotenv": "^2.0.0",
2423
"express": "^4.14.0",

0 commit comments

Comments
 (0)