LP#1526185 Disable second toast on permfail
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / op_change.js
index 3f233a7..e7bf1aa 100644 (file)
@@ -11,66 +11,96 @@ angular.module('egCoreMod')
        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
 
        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
 
-       var service = {};
+    var service = {};
 
 
-
-       service.changeOperator = function(calledFromNavbar, failedRequest) {
-               var _op_changed = false;
-        $uibModal.open({
+    // Returns a promise resolved upon successful op-change.
+    // Rejected otherwise.
+    service.changeOperator = function(permEvt) {
+        return $uibModal.open({
             templateUrl: './share/t_opchange',
             controller:
                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
                 $scope.args = {username : '', password : '', type : 'temp'};
             templateUrl: './share/t_opchange',
             controller:
                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
                 $scope.args = {username : '', password : '', type : 'temp'};
-                $scope.displayTypeField = calledFromNavbar;
                 $scope.title = egStrings.OP_CHANGE_TITLE;
                 $scope.title = egStrings.OP_CHANGE_TITLE;
-                if(failedRequest) {
-                    $scope.title = failedRequest.perm_evt.desc + ": "
-                        + failedRequest.perm_evt.ilsperm;
+                if (permEvt) {
+                    $scope.title = permEvt.desc + ": " + permEvt.ilsperm;
                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
-                    console.log($scope.message);
+                } else {
+                    $scope.displayTypeField = true;
                 }
                 $scope.focus = true;
                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
             }]
         }).result.then(function (args) {
                 }
                 $scope.focus = true;
                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
             }]
         }).result.then(function (args) {
-            if (!args || !args.username || !args.password) return;
+            if (!args || !args.username || !args.password)
+                return $q.reject();
+
             args.type = args.type || 'temp';
             args.workstation = egAuth.workstation();
             args.type = args.type || 'temp';
             args.workstation = egAuth.workstation();
-            egAuth.opChange(args).then(
+            return egAuth.opChange(args).then(
                 function() {
                 function() {
-                    _op_changed = true;
-                    if(failedRequest) {
-                        console.log(js2JSON(failedRequest));
-                        egNet.request(
-                            failedRequest.service,
-                            failedRequest.method,
-                            egAuth.token(),
-                            failedRequest.params[1]
-                        ).then(service.changeOperatorUndo());
+                    console.debug('op-change succeeded');
+                    if (permEvt) {
+                        ngToast.create(egStrings.PERM_OP_CHANGE_SUCCESS);
                     } else {
                         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
                     }
                 },
                 function() {
                     } else {
                         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
                     }
                 },
                 function() {
-                    ngToast.warning(egStrings.OP_CHANGE_FAILURE);
+                    console.debug('op-change failed');
+                    if (permEvt) {
+                        ngToast.warning(egStrings.PERM_OP_CHANGE_FAILURE);
+                    } else {
+                        ngToast.warning(egStrings.OP_CHANGE_FAILURE);
+                    }
                 }
             );
         });
                 }
             );
         });
-        return _op_changed;
     }
 
     }
 
-    service.changeOperatorUndo = function() {
-        egAuth.opChangeUndo();
-        var _op_changed = false;
-        ngToast.create(egStrings.OP_CHANGE_SUCCESS);
-        return _op_changed;
+    // Returns a promise resolved on successful op-change undo.
+    service.changeOperatorUndo = function(hideToast) {
+        return egAuth.opChangeUndo().then(
+            function() {
+                console.debug('op-change undo succeeded');
+                if (!hideToast) ngToast.create(egStrings.OP_CHANGE_SUCCESS);
+            },
+            function() {
+                console.debug('op-change undo failed');
+                if (!hideToast) ngToast.warning(egStrings.OP_CHANGE_FAILURE);
+            }
+        );
     }
 
     }
 
-    //Check for any permission failure broadcasts. then call changeOperator and retry the action
-    $rootScope.$on('egNetPermFailure', function(args, request_info) {
-        var op_changed = service.changeOperator(false, request_info);
-    })
+    // Tell egNet to use our permission failure handler,
+    // since we know how to launch a login override dialog.
+    //
+    // 1. Launch the change-operator dialog
+    // 2. If op-change succeeds, re-do the failed request using the
+    //    op-change'd authtoken.
+    // 3. Undo the op-change.
+    //
+    // Returns a promise resolved along with the re-ran request.
+    egNet.handlePermFailure = function(request) {
+        console.debug("perm override required for "+request.method);
+
+        return service.changeOperator(request.evt).then(function() {
+
+            return egNet.requestWithParamList(
+                request.service,
+                request.method,
+                // original params, but replace the failed authtoken
+                // with the op-change'd authtoken
+                [egAuth.token()].concat(request.params.splice(1))
+
+            )['finally'](function() {
+                // always undo the operator change after a perm override.
+                console.debug("clearing op-change after perm override redo");
+                service.changeOperatorUndo(true);
+            });
+        });
+    }
 
 
-       return service;
+    return service;
 }])
 }])