Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
updated libraries to use postgres and updated config
  • Loading branch information
radicaldrew committed Nov 17, 2024
commit 0c349eefebe33ab3475d88d60505645dc96d7f05
7 changes: 7 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@
"user": "jambones_write_test",
"database": "jambones_test",
"password": "jambones_test"
},
"write-postgres": {
"host": "localhost",
"dialect": "postgres",
"user": "postgres",
"database": "jambones_test",
"password": "jambones_test"
}
}
15 changes: 11 additions & 4 deletions lib/clean-sbc-addresses.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const debug = require('debug')('jambonz:db-helpers');
const { QueryTypes } = require('sequelize');

const dialect = process.env.JAMBONES_DB_DIALECT || 'mysql';
/**
* Clean the sbc address after long time no update
* @param {*} pool
Expand All @@ -9,9 +9,16 @@ const { QueryTypes } = require('sequelize');
*/
async function cleanSbcAddresses(pool, logger) {
try {
const sql = `DELETE FROM sbc_addresses WHERE last_updated IS NULL OR
last_updated < DATE_SUB(NOW(), INTERVAL
'${process.env.DEAD_SBC_IN_SECOND || 3600}' SECOND)`;
let sql;
if (dialect == 'mysql') {
sql = `DELETE FROM sbc_addresses WHERE last_updated IS NULL OR
last_updated < DATE_SUB(NOW(), INTERVAL '${process.env.DEAD_SBC_IN_SECOND || 3600}' SECOND)`;
} else {
sql = `DELETE FROM sbc_addresses WHERE last_updated IS NULL OR
last_updated < NOW() - INTERVAL '${process.env.DEAD_SBC_IN_SECOND || 3600} second'`;
}


const r = await pool.query(sql, { type: QueryTypes.DELETE });
debug(`results from cleaning for sbc address ${JSON.stringify(r)}`);
} catch (err) {
Expand Down
28 changes: 14 additions & 14 deletions lib/lookup-app-by-phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ const { QueryTypes } = require('sequelize');

const sql =
`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
LEFT JOIN webhooks AS mh ON app.messaging_hook_sid = mh.webhook_sid
WHERE app.application_sid = (
SELECT application_sid from phone_numbers where number = ?
)`;
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
//LEFT JOIN webhooks AS mh ON app.messaging_hook_sid = mh.webhook_sid
//WHERE app.application_sid = (
// SELECT application_sid from phone_numbers where number = ?
//)`;

const sqlWithCarrier =
`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
LEFT JOIN webhooks AS mh ON app.messaging_hook_sid = mh.webhook_sid
WHERE app.application_sid = (
SELECT application_sid from phone_numbers where number = ? AND voip_carrier_sid = ?
)`;
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
//LEFT JOIN webhooks AS mh ON app.messaging_hook_sid = mh.webhook_sid
//WHERE app.application_sid = (
// SELECT application_sid from phone_numbers where number = ? AND voip_carrier_sid = ?
//)`;

const sqlCallRoutes =
`SELECT cr.regex, cr.application_sid
Expand Down
5 changes: 4 additions & 1 deletion lib/lookup-app-by-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ async function lookupAppByRegex(pool, logger, phoneNumber, account_sid) {
});

debug(`lookupAppByRegex: got regex for ${account_sid}: ${JSON.stringify(callRoutes)}`);
const selectedRoute = callRoutes.find((cr) => RegExp(cr.regex).test(phoneNumber));
debug('callRoutes', callRoutes);

const selectedRoute = callRoutes.find((cr) =>
RegExp(cr.regex.replace('\\\\', '\\')).test(phoneNumber)); // @todo validate other regex examples
if (selectedRoute) {
logger.debug({selectedRoute}, `lookupAppByRegex: returning based on regex match for ${phoneNumber}`);
return await lookupAppBySid(selectedRoute.application_sid);
Expand Down
11 changes: 10 additions & 1 deletion lib/lookup-carrier-by-sid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const debug = require('debug')('jambonz:db-helpers');
const { QueryTypes } = require('sequelize');
const dialect = process.env.JAMBONES_DB_DIALECT || 'mysql';

const sql =
`SELECT *
Expand All @@ -19,7 +20,15 @@ async function lookupCarrierBySid(pool, logger, voip_carrier_sid) {
});
debug(`results: ${JSON.stringify(r)}`);
if (r.length > 0) {
return r[0];
if ([r[0]['register_status'] !== '']) {
if (dialect == 'mysql') {
return r[0];
} else {
r[0]['register_status'] = JSON.stringify(r[0]['register_status']);
return r[0];
}
}

}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/lookup-system-information.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sql = 'SELECT * FROM system_information';

async function lookupSystemInformation(pool, logger) {
const pp = pool.promise();
const [r] = await pp.execute(sql);
const [r] = await pp.query(sql);
debug(`results: ${JSON.stringify(r)}`);
return r.length > 0 ? r[0] : null;
}
Expand Down