mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-01-25 02:08:24 +00:00
revamp db - refactor webhooks
This commit is contained in:
@@ -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',
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user