Skip to content
Next Next commit
converted lookup-auth to use sequelize
  • Loading branch information
radicaldrew committed Nov 7, 2024
commit a5dd213e26d2bc0d1fb3cdbc50aaaac1d7f7c129
21 changes: 17 additions & 4 deletions lib/lookup-auth-hook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const debug = require('debug')('jambonz:db-helpers');
const {sequelize, QueryTypes } = require('sequelize');

const accountSql = `SELECT * from accounts app, webhooks rh
WHERE app.registration_hook_sid = rh.webhook_sid
AND app.sip_realm = ?`;
Expand All @@ -15,8 +17,11 @@ AND sp.root_domain = ?`;
* @param {*} sipRealm - sip realm/domain to search for
*/
async function lookupAuthHook(pool, logger, sipRealm) {
const pp = pool.promise();
const [r] = await pp.execute({sql: accountSql, nestTables: true}, [sipRealm]);
const r = await sequelize.query(accountSql, {
nest: true,
type: QueryTypes.SELECT,
replacements: [sipRealm]
});
debug(`results from querying account for sip realm ${sipRealm}: ${JSON.stringify(r)}`);
if (r.length > 0 && r[0].rh) {
return r[0].rh;
Expand All @@ -31,7 +36,11 @@ async function lookupAuthHook(pool, logger, sipRealm) {
if (parts.length > 2) {
parts.shift();
const superDomain = parts.join('.');
const [r] = await pp.execute({sql: accountSql, nestTables: true}, [superDomain]);
const r = await sequelize.query(accountSql, {
nest: true,
type: QueryTypes.SELECT,
replacements: [superDomain]
});
debug(`results from querying account for sip realm ${superDomain}: ${JSON.stringify(r)}`);
if (r.length > 0 && r[0].rh) {
return r[0].rh;
Expand All @@ -43,7 +52,11 @@ async function lookupAuthHook(pool, logger, sipRealm) {

const rootDomain = arr[1];
debug(`did not find hook at account level, checking service provider for ${rootDomain}`);
const [r2] = await pp.execute({sql: spSql, nestTables: true}, [rootDomain]);
const r2 = await sequelize.query(spSql, {
nest: true,
type: QueryTypes.SELECT,
replacements: [rootDomain]
});
debug(`results from querying service_providers for root domain ${rootDomain}: ${JSON.stringify(r2)}`);
if (r2.length > 0 && r2[0].rh) {
return r2[0].rh;
Expand Down