mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-24 21:12:10 +00:00
revamp db - refactor webhooks
This commit is contained in:
+55
-14
@@ -1,8 +1,35 @@
|
||||
const Model = require('./model');
|
||||
const {getMysqlConnection} = require('../db');
|
||||
const listSqlSp = 'SELECT * from accounts WHERE service_provider_sid = ?';
|
||||
const listSqlAccount = 'SELECT * from accounts WHERE account_sid = ?';
|
||||
const retrieveSql = 'SELECT * from accounts WHERE service_provider_sid = ? AND account_sid = ?';
|
||||
|
||||
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`;
|
||||
|
||||
function transmogrifyResults(results) {
|
||||
return results.map((row) => {
|
||||
const obj = row.acc;
|
||||
if (row.rh && Object.keys(row.rh).length && row.rh.url !== null) {
|
||||
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;
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
class Account extends Model {
|
||||
constructor() {
|
||||
@@ -13,16 +40,24 @@ class Account extends Model {
|
||||
* list all accounts
|
||||
*/
|
||||
static retrieveAll(service_provider_sid, account_sid) {
|
||||
if (!service_provider_sid && !account_sid) return super.retrieveAll();
|
||||
let sql = retrieveSql;
|
||||
const args = [];
|
||||
if (account_sid) {
|
||||
sql = `${sql} WHERE acc.account_sid = ?`;
|
||||
args.push(account_sid);
|
||||
}
|
||||
else if (service_provider_sid) {
|
||||
sql = `${sql} WHERE acc.service_provider_sid = ?`;
|
||||
args.push(service_provider_sid);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
getMysqlConnection((err, conn) => {
|
||||
if (err) return reject(err);
|
||||
const sql = account_sid ? listSqlAccount : listSqlSp;
|
||||
const args = account_sid ? [account_sid] : [service_provider_sid];
|
||||
conn.query(sql, args, (err, results, fields) => {
|
||||
conn.query({sql, nestTables: true}, args, (err, results, fields) => {
|
||||
conn.release();
|
||||
if (err) return reject(err);
|
||||
resolve(results);
|
||||
const r = transmogrifyResults(results);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -32,14 +67,20 @@ class Account extends Model {
|
||||
* retrieve an account
|
||||
*/
|
||||
static retrieve(sid, service_provider_sid) {
|
||||
if (!service_provider_sid) return super.retrieve(sid);
|
||||
const args = [sid];
|
||||
let sql = `${retrieveSql} WHERE acc.account_sid = ?`;
|
||||
if (service_provider_sid) {
|
||||
sql = `${retrieveSql} WHERE acc.account_sid = ? AND acc.service_provider_sid = ?`;
|
||||
args.push(service_provider_sid);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
getMysqlConnection((err, conn) => {
|
||||
if (err) return reject(err);
|
||||
conn.query(retrieveSql, [service_provider_sid, sid], (err, results, fields) => {
|
||||
conn.query({sql, nestTables: true}, args, (err, results, fields) => {
|
||||
conn.release();
|
||||
if (err) return reject(err);
|
||||
resolve(results);
|
||||
const r = transmogrifyResults(results);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -69,15 +110,15 @@ Account.fields = [
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'registration_hook',
|
||||
name: 'registration_hook_sid',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'hook_basic_auth_user',
|
||||
name: 'device_calling_hook_sid',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'hook_basic_auth_password',
|
||||
name: 'error_hook_sid',
|
||||
type: 'string',
|
||||
}
|
||||
];
|
||||
|
||||
+51
-24
@@ -19,6 +19,29 @@ SELECT * from applications
|
||||
WHERE account_sid = ?
|
||||
AND application_sid = ?`;
|
||||
|
||||
const retrieveSql = `SELECT * from applications app
|
||||
LEFT JOIN webhooks AS ch
|
||||
ON app.call_hook_sid = ch.webhook_sid
|
||||
LEFT JOIN webhooks AS sh
|
||||
ON app.call_status_hook_sid = sh.webhook_sid`;
|
||||
|
||||
function transmogrifyResults(results) {
|
||||
return results.map((row) => {
|
||||
const obj = row.app;
|
||||
if (row.ch && Object.keys(row.ch).length && row.ch.url !== null) {
|
||||
Object.assign(obj, {call_hook: row.ch});
|
||||
}
|
||||
else obj.call_hook = null;
|
||||
if (row.sh && Object.keys(row.sh).length && row.sh.url !== null) {
|
||||
Object.assign(obj, {call_status_hook: row.sh});
|
||||
}
|
||||
else obj.call_status_hook = null;
|
||||
delete obj.call_hook_sid;
|
||||
delete obj.call_status_hook_sid;
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
|
||||
class Application extends Model {
|
||||
constructor() {
|
||||
super();
|
||||
@@ -28,16 +51,24 @@ class Application extends Model {
|
||||
* list all applications - for all service providers, for one service provider, or for one account
|
||||
*/
|
||||
static retrieveAll(service_provider_sid, account_sid) {
|
||||
if (!service_provider_sid && !account_sid) return super.retrieveAll();
|
||||
let sql = retrieveSql;
|
||||
const args = [];
|
||||
if (account_sid) {
|
||||
sql = `${sql} WHERE app.account_sid = ?`;
|
||||
args.push(account_sid);
|
||||
}
|
||||
else if (service_provider_sid) {
|
||||
sql = `${sql} WHERE account_sid in (SELECT account_sid from accounts WHERE service_provider_sid = ?)`;
|
||||
args.push(service_provider_sid);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
getMysqlConnection((err, conn) => {
|
||||
if (err) return reject(err);
|
||||
const sql = account_sid ? listSqlAccount : listSqlSp;
|
||||
const args = account_sid ? [account_sid] : [service_provider_sid];
|
||||
conn.query(sql, args, (err, results, fields) => {
|
||||
conn.query({sql, nestTables: true}, args, (err, results, fields) => {
|
||||
conn.release();
|
||||
if (err) return reject(err);
|
||||
resolve(results);
|
||||
const r = transmogrifyResults(results);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -47,16 +78,24 @@ class Application extends Model {
|
||||
* retrieve an application
|
||||
*/
|
||||
static retrieve(sid, service_provider_sid, account_sid) {
|
||||
if (!service_provider_sid && !account_sid) return super.retrieve(sid);
|
||||
const args = [sid];
|
||||
let sql = `${retrieveSql} WHERE app.application_sid = ?`;
|
||||
if (account_sid) {
|
||||
sql = `${sql} AND app.account_sid = ?`;
|
||||
args.push(account_sid);
|
||||
}
|
||||
if (service_provider_sid) {
|
||||
sql = `${sql} AND account_sid in (SELECT account_sid from accounts WHERE service_provider_sid = ?)`;
|
||||
args.push(service_provider_sid);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
getMysqlConnection((err, conn) => {
|
||||
if (err) return reject(err);
|
||||
const sql = account_sid ? retrieveSqlAccount : retrieveSqlSp;
|
||||
const args = account_sid ? [account_sid, sid] : [service_provider_sid, sid];
|
||||
conn.query(sql, args, (err, results, fields) => {
|
||||
conn.query({sql, nestTables: true}, args, (err, results, fields) => {
|
||||
conn.release();
|
||||
if (err) return reject(err);
|
||||
resolve(results);
|
||||
const r = transmogrifyResults(results);
|
||||
resolve(r);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -82,26 +121,14 @@ Application.fields = [
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'call_hook',
|
||||
name: 'call_hook_sid',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'call_status_hook',
|
||||
name: 'call_status_hook_sid',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'hook_basic_auth_user',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'hook_basic_auth_password',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
name: 'hook_http_method',
|
||||
type: 'string',
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
const Model = require('./model');
|
||||
|
||||
class Webhook extends Model {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
Webhook.table = 'webhooks';
|
||||
Webhook.fields = [
|
||||
{
|
||||
name: 'webhook_sid',
|
||||
type: 'string',
|
||||
primaryKey: true
|
||||
},
|
||||
{
|
||||
name: 'url',
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'method',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'username',
|
||||
type: 'string'
|
||||
},
|
||||
{
|
||||
name: 'password',
|
||||
type: 'string'
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = Webhook;
|
||||
Reference in New Issue
Block a user