add tag task and varioius cleanup

This commit is contained in:
Dave Horton
2020-01-29 15:27:20 -05:00
parent bed4fa1f42
commit 92acd50595
17 changed files with 278 additions and 111 deletions

View File

@@ -3,6 +3,7 @@ const uuidv4 = require('uuid/v4');
const {CallStatus, CallDirection} = require('./utils/constants');
const CallInfo = require('./session/call-info');
const retrieveApp = require('./utils/retrieve-app');
const parseUrl = require('parse-url');
module.exports = function(srf, logger) {
const {lookupAppByPhoneNumber} = srf.locals.dbHelpers;
@@ -44,8 +45,8 @@ module.exports = function(srf, logger) {
const logger = req.locals.logger;
try {
const app = await lookupAppByPhoneNumber(req.locals.calledNumber);
if (!app) {
logger.info(`rejecting call to DID ${req.locals.calledNumber}: no application associated`);
if (!app || !app.call_hook || !app.call_hook.url) {
logger.info(`rejecting call to ${req.locals.calledNumber}: no application or webhook url`);
return res.send(480, {
headers: {
'X-Reason': 'no configured application'
@@ -53,22 +54,9 @@ 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')
});
req.locals.callInfo = new CallInfo({req, app, direction: CallDirection.Inbound});
next();
} catch (err) {
logger.error(err, `${req.get('Call-ID')} Error looking up application for ${req.calledNumber}`);
@@ -82,20 +70,25 @@ module.exports = function(srf, logger) {
async function invokeWebCallback(req, res, next) {
const logger = req.locals.logger;
const app = req.locals.application;
const method = (app.hook_http_method || 'POST').toUpperCase();
const qs = Object.assign({}, req.locals.callInfo, {
sipStatus: 100,
callStatus: CallStatus.Trying,
originatingSipIP: req.get('X-Forwarded-For'),
originatingSipTrunkName: req.get('X-Originating-Carrier')
});
const call_hook = app.call_hook;
const method = (call_hook.method || 'POST').toUpperCase();
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}`);
auth = Object.assign({}, {user: app.hook_basic_auth_user, password: app.hook_basic_auth_password});
if (call_hook.username && call_hook.password) {
auth = {username: call_hook.username, password: call_hook.password};
}
try {
app.tasks = await retrieveApp(logger, app.call_hook, method, auth, qs, method === 'POST' ? req.msg : null);
const u = parseUrl(call_hook.url);
const myPort = u.port ? `:${u.port}` : '';
app.originalRequest = {
baseUrl: `${u.protocol}://${u.resource}${myPort}`,
auth
};
logger.debug({originalRequest: app.originalRequest}, 'invokeWebCallback');
const obj = req.locals.callInfo;
// if the call hook is a POST add the entire SIP message to the payload
if (method === 'POST') Object.assign(obj, {sip: req.msg});
app.tasks = await retrieveApp(logger, app.call_hook, method, auth, obj);
next();
} catch (err) {
logger.error(err, 'Error retrieving or parsing application');