Skip to content

Commit f51efbc

Browse files
committed
Changed the number purchase flow to use SDK
1 parent 5b136f8 commit f51efbc

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

package-lock.json

Lines changed: 33 additions & 15 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
@@ -12,7 +12,7 @@
1212
"@koa/router": "^12.0.0",
1313
"@types/koa__router": "^12.0.0",
1414
"@types/koa-router": "^7.4.4",
15-
"@vonage/server-sdk": "^3.0.3",
15+
"@vonage/server-sdk": "^3.0.4",
1616
"dotenv": "^16.0.3",
1717
"koa": "^2.13.4",
1818
"koa-body": "^6.0.1",

server-src/api.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import cors from '@koa/cors'
1212

1313
const base64Key = process.env.VONAGE_PRIVATE_KEY || ''
1414
const buff = Buffer.from(base64Key, 'base64')
15-
const vonage = new Vonage(new Auth({
15+
const vonageAuth = new Auth({
1616
apiKey: process.env.VONAGE_API_KEY,
1717
apiSecret: process.env.VONAGE_API_SECRET,
1818
privateKey: buff.toString('ascii'),
1919
applicationId: process.env.VONAGE_APPLICATION_ID,
20-
}));
20+
});
21+
const vonage = new Vonage(vonageAuth);
2122

2223
async function home(ctx: any) {
2324
ctx.body = { 'status': 'ok' }
@@ -28,23 +29,14 @@ let driverNumber: string = '';
2829
let clientNumber: string = '';
2930

3031
async function placeOrder(ctx: any) {
31-
console.log(ctx.request.body)
32-
clientNumber = ctx.request.body.client_number;
32+
clientNumber = JSON.parse(ctx.request.body).client_number;
3333
await vonage.numbers.getAvailableNumbers({ country: 'US', size: 1, type: LineType['LANDLINE-TOLL-FREE'] })
3434
.then(async (resp: any) => {
3535
driverNumber = resp.numbers[0].msisdn;
36-
const buyURL = 'https://rest.nexmo.com/number/buy?' + new URLSearchParams([["api_key", process.env.VONAGE_API_KEY || ''], ["api_secret", process.env.VONAGE_API_SECRET || ''], ['country', 'US'], ['msisdn', driverNumber]]);
37-
await fetch(buyURL, {
38-
method: 'POST',
39-
})
40-
.then(resp => resp.json())
41-
.then(async resp => {
42-
await fetch('https://rest.nexmo.com/number/update?' + new URLSearchParams([["api_key", process.env.VONAGE_API_KEY || ''], ["api_secret", process.env.VONAGE_API_SECRET || ''], ['country', 'US'], ['msisdn', driverNumber], ['app_id', process.env.VONAGE_APPLICATION_ID || '']]), {
43-
method: 'POST',
44-
})
45-
.then(resp => resp.json())
46-
.then(resp => {
47-
console.log('Updated', resp);
36+
await vonage.numbers.buyNumber({ country: 'US', msisdn: driverNumber })
37+
.then(async () => {
38+
await vonage.numbers.updateNumber({ country: 'US', msisdn: driverNumber, applicationId: process.env.VONAGE_APPLICATION_ID })
39+
.then(() => {
4840
ctx.body = { driver_number: driverNumber }
4941
})
5042
.catch(err => console.error(err))
@@ -74,7 +66,6 @@ async function answerCall(ctx: any) {
7466
}
7567

7668
async function answerMessage(ctx: any) {
77-
console.log(deliveryPhone, driverNumber, clientNumber)
7869
if (ctx.request.body.from === clientNumber) {
7970
await vonage.messages.send(new SMS('From orderer: ' + ctx.request.body.text, deliveryPhone, driverNumber))
8071
.then(resp => { ctx.status = 200; ctx.body = { response: 'OK' } })

0 commit comments

Comments
 (0)