diff --git a/lib/sip-trunk-register.js b/lib/sip-trunk-register.js index cd585c9..2c8958b 100644 --- a/lib/sip-trunk-register.js +++ b/lib/sip-trunk-register.js @@ -210,17 +210,8 @@ const updateCarrierRegbots = async(logger, srf) => { } if (JSON.stringify(gws) !== JSON.stringify(gateways)) hasChanged = true; - // find account_sip_realm for each gateway if it associated with an account - for (const gw of gws) { - if (gw.carrier.account_sid) { - const account = await lookupAccountBySid(gw.carrier.account_sid); - if (account && account.sip_realm) { - gw.account_sip_realm = account.sip_realm; - } - } - } - if (hasChanged) { + debug('updateCarrierRegbots: got new or changed carriers'); logger.info('updateCarrierRegbots: got new or changed carriers'); carriers.length = 0; @@ -235,7 +226,17 @@ const updateCarrierRegbots = async(logger, srf) => { // start new regbots let batch_count = 0; - for (const gw of getUniqueGateways(gateways, logger)) { + const uniqueGateways = getUniqueGateways(gateways, logger); + // find account_sip_realm for each gateway if it associated with an account + for (const gw of uniqueGateways) { + if (gw.carrier.account_sid) { + const account = await lookupAccountBySid(gw.carrier.account_sid); + if (account && account.sip_realm) { + gw.account_sip_realm = account.sip_realm; + } + } + } + for (const gw of uniqueGateways) { try { const rb = new Regbot(logger, { voip_carrier_sid: gw.carrier.voip_carrier_sid,