mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
major refactoring
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
//const debug = require('debug')('jambonz:feature-server');
|
||||
const assert = require('assert');
|
||||
const request = require('request');
|
||||
//require('request-debug')(request);
|
||||
const uuidv4 = require('uuid/v4');
|
||||
const makeTask = require('./tasks/make_task');
|
||||
const normalizeJamones = require('./utils/normalize-jamones');
|
||||
const {CallStatus, CallDirection} = require('./utils/constants');
|
||||
const CallInfo = require('./session/call-info');
|
||||
const retrieveApp = require('./utils/retrieve-app');
|
||||
|
||||
module.exports = function(srf, logger) {
|
||||
const {lookupAppByPhoneNumber} = srf.locals.dbHelpers;
|
||||
|
||||
function initLocals(req, res, next) {
|
||||
req.locals = req.locals || {};
|
||||
req.locals.logger = logger.child({callId: req.get('Call-ID')});
|
||||
const callSid = uuidv4();
|
||||
req.locals = {
|
||||
callSid,
|
||||
logger: logger.child({callId: req.get('Call-ID'), callSid})
|
||||
};
|
||||
next();
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ module.exports = function(srf, logger) {
|
||||
async function retrieveApplication(req, res, next) {
|
||||
const logger = req.locals.logger;
|
||||
try {
|
||||
const app = req.locals.application = await lookupAppByPhoneNumber(req.locals.calledNumber);
|
||||
const app = await lookupAppByPhoneNumber(req.locals.calledNumber);
|
||||
if (!app) {
|
||||
logger.info(`rejecting call to DID ${req.locals.calledNumber}: no application associated`);
|
||||
return res.send(480, {
|
||||
@@ -52,7 +53,23 @@ module.exports = function(srf, logger) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//TODO: temp hack pre-refactoring to latest db schema: bang the data into expected shape
|
||||
|
||||
req.locals.application = app;
|
||||
//end hack
|
||||
logger.debug(app, `retrieved application for ${req.locals.calledNumber}`);
|
||||
const from = req.getParsedHeader('From');
|
||||
req.locals.callInfo = new CallInfo({
|
||||
callSid: req.locals.callSid,
|
||||
accountSid: app.account_sid,
|
||||
applicationSid: app.application_sid,
|
||||
from: req.callingNumber,
|
||||
to: req.calledNumber,
|
||||
direction: CallDirection.Inbound,
|
||||
callerName: from.name || req.callingNumber,
|
||||
callId: req.get('Call-ID')
|
||||
});
|
||||
next();
|
||||
} catch (err) {
|
||||
logger.error(err, `${req.get('Call-ID')} Error looking up application for ${req.calledNumber}`);
|
||||
@@ -66,52 +83,23 @@ module.exports = function(srf, logger) {
|
||||
async function invokeWebCallback(req, res, next) {
|
||||
const logger = req.locals.logger;
|
||||
const app = req.locals.application;
|
||||
const call_sid = uuidv4();
|
||||
const method = (app.hook_http_method || 'POST').toUpperCase();
|
||||
const from = req.getParsedHeader('From');
|
||||
req.locals.callAttributes = {
|
||||
CallSid: call_sid,
|
||||
AccountSid: app.account_sid,
|
||||
From: req.callingNumber,
|
||||
To: req.calledNumber,
|
||||
Direction: CallDirection.Inbound,
|
||||
CallerName: from.name || req.callingNumber,
|
||||
SipCallID: req.get('Call-ID')
|
||||
};
|
||||
const qs = Object.assign({}, req.locals.callAttributes, {
|
||||
CallStatus: CallStatus.Trying,
|
||||
SipStatus: 100,
|
||||
const qs = Object.assign({}, req.locals.callInfo, {
|
||||
sipStatus: 100,
|
||||
callStatus: CallStatus.Trying,
|
||||
RequestorIP: req.get('X-Forwarded-For'),
|
||||
RequestorName: req.get('X-Originating-Carrier')
|
||||
});
|
||||
const opts = {
|
||||
url: app.call_hook,
|
||||
method,
|
||||
json: true,
|
||||
qs
|
||||
};
|
||||
let auth;
|
||||
if (app.hook_basic_auth_user && app.hook_basic_auth_password) {
|
||||
logger.debug(`using basic auth with ${app.hook_basic_auth_user}:${app.hook_basic_auth_password}`);
|
||||
Object.assign(opts, {auth: {user: app.hook_basic_auth_user, password: app.hook_basic_auth_password}});
|
||||
auth = Object.assign({}, {user: app.hook_basic_auth_user, password: app.hook_basic_auth_password});
|
||||
}
|
||||
if (method === 'POST') Object.assign(opts, {body: req.msg});
|
||||
try {
|
||||
request(opts, (err, response, body) => {
|
||||
if (err) {
|
||||
logger.error(err, `Error invoking callback ${app.call_hook}`);
|
||||
return res.send(500, 'Webhook Failure');
|
||||
}
|
||||
logger.debug(body, `application payload: ${body}`);
|
||||
try {
|
||||
app.tasks = normalizeJamones(logger, body).map((tdata) => makeTask(logger, tdata));
|
||||
next();
|
||||
} catch (err) {
|
||||
logger.error(err, 'Invalid Webhook Response');
|
||||
res.send(500);
|
||||
}
|
||||
});
|
||||
app.tasks = await retrieveApp(logger, app.call_hook, method, auth, qs, method === 'POST' ? req.msg : null);
|
||||
next();
|
||||
} catch (err) {
|
||||
logger.error(err, 'Error invoking web callback');
|
||||
logger.error(err, 'Error retrieving or parsing application');
|
||||
res.send(500);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user