I have a primary and secondary sms service for my app. If the primary fails, for any issue, I want to default to the secondary service.
Here is the flow:
However, due to the time frames involved (internet, carrier delays, etc), I need to loop #5 until the carrier receipt comes in...or times out...to determine if it needs to go to the secondary service. Currently I am doing a 3 second timeout delay before querying for #5 - but I would like to convert that to a 1 second loop to ensure that if #4 takes longer than 3 seconds that my #5 will still working correctly.
$scope.sendCode = function() { $scope.errColor = "red" ; $scope.errMsg = "SENDING AUTH CODE" ; var newCode = getRandom(10000,99999) ; //var tktCode = new Date().format("yyyymmdd")+ "_" +getRandom(10000,999999) ; var smsNumber = getDB("user_phone") ; setDB("app_regCode",newCode) ; var msg = "App registration code: "+newCode+ ". Enter this code into the 'VERIFY CODE' input field" ; var tReq = twilio(smsNumber,msg) ;var smsCarrier = getDB("dev_carrier") ; if (!smsCarrier) { smsCarrier = "No Carrier Info" ; } var smsEmail = $scope.userObj.user_email ; if (!smsEmail) { smsEmail = "No Email" ; } $scope.errColor ; $scope.errMsg ; var error = {} ; var smsShortCode = vendorData.twilioShortCode ; var smsLongCode = vendorData.twilioLongCode ; // log message sent to internal API $scope.smsID = logSMS(smsNumber,smsLongCode,smsShortCode,msg,smsCarrier,smsEmail,"Twilio") ; $http(tReq).then(function(response) { $scope.errColor = "green" ; $scope.errMsg = "SENT! Confirming Delivery" ; smsResponse = response.data ; if (smsResponse.sid) { //update smsSent record apiService.all("PUT",[{table:"smsSent",sid:"rid",sidValue:$scope.smsID,soloField:1,dataInfo:{smsTkt:smsResponse.sid}}]) ; } if (smsResponse.status.match(/(queued|accepted|sending|sent)/i)) { //if (sms.success == true) { // need to replace this with a proper 1 second loop // to test for "delivered" carrier receipt // if status still (queued|accepted|sending|sent), then loop until its updated // final receipt statuses are: Delivered, Undelivered, Failed setTimeout(function() { return $http(twilioVerify(smsResponse.uri)) .then(function(response) { // now check status of message to determine if needed to exit or go to back up service //return $http(twilioVerify(smsResponse.uri)) if (response.data.status == "delivered") { $scope.errColor = "green" $scope.errMsg = "Delivery Confirmed" ; messageSent('Primary'); } else { //backup service // determine fail reason, send to secondary if necessary messageFail('Primary') ; $scope.errColor = "red" $scope.errMsg = "Problem detected, trying secondary service" ; secondSMS(smsNumber,msg) ; } }) ; },3000) ; } else { // failed response from Twilio, send to MissionMobile $scope.errColor = "red" ; $scope.errMsg = "Problem detected, attempting secondary service" messageFail('Primary') ; secondarySMS(smsNumber,msg) ; } }, function(thisError) { // failed communication to Twilio, send to MissionMobile $scope.errColor = "red" ; $scope.errMsg = "Issue sending code, attempting secondary service" ; messageFail('Primary') ; secondarySMS(smsNumber,msg) ; }).catch(function(error) { $scope.errColor = error.errColor ; $scope.errMsg = error.errMsg ; }) ;
}
#javascript #angular.js #loops #cordova