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

@@ -1,22 +1,69 @@
const {CallDirection, CallStatus} = require('../lib/constants');
const uuidv4 = require('uuid/v4');
class CallInfo {
constructor(opts) {
this.callSid = opts.callSid;
this.parentCallSid = opts.parentCallSid;
this.direction = opts.direction;
this.from = opts.from;
this.to = opts.to;
this.callId = opts.callId;
this.sipStatus = opts.sipStatus;
this.callStatus = opts.callStatus;
this.callerId = opts.callerId;
this.accountSid = opts.accountSid;
this.applicationSid = opts.applicationSid;
if (this.direction === CallDirection.Inbound) {
const {app, req} = opts;
this.callSid = req.locals.callSid,
this.accountSid = app.account_sid,
this.applicationSid = app.application_sid;
this.from = req.callingNumber;
this.to = req.calledNumber;
this.callerName = this.from.name || req.callingNumber;
this.callId = req.get('Call-ID');
this.sipStatus = 100;
this.callStatus = CallStatus.Trying;
this.originatingSipIP = req.get('X-Forwarded-For');
this.originatingSipTrunkName = req.get('X-Originating-Carrier');
}
else if (opts.parentCallInfo instanceof CallInfo) {
const {req, parentCallInfo} = opts;
this.callSid = uuidv4();
this.parentCallSid = parentCallInfo.callSid;
this.accountSid = parentCallInfo.accountSid;
this.applicationSid = parentCallInfo.applicationSid;
this.from = req.callingNumber;
this.to = req.calledNumber;
this.callerName = this.from.name || req.callingNumber;
this.callId = req.get('Call-ID');
this.callStatus = CallStatus.Trying,
this.sipStatus = 100;
}
}
updateCallStatus(callStatus, sipStatus) {
this.callStatus = callStatus;
if (sipStatus) this.sipStatus = sipStatus;
}
set customerData(obj) {
this._customerData = obj;
}
toJSON() {
const obj = {
callSid: this.callSid,
direction: this.direction,
from: this.from,
to: this.to,
callId: this.callId,
sipStatus: this.sipStatus,
callStatus: this.callStatus,
callerId: this.callId,
accountSid: this.accountSid,
applicationSid: this.applicationSid
};
['parentCallSid', 'originatingSipIP', 'originatingSipTrunkName'].forEach((prop) => {
if (this[prop]) obj[prop] = this[prop];
});
if (this._customerData && Object.keys(this._customerData).length) {
obj.customerData = this._customerData;
}
return obj;
}
}
module.exports = CallInfo;

View File

@@ -1,24 +1,44 @@
const Emitter = require('events');
const config = require('config');
const {CallDirection, TaskPreconditions, CallStatus} = require('../utils/constants');
const hooks = require('../utils/notifiers');
const moment = require('moment');
const assert = require('assert');
const BADPRECONDITIONS = 'preconditions not met';
class CallSession extends Emitter {
constructor({logger, application, srf, tasks, callSid}) {
constructor({logger, application, srf, tasks, callInfo}) {
super();
this.logger = logger;
this.application = application;
this.srf = srf;
this.callSid = callSid;
this.callInfo = callInfo;
this.tasks = tasks;
const {notifyHook} = hooks(this.logger, this.callInfo);
this.notifyHook = notifyHook;
this.taskIdx = 0;
this.stackIdx = 0;
this.callGone = false;
}
get callSid() {
return this.callInfo.callSid;
}
get originalRequest() {
return this.application.originalRequest;
}
get direction() {
return this.callInfo.direction;
}
get callId() {
return this.callInfo.direction;
}
async exec() {
this.logger.info(`CallSession:exec starting task list with ${this.tasks.length} tasks`);
while (this.tasks.length && !this.callGone) {
@@ -176,19 +196,13 @@ class CallSession extends Emitter {
}
return {ms: this.ms, ep: this.ep};
}
_notifyCallStatusChange(callStatus) {
this.logger.debug({app: this.application}, `CallSession:_notifyCallStatusChange: ${JSON.stringify(callStatus)}`);
_notifyCallStatusChange({callStatus, sipStatus}) {
this.logger.debug(`CallSession:_notifyCallStatusChange: ${callStatus} ${sipStatus}`);
this.callInfo.updateStatus(callStatus, sipStatus);
try {
const auth = {};
if (this.application.hook_basic_auth_user && this.application.hook_basic_auth_password) {
Object.assign(auth, {user: this.application.hook_basic_auth_user, password: this.hook_basic_auth_password});
}
this.notifyHook(this.application.call_status_hook,
this.application.hook_http_method,
auth,
callStatus);
this.notifyHook(this.application.call_status_hook);
} catch (err) {
this.logger.info(err, `CallSession:_notifyCallStatusChange error sending ${JSON.stringify(callStatus)}`);
this.logger.info(err, `CallSession:_notifyCallStatusChange error sending ${callStatus} ${sipStatus}`);
}
}
}

View File

@@ -1,6 +1,5 @@
const CallSession = require('./call-session');
const {CallDirection, CallStatus} = require('../utils/constants');
const hooks = require('../utils/notifiers');
const {CallStatus} = require('../utils/constants');
const moment = require('moment');
const assert = require('assert');
@@ -10,17 +9,11 @@ class InboundCallSession extends CallSession {
logger: req.locals.logger,
srf: req.srf,
application: req.locals.application,
callSid: req.locals.callInfo.callSid,
callInfo: req.locals.callInfo,
tasks: req.locals.application.tasks
});
this.req = req;
this.res = res;
this.srf = req.srf;
this.logger = req.locals.logger;
this.callInfo = req.locals.callInfo;
this.direction = CallDirection.Inbound;
const {notifyHook} = hooks(this.logger, this.callInfo);
this.notifyHook = notifyHook;
req.on('cancel', this._callReleased.bind(this));