{"size": 3063, "ext": "js", "max_stars_repo_path": "public/setup/SetupController.js", "max_stars_repo_name": "theryankennedy/twilio-contact-center", "max_stars_repo_stars_event_min_datetime": "2016-11-21T21:15:01.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-24T22:46:21.000Z", "max_issues_repo_path": "public/setup/SetupController.js", "max_issues_repo_name": "joeaa17/call-center-twilio", "max_issues_repo_issues_event_min_datetime": "2016-11-23T14:25:36.000Z", "max_issues_repo_issues_event_max_datetime": "2016-11-23T14:25:36.000Z", "max_forks_repo_path": "public/setup/SetupController.js", "max_forks_repo_name": "joeaa17/call-center-twilio", "max_forks_repo_forks_event_min_datetime": "2016-10-04T03:11:56.000Z", "max_forks_repo_forks_event_max_datetime": "2019-09-22T18:24:11.000Z"}
{"max_stars_count": 5, "max_issues_count": 1, "max_forks_count": 6, "avg_line_length": 28.3611111111, "max_line_length": 141, "alphanum_fraction": 0.6225922298}
var app = angular.module('setupApplication', ['ngMessages']);

app.controller('SetupController', function ($scope, $http, $q) {

  $scope.phoneNumber    = { isValid: true, message: null, code: null};
  $scope.isSaving       = false;
  $scope.configuration  = null;
  $scope.workspace      = null;
  $scope.activities     = [];

  $scope.init = function(){

    var retrieveSetup = function() {
      var deferred = $q.defer();

      $http.get('/api/setup').then(function(response){
        $scope.configuration = response.data;
        deferred.resolve();
      }, function(response) {
        deferred.reject('The application could not access the configuration');
      });
      return deferred.promise;

    };

    var retrieveActivities = function() {
      var deferred = $q.defer();

      $http.get('/api/setup/activities').then(function(response){
        $scope.activities = response.data;
        deferred.resolve();
      }, function(response) {
        deferred.reject('The application could not access the your Twilio TaskRouter workspace activities please verify the Workspace Sid.');
      });
      return deferred.promise;

    };

    var retrieveWorkspace = function() {
      var deferred = $q.defer();

      $http.get('/api/setup/workspace').then(function(response){
        $scope.workspace = response.data;
        deferred.resolve();
      }, function(response) {
        deferred.reject('The application could not access the your Twilio Taskrouter workspace please verify the Workspace Sid.');
      });
      return deferred.promise;

    };

    $q.all([retrieveSetup(), retrieveActivities(), retrieveWorkspace()])
      .then(function(data) {})
      .catch(function(err) {
        alert(err);
      });

  };

  $scope.saveConfig = function(){
    $scope.phoneNumber.isValid = true;
    $scope.isSaving = true;

    var verifyPhoneNumber = function() {
      var deferred = $q.defer();

      $http.post('/api/validate/phone-number', { callerId: $scope.configuration.twilio.callerId }).then(function(response){
        deferred.resolve(response.data);
      }, function(error) {
        deferred.reject(error);
      });

      return deferred.promise;

    };

    var saveConfiguration = function(sid, configuration) {
      var deferred = $q.defer();

      $http.post('/api/setup', { sid: sid, configuration: configuration }).then(function(response){
        deferred.resolve();
      }, function(error) {
        deferred.reject(error);
      });

      return deferred.promise;

    };

    /* verify phone number and save configuration */
    verifyPhoneNumber().then(function(phoneNumber){

      return saveConfiguration(phoneNumber.sid, $scope.configuration).then(function(){
        $scope.isSaving = false;
        $scope.phoneNumber = { isValid: true, message: null, code: null};
      });

    }).catch(function(error) {
      $scope.phoneNumber.isValid = false;
      $scope.phoneNumber.code = error.data.code;
      $scope.phoneNumber.message = error.data.message;
      $scope.isSaving = false;
    });

  };

});
