feat: lcr

This commit is contained in:
Quan HL
2023-04-24 21:40:19 +07:00
parent 6dc019e836
commit 2e3d783dee
3 changed files with 41 additions and 6 deletions

View File

@@ -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
};
};