diff --git a/lib/http-routes/api/create-call.js b/lib/http-routes/api/create-call.js index a1432ece..55e5bc72 100644 --- a/lib/http-routes/api/create-call.js +++ b/lib/http-routes/api/create-call.js @@ -91,8 +91,13 @@ router.post('/', async(req, res) => { * check if from-number matches any existing numbers on Jambonz * */ if (target.type === 'phone' && !target.trunk) { - const {lookupCarrierByPhoneNumber} = dbUtils(this.logger, srf); - const voip_carrier_sid = await lookupCarrierByPhoneNumber(req.body.account_sid, restDial.from); + const {lookupCarrierByPhoneNumber, lookupCarrierByLcr} = dbUtils(this.logger, srf); + // firstly LCR + let voip_carrier_sid = await lookupCarrierByLcr(req.body.account_sid, to); + if (!voip_carrier_sid) { + // later by phone + voip_carrier_sid = await lookupCarrierByPhoneNumber(req.body.account_sid, restDial.from); + } logger.info( `createCall: selected ${voip_carrier_sid} for requested phone number: ${restDial.from || 'unspecified'})`); if (voip_carrier_sid) { diff --git a/lib/tasks/dial.js b/lib/tasks/dial.js index 4fcbb053..ba465531 100644 --- a/lib/tasks/dial.js +++ b/lib/tasks/dial.js @@ -399,7 +399,7 @@ class TaskDial extends Task { const {req, srf} = cs; const {getSBC} = srf.locals; const {lookupTeamsByAccount, lookupAccountBySid} = srf.locals.dbHelpers; - const {lookupCarrier, lookupCarrierByPhoneNumber} = dbUtils(this.logger, cs.srf); + const {lookupCarrier, lookupCarrierByPhoneNumber, lookupCarrierByLcr} = dbUtils(this.logger, cs.srf); const sbcAddress = this.proxy || getSBC(); const teamsInfo = {}; let fqdn; @@ -467,10 +467,10 @@ class TaskDial extends Task { /** * trunk isn't specified, - * check if number matches any existing numbers + * check if number matches any LCR routes * */ if (t.type === 'phone' && !t.trunk) { - const voip_carrier_sid = await lookupCarrierByPhoneNumber(req.body.account_sid, t.number); + const voip_carrier_sid = lookupCarrierByLcr(req.body.account_sid, t.number); this.logger.info( `Dial:_attemptCalls: selected ${voip_carrier_sid} for requested phone number: ${t.number})`); if (voip_carrier_sid) { diff --git a/lib/utils/db-utils.js b/lib/utils/db-utils.js index f3eb1744..9f65d9c2 100644 --- a/lib/utils/db-utils.js +++ b/lib/utils/db-utils.js @@ -150,10 +150,40 @@ module.exports = (logger, srf) => { } }; + const sqlQueryLcrByAccountSid = `SELECT lcr_sid FROM lcr WHERE account_sid = ? OR + service_provider_sid = (SELECT service_provider_sid from accounts where account_sid = ?)`; + const sqlQueryLcrRouteByLcrSid = 'SELECT * FROM lcr_routes WHERE lcr_sid = ? ORDER BY priority'; + const sqlQueryLcrCarrierSetEntryByLcrRouteSid = 'SELECT * FROM lcr_carrier_set_entry WHERE lcr_route_sid = ? ORDER BY priority' + const lookupCarrierByLcr = async(account_sid, toNumber) => { + const pp = pool.promise(); + try { + const[lcrs] = await pp.query(sqlQueryLcrByAccountSid, [account_sid, account_sid]); + if (lcrs.length) { + const lcr_sid = lcrs[0]; + const [lcr_routes] = await pp.query(sqlQueryLcrRouteByLcrSid, [lcr_sid]); + if (lcr_routes.length) { + for (const r of lcr_routes) { + var matcher = new RegExp(r.regex); + if (matcher.test(toNumber)) { + const [entries] = await pp.query(sqlQueryLcrCarrierSetEntryByLcrRouteSid, [r.lcr_route_sid]); + // Currently just support first entry; + if(entries.length) { + return entries[0].voip_carrier_sid; + } + } + } + } + } + } catch (error) { + logger.error({err}, `lookupCarrierByLcr: Error ${account_sid}:${toNumber}`); + } + } + return { lookupAccountDetails, updateSpeechCredentialLastUsed, lookupCarrier, - lookupCarrierByPhoneNumber + lookupCarrierByPhoneNumber, + lookupCarrierByLcr }; };