update to 0.3.2 jambonz-db-helpers

This commit is contained in:
Dave Horton
2020-02-15 15:42:32 -05:00
parent 0ddf434554
commit 4df6f1e115
8 changed files with 32 additions and 48 deletions
+2 -2
View File
@@ -27,7 +27,7 @@ const {
port: process.env.JAMBONES_REDIS_PORT || 6379
}, logger);
const {
lookupApplicationBySid,
lookupAppBySid,
lookupAccountBySid
} = require('jambonz-db-helpers')({
host: process.env.JAMBONES_MYSQL_HOST,
@@ -47,7 +47,7 @@ Object.assign(app.locals, {
deleteCall,
listCalls,
purgeCalls,
lookupApplicationBySid,
lookupAppBySid,
lookupAccountBySid
});
+7 -4
View File
@@ -1,5 +1,6 @@
/* SQLEditor (MySQL (2))*/
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `call_routes`;
@@ -15,14 +16,16 @@ DROP TABLE IF EXISTS `sip_gateways`;
DROP TABLE IF EXISTS `voip_carriers`;
DROP TABLE IF EXISTS `applications`;
DROP TABLE IF EXISTS `accounts`;
DROP TABLE IF EXISTS `applications`;
DROP TABLE IF EXISTS `service_providers`;
DROP TABLE IF EXISTS `webhooks`;
SET FOREIGN_KEY_CHECKS = 1;
CREATE TABLE IF NOT EXISTS `call_routes`
(
`call_route_sid` CHAR(36) NOT NULL UNIQUE ,
@@ -134,7 +137,7 @@ CREATE TABLE IF NOT EXISTS `accounts`
`sip_realm` VARCHAR(132) UNIQUE COMMENT 'sip domain that will be used for devices registering under this account',
`service_provider_sid` CHAR(36) NOT NULL COMMENT 'service provider that owns the customer relationship with this account',
`registration_hook_sid` CHAR(36) COMMENT 'webhook to call when devices underr this account attempt to register',
`device_calling_hook_sid` CHAR(36) COMMENT 'webhook to call when devices under this account place outbound calls',
`device_calling_application_sid` CHAR(36) COMMENT 'application to use for outbound calling from an account',
`is_active` BOOLEAN NOT NULL DEFAULT true,
PRIMARY KEY (`account_sid`)
) ENGINE=InnoDB COMMENT='An enterprise that uses the platform for comm services';
@@ -198,4 +201,4 @@ ALTER TABLE `accounts` ADD FOREIGN KEY service_provider_sid_idxfk_1 (`service_pr
ALTER TABLE `accounts` ADD FOREIGN KEY registration_hook_sid_idxfk_1 (`registration_hook_sid`) REFERENCES `webhooks` (`webhook_sid`);
ALTER TABLE `accounts` ADD FOREIGN KEY device_calling_hook_sid_idxfk (`device_calling_hook_sid`) REFERENCES `webhooks` (`webhook_sid`);
ALTER TABLE `accounts` ADD FOREIGN KEY device_calling_application_sid_idxfk (`device_calling_application_sid`) REFERENCES `applications` (`application_sid`);
+14 -14
View File
@@ -401,17 +401,17 @@
<uid><![CDATA[A75FAB8E-C2A1-4A05-A09E-6FF454109B6F]]></uid>
</SQLField>
<SQLField>
<name><![CDATA[device_calling_hook_sid]]></name>
<name><![CDATA[device_calling_application_sid]]></name>
<type><![CDATA[CHAR(36)]]></type>
<referencesField>webhook_sid</referencesField>
<referencesTable>webhooks</referencesTable>
<referencesField><![CDATA[webhook_sid]]></referencesField>
<referencesTable><![CDATA[webhooks]]></referencesTable>
<referencesField>application_sid</referencesField>
<referencesTable>applications</referencesTable>
<referencesField><![CDATA[application_sid]]></referencesField>
<referencesTable><![CDATA[applications]]></referencesTable>
<sourceCardinality>4</sourceCardinality>
<destinationCardinality>2</destinationCardinality>
<referencesFieldUID><![CDATA[E046BA30-BC18-483C-A5C8-766E7160F574]]></referencesFieldUID>
<referencesTableUID><![CDATA[64D64CB9-0990-4C68-BE71-F9FD43C2BE19]]></referencesTableUID>
<objectComment><![CDATA[webhook to call when devices under this account place outbound calls]]></objectComment>
<referencesFieldUID><![CDATA[EF943D13-DCB0-43C1-B03F-550612E20F9D]]></referencesFieldUID>
<referencesTableUID><![CDATA[E97EE4F0-7ED7-4E8C-862E-D98192D6EAE0]]></referencesTableUID>
<objectComment><![CDATA[application to use for outbound calling from an account]]></objectComment>
<uid><![CDATA[36C13087-E026-4F6F-A409-A082F7ADE1B6]]></uid>
</SQLField>
<SQLField>
@@ -850,17 +850,17 @@
<overviewPanelHidden><![CDATA[0]]></overviewPanelHidden>
<pageBoundariesVisible><![CDATA[0]]></pageBoundariesVisible>
<PageGridVisible><![CDATA[0]]></PageGridVisible>
<RightSidebarWidth><![CDATA[1403.000000]]></RightSidebarWidth>
<RightSidebarWidth><![CDATA[2146.000000]]></RightSidebarWidth>
<sidebarIndex><![CDATA[2]]></sidebarIndex>
<snapToGrid><![CDATA[0]]></snapToGrid>
<SourceSidebarWidth><![CDATA[0.000000]]></SourceSidebarWidth>
<SQLEditorFileFormatVersion><![CDATA[4]]></SQLEditorFileFormatVersion>
<uid><![CDATA[58C99A00-06C9-478C-A667-C63842E088F3]]></uid>
<windowHeight><![CDATA[1027.000000]]></windowHeight>
<windowLocationX><![CDATA[0.000000]]></windowLocationX>
<windowLocationY><![CDATA[0.000000]]></windowLocationY>
<windowScrollOrigin><![CDATA[{0, 17.5}]]></windowScrollOrigin>
<windowWidth><![CDATA[1680.000000]]></windowWidth>
<windowHeight><![CDATA[1322.000000]]></windowHeight>
<windowLocationX><![CDATA[2553.000000]]></windowLocationX>
<windowLocationY><![CDATA[95.000000]]></windowLocationY>
<windowScrollOrigin><![CDATA[{0, 0}]]></windowScrollOrigin>
<windowWidth><![CDATA[2423.000000]]></windowWidth>
</SQLDocumentInfo>
<AllowsIndexRenamingOnInsert><![CDATA[1]]></AllowsIndexRenamingOnInsert>
<defaultLabelExpanded><![CDATA[1]]></defaultLabelExpanded>
+2 -9
View File
@@ -3,9 +3,7 @@ const {getMysqlConnection} = require('../db');
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`;
ON acc.registration_hook_sid = rh.webhook_sid`;
function transmogrifyResults(results) {
return results.map((row) => {
@@ -14,12 +12,7 @@ function transmogrifyResults(results) {
Object.assign(obj, {registration_hook: row.rh});
}
else obj.registration_hook = null;
if (row.dh && Object.keys(row.dh).length && row.dh.url !== null) {
Object.assign(obj, {device_calling_hook: row.dh});
}
else obj.device_calling_hook = null;
delete obj.registration_hook_sid;
delete obj.device_calling_hook_sid;
return obj;
});
}
@@ -107,7 +100,7 @@ Account.fields = [
type: 'string',
},
{
name: 'device_calling_hook_sid',
name: 'device_calling_application_sid',
type: 'string',
}
];
+2 -5
View File
@@ -137,9 +137,6 @@ async function validateAdd(req) {
if (req.body.registration_hook && typeof req.body.registration_hook !== 'object') {
throw new DbErrorBadRequest('\'registration_hook\' must be an object when adding an account');
}
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');
}
}
async function validateUpdate(req, sid) {
if (req.user.hasAccountAuth && req.user.account_sid !== sid) {
@@ -178,7 +175,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']) {
for (const prop of ['registration_hook']) {
if (obj[prop]) {
obj[`${prop}_sid`] = await Webhook.make(obj[prop]);
delete obj[prop];
@@ -227,7 +224,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']) {
for (const prop of ['registration_hook']) {
if (prop in obj && Object.keys(obj[prop]).length) {
if ('webhook_sid' in obj[prop]) {
const sid = obj[prop]['webhook_sid'];
+2 -2
View File
@@ -9,12 +9,12 @@ const preconditions = {
};
async function validate(req) {
const {lookupApplicationBySid, lookupAccountBySid} = req.app.locals;
const {lookupAppBySid, 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);
const application = await lookupAppBySid(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');
+1 -1
View File
@@ -17,7 +17,7 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"jambonz-db-helpers": "^0.2.3",
"jambonz-db-helpers": "^0.3.2",
"jambonz-realtimedb-helpers": "0.1.6",
"mysql2": "^2.0.2",
"passport": "^0.4.0",
+2 -11
View File
@@ -32,12 +32,6 @@ test('account tests', async(t) => {
registration_hook: {
url: 'http://example.com/reg',
method: 'get'
},
device_calling_hook: {
url: 'http://example.com/device',
method: 'get',
username: 'foo',
password: 'abr'
}
}
});
@@ -50,10 +44,8 @@ test('account tests', async(t) => {
json: true,
});
let regHook = result[0].registration_hook;
let devHook = result[0].device_calling_hook;
t.ok(result.length === 1 &&
Object.keys(regHook).length == 5 &&
Object.keys(devHook).length === 5, 'successfully queried all accounts');
Object.keys(regHook).length == 5, 'successfully queried all accounts');
/* query one accounts */
result = await request.get(`/Accounts/${sid}`, {
@@ -82,8 +74,7 @@ test('account tests', async(t) => {
json: true,
});
//console.log(`retrieved account after update: ${JSON.stringify(result)}`);
t.ok(result.device_calling_hook === null &&
Object.keys(result.registration_hook).length === 5, 'successfully removed a hook from account');
t.ok(Object.keys(result.registration_hook).length === 5, 'successfully removed a hook from account');
/* assign phone number to account */
result = await request.put(`/PhoneNumbers/${phone_number_sid}`, {