write service_provider_sid with alerts

This commit is contained in:
Dave Horton
2022-09-07 23:51:40 +02:00
parent e90ef6bc70
commit 889257d7db
14 changed files with 85 additions and 175 deletions

View File

@@ -17,7 +17,8 @@ module.exports = function(srf, logger) {
lookupAppByRegex,
lookupAppBySid,
lookupAppByRealm,
lookupAppByTeamsTenant
lookupAppByTeamsTenant,
lookupAccountBySid
} = srf.locals.dbHelpers;
const {
writeAlerts,
@@ -25,7 +26,7 @@ module.exports = function(srf, logger) {
} = srf.locals;
const {lookupAccountDetails} = dbUtils(logger, srf);
function initLocals(req, res, next) {
const initLocals = async(req, res, next) => {
const callId = req.get('Call-ID');
logger.info({callId}, 'new incoming call');
if (!req.has('X-Account-Sid')) {
@@ -34,7 +35,10 @@ module.exports = function(srf, logger) {
}
const callSid = req.has('X-Retain-Call-Sid') ? req.get('X-Retain-Call-Sid') : uuidv4();
const account_sid = req.get('X-Account-Sid');
req.locals = {callSid, account_sid, callId};
console.log(`account_sid: ${account_sid}`);
const account = await lookupAccountBySid(account_sid);
console.log(`account: ${JSON.stringify(account)}`);
req.locals = {callSid, account_sid, service_provider_sid: account?.service_provider_sid, callId};
if (req.has('X-Application-Sid')) {
const application_sid = req.get('X-Application-Sid');
logger.debug(`got application from X-Application-Sid header: ${application_sid}`);
@@ -44,7 +48,7 @@ module.exports = function(srf, logger) {
if (req.has('X-MS-Teams-Tenant-FQDN')) req.locals.msTeamsTenant = req.get('X-MS-Teams-Tenant-FQDN');
next();
}
};
function createRootSpan(req, res, next) {
const {callId, callSid, account_sid} = req.locals;
@@ -161,7 +165,7 @@ module.exports = function(srf, logger) {
* Given the dialed DID/phone number, retrieve the application to invoke
*/
async function retrieveApplication(req, res, next) {
const {logger, accountInfo, account_sid, rootSpan} = req.locals;
const {logger, accountInfo, service_provider_sid, account_sid, rootSpan} = req.locals;
const {span} = rootSpan.startChildSpan('lookupApplication');
try {
let app;
@@ -231,9 +235,22 @@ module.exports = function(srf, logger) {
app.call_hook.method = 'WS';
}
else {
app.requestor = new HttpRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret);
if (app.call_status_hook) app.notifier = new HttpRequestor(logger, account_sid, app.call_status_hook,
accountInfo.account.webhook_secret);
app.requestor = new HttpRequestor(
logger,
service_provider_sid,
account_sid,
app.call_hook,
accountInfo.account.webhook_secret
);
if (app.call_status_hook) {
app.notifier = new HttpRequestor(
logger,
service_provider_sid,
account_sid,
app.call_status_hook,
accountInfo.account.webhook_secret
);
}
else app.notifier = {request: () => {}};
}
@@ -316,6 +333,7 @@ module.exports = function(srf, logger) {
span?.setAttributes({webhookStatus: err.statusCode});
span?.end();
writeAlerts({
service_provider_sid: req.locals.service_provider_sid,
account_sid: req.locals.account_sid,
target_sid: req.locals.callSid,
alert_type: AlertType.INVALID_APP_PAYLOAD,