support for VoipCarriers that are customer PBXs

This commit is contained in:
Dave Horton
2020-02-08 10:39:17 -05:00
parent 1c9afcdf59
commit 9783afcff5
12 changed files with 271 additions and 799 deletions

View File

@@ -5,9 +5,7 @@ const retrieveSql = `SELECT * from accounts acc
LEFT JOIN webhooks AS rh
ON acc.registration_hook_sid = rh.webhook_sid
LEFT JOIN webhooks AS dh
ON acc.device_calling_hook_sid = dh.webhook_sid
LEFT JOIN webhooks AS eh
ON acc.error_hook_sid = eh.webhook_sid`;
ON acc.device_calling_hook_sid = dh.webhook_sid`;
function transmogrifyResults(results) {
return results.map((row) => {
@@ -20,13 +18,8 @@ function transmogrifyResults(results) {
Object.assign(obj, {device_calling_hook: row.dh});
}
else obj.device_calling_hook = null;
if (row.eh && Object.keys(row.eh).length && row.eh.url !== null) {
Object.assign(obj, {error_hook: row.eh});
}
else obj.error_hook = null;
delete obj.registration_hook_sid;
delete obj.device_calling_hook_sid;
delete obj.error_hook_sid;
return obj;
});
}
@@ -116,10 +109,6 @@ Account.fields = [
{
name: 'device_calling_hook_sid',
type: 'string',
},
{
name: 'error_hook_sid',
type: 'string',
}
];

View File

@@ -37,6 +37,14 @@ SipGateway.fields = [
{
name: 'is_active',
type: 'number'
},
{
name: 'account_sid',
type: 'string'
},
{
name: 'application_sid',
type: 'string'
}
];

View File

@@ -45,7 +45,7 @@ function validateTo(to) {
throw new DbErrorBadRequest(`missing or invalid to property: ${JSON.stringify(to)}`);
}
async function validateCreateCall(logger, sid, req) {
const {lookupApplicationBySid} = require('jambonz-db-helpers')(config.get('mysql'), logger);
const {lookupApplicationBySid} = req.app.locals;
const obj = req.body;
if (req.user.account_sid !== sid) throw new DbErrorBadRequest(`unauthorized createCall request for account ${sid}`);
@@ -108,10 +108,6 @@ async function validateAdd(req) {
if (req.body.device_calling_hook && typeof req.body.device_calling_hook !== 'object') {
throw new DbErrorBadRequest('\'device_calling_hook\' must be an object when adding an account');
}
if (req.body.error_hook && typeof req.body.error_hook !== 'object') {
throw new DbErrorBadRequest('\'error_hook\' must be an object when adding an account');
}
}
async function validateUpdate(req, sid) {
if (req.user.hasAccountAuth && req.user.account_sid !== sid) {
@@ -150,7 +146,7 @@ router.post('/', async(req, res) => {
// create webhooks if provided
const obj = Object.assign({}, req.body);
for (const prop of ['registration_hook', 'device_calling_hook', 'error_hook']) {
for (const prop of ['registration_hook', 'device_calling_hook']) {
if (obj[prop]) {
obj[`${prop}_sid`] = await Webhook.make(obj[prop]);
delete obj[prop];
@@ -199,7 +195,7 @@ router.put('/:sid', async(req, res) => {
try {
// create webhooks if provided
const obj = Object.assign({}, req.body);
for (const prop of ['registration_hook', 'device_calling_hook', 'error_hook']) {
for (const prop of ['registration_hook', 'device_calling_hook']) {
if (prop in obj && Object.keys(obj[prop]).length) {
if ('webhook_sid' in obj[prop]) {
const sid = obj[prop]['webhook_sid'];

View File

@@ -3,7 +3,6 @@ const SipGateway = require('../../models/sip-gateway');
const decorate = require('./decorate');
const preconditions = {};
decorate(router, SipGateway, ['*'], preconditions);
module.exports = router;

View File

@@ -1,11 +1,31 @@
const router = require('express').Router();
const {DbErrorUnprocessableRequest} = require('../../utils/errors');
const {DbErrorBadRequest, DbErrorUnprocessableRequest} = require('../../utils/errors');
const VoipCarrier = require('../../models/voip-carrier');
const decorate = require('./decorate');
const preconditions = {
'add': validate,
'update': validate,
'delete': noActiveAccounts
};
async function validate(req) {
const {lookupApplicationBySid, lookupAccountBySid} = req.app.locals;
if (req.body.application_sid && !req.body.account_sid) {
throw new DbErrorBadRequest('account_sid missing');
}
if (req.body.application_sid) {
const application = await lookupApplicationBySid(req.body.application_sid);
if (!application) throw new DbErrorBadRequest('unknown application_sid');
if (application.account_sid !== req.body.account_sid) {
throw new DbErrorBadRequest('application_sid does not exist for specified account_sid');
}
}
else if (req.body.account_sid) {
const account = await lookupAccountBySid(req.body.account_sid);
if (!account) throw new DbErrorBadRequest('unknown account_sid');
}
}
/* can not delete a voip provider if it has any active phone numbers */
async function noActiveAccounts(req, sid) {
const activeAccounts = await VoipCarrier.getForeignKeyReferences('phone_numbers.voip_carrier_sid', sid);