feat: add metadata to create call as tag

This commit is contained in:
Quan HL
2023-05-07 06:59:35 +07:00
parent 9948592080
commit 252ad2d07f
3 changed files with 74 additions and 51 deletions

View File

@@ -197,7 +197,8 @@ router.post('/', async(req, res) => {
callSid, callSid,
accountSid: req.body.account_sid, accountSid: req.body.account_sid,
applicationSid: app.application_sid, applicationSid: app.application_sid,
traceId: rootSpan.traceId traceId: rootSpan.traceId,
metadata: req.body.metadata
}); });
cs = new RestCallSession({ cs = new RestCallSession({
logger: sipLogger, logger: sipLogger,

View File

@@ -13,6 +13,7 @@ class CallInfo {
this.direction = opts.direction; this.direction = opts.direction;
this.traceId = opts.traceId; this.traceId = opts.traceId;
this.callTerminationBy = undefined; this.callTerminationBy = undefined;
this.metadata = opts.metadata;
if (opts.req) { if (opts.req) {
const u = opts.req.getParsedHeader('from'); const u = opts.req.getParsedHeader('from');
const uri = parseUri(u.uri); const uri = parseUri(u.uri);
@@ -91,6 +92,7 @@ class CallInfo {
this.from = from || req.callingNumber; this.from = from || req.callingNumber;
this.to = to; this.to = to;
if (tag) this._customerData = tag; if (tag) this._customerData = tag;
if (this.metadata) this._customerData = {...this._customerData , ...this.metadata}
} }
this.localSipAddress = srf.locals.localSipAddress; this.localSipAddress = srf.locals.localSipAddress;

View File

@@ -1,50 +1,51 @@
const test = require('tape'); const test = require("tape");
const { sippUac } = require('./sipp')('test_fs'); const { sippUac } = require("./sipp")("test_fs");
const bent = require('bent'); const bent = require("bent");
const clearModule = require('clear-module'); const clearModule = require("clear-module");
const {provisionCallHook} = require('./utils') const { provisionCallHook } = require("./utils");
const getJSON = bent('json') const getJSON = bent("json");
const waitFor = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); const waitFor = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
process.on('unhandledRejection', (reason, p) => { process.on("unhandledRejection", (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); console.log("Unhandled Rejection at: Promise", p, "reason:", reason);
}); });
function connect(connectable) { function connect(connectable) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
connectable.on('connect', () => { connectable.on("connect", () => {
return resolve(); return resolve();
}); });
}); });
} }
test('test create-call timeout', async(t) => { test("test create-call timeout", async (t) => {
clearModule.all(); clearModule.all();
const {srf, disconnect} = require('../app'); const { srf, disconnect } = require("../app");
try { try {
await connect(srf); await connect(srf);
// give UAS app time to come up // give UAS app time to come up
const p = sippUac('uas-timeout-cancel.xml', '172.38.0.10'); const p = sippUac("uas-timeout-cancel.xml", "172.38.0.10");
await waitFor(1000); await waitFor(1000);
// GIVEN // GIVEN
let account_sid = '622f62e4-303a-49f2-bbe0-eb1e1714e37a'; let account_sid = "622f62e4-303a-49f2-bbe0-eb1e1714e37a";
const post = bent('http://127.0.0.1:3000/', 'POST', 'json', 201); const post = bent("http://127.0.0.1:3000/", "POST", "json", 201);
post('v1/createCall', { post("v1/createCall", {
'account_sid':account_sid, account_sid: account_sid,
'timeout': 1, timeout: 1,
"call_hook": { call_hook: {
"url": "https://public-apps.jambonz.us/hello-world", url: "https://public-apps.jambonz.us/hello-world",
"method": "POST" method: "POST",
}, },
"from": "15083718299", from: "15083718299",
"to": { to: {
"type": "phone", type: "phone",
"number": "15583084809" number: "15583084809",
}}); },
});
//THEN //THEN
await p; await p;
disconnect(); disconnect();
@@ -55,50 +56,69 @@ test('test create-call timeout', async(t) => {
} }
}); });
test('test create-call call-hook basic authentication', async(t) => { test("test create-call call-hook basic authentication", async (t) => {
clearModule.all(); clearModule.all();
const {srf, disconnect} = require('../app'); const { srf, disconnect } = require("../app");
try { try {
await connect(srf); await connect(srf);
// GIVEN // GIVEN
let from = 'call_hook_basic_authentication'; let from = "call_hook_basic_authentication";
let account_sid = '622f62e4-303a-49f2-bbe0-eb1e1714e37a'; let account_sid = "622f62e4-303a-49f2-bbe0-eb1e1714e37a";
// Give UAS app time to come up // Give UAS app time to come up
const p = sippUac('uas.xml', '172.38.0.10', from); const p = sippUac("uas.xml", "172.38.0.10", from);
await waitFor(1000); await waitFor(1000);
const post = bent('http://127.0.0.1:3000/', 'POST', 'json', 201); const post = bent("http://127.0.0.1:3000/", "POST", "json", 201);
post('v1/createCall', { post("v1/createCall", {
'account_sid':account_sid, account_sid: account_sid,
"call_hook": { call_hook: {
"url": "http://127.0.0.1:3100/", url: "http://127.0.0.1:3100/",
"method": "POST", method: "POST",
"username": "username", username: "username",
"password": "password" password: "password",
}, },
"from": from, call_status_hook: {
"to": { url: "http://127.0.0.1:3100/callStatus",
"type": "phone", method: "POST",
"number": "15583084809" },
}}); from: from,
to: {
type: "phone",
number: "15583084809",
},
metadata: {
customer: "acme",
referenceId: "deadbeef",
},
});
let verbs = [ let verbs = [
{ {
"verb": "say", verb: "say",
"text": "hello" text: "hello",
} },
]; ];
provisionCallHook(from, verbs); provisionCallHook(from, verbs);
//THEN //THEN
await p; await p;
let obj = await getJSON(`http:127.0.0.1:3100/lastRequest/${from}`) let obj = await getJSON(`http:127.0.0.1:3100/lastRequest/${from}`);
t.ok(obj.headers.Authorization = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', t.ok(
'create-call: call-hook contains basic authentication header'); (obj.headers.Authorization === "Basic dXNlcm5hbWU6cGFzc3dvcmQ="),
"create-call: call-hook contains basic authentication header"
);
t.ok(
(obj.customerData.customer === 'acme'),
"create-call: metadata is working"
);
obj = await getJSON(`http:127.0.0.1:3100/lastRequest/${from}_callStatus`);
t.ok(
(obj.customerData.customer === 'acme'),
"create-call: metadata is working"
);
disconnect(); disconnect();
} catch (err) { } catch (err) {
console.log(`error received: ${err}`); console.log(`error received: ${err}`);