LP#1526185 Disable second toast on permfail
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / op_change.js
1 angular.module('egCoreMod')
2
3 /**
4  * egPermLoginDialog.open(
5  *  open("some message goes {{here}}", {
6  *  here : 'foo', ok : function() {}, cancel : function() {}},
7  *  'OK', 'Cancel');
8  */
9 .factory('egOpChange',
10
11        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
12 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
13
14     var service = {};
15
16     // Returns a promise resolved upon successful op-change.
17     // Rejected otherwise.
18     service.changeOperator = function(permEvt) {
19         return $uibModal.open({
20             templateUrl: './share/t_opchange',
21             controller:
22                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
23                 $scope.args = {username : '', password : '', type : 'temp'};
24                 $scope.title = egStrings.OP_CHANGE_TITLE;
25                 if (permEvt) {
26                     $scope.title = permEvt.desc + ": " + permEvt.ilsperm;
27                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
28                 } else {
29                     $scope.displayTypeField = true;
30                 }
31                 $scope.focus = true;
32                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
33                 $scope.cancel = function () { $uibModalInstance.dismiss() }
34             }]
35         }).result.then(function (args) {
36             if (!args || !args.username || !args.password)
37                 return $q.reject();
38
39             args.type = args.type || 'temp';
40             args.workstation = egAuth.workstation();
41             return egAuth.opChange(args).then(
42                 function() {
43                     console.debug('op-change succeeded');
44                     if (permEvt) {
45                         ngToast.create(egStrings.PERM_OP_CHANGE_SUCCESS);
46                     } else {
47                         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
48                     }
49                 },
50                 function() {
51                     console.debug('op-change failed');
52                     if (permEvt) {
53                         ngToast.warning(egStrings.PERM_OP_CHANGE_FAILURE);
54                     } else {
55                         ngToast.warning(egStrings.OP_CHANGE_FAILURE);
56                     }
57                 }
58             );
59         });
60     }
61
62     // Returns a promise resolved on successful op-change undo.
63     service.changeOperatorUndo = function(hideToast) {
64         return egAuth.opChangeUndo().then(
65             function() {
66                 console.debug('op-change undo succeeded');
67                 if (!hideToast) ngToast.create(egStrings.OP_CHANGE_SUCCESS);
68             },
69             function() {
70                 console.debug('op-change undo failed');
71                 if (!hideToast) ngToast.warning(egStrings.OP_CHANGE_FAILURE);
72             }
73         );
74     }
75
76     // Tell egNet to use our permission failure handler,
77     // since we know how to launch a login override dialog.
78     //
79     // 1. Launch the change-operator dialog
80     // 2. If op-change succeeds, re-do the failed request using the
81     //    op-change'd authtoken.
82     // 3. Undo the op-change.
83     //
84     // Returns a promise resolved along with the re-ran request.
85     egNet.handlePermFailure = function(request) {
86         console.debug("perm override required for "+request.method);
87
88         return service.changeOperator(request.evt).then(function() {
89
90             return egNet.requestWithParamList(
91                 request.service,
92                 request.method,
93                 // original params, but replace the failed authtoken
94                 // with the op-change'd authtoken
95                 [egAuth.token()].concat(request.params.splice(1))
96
97             )['finally'](function() {
98                 // always undo the operator change after a perm override.
99                 console.debug("clearing op-change after perm override redo");
100                 service.changeOperatorUndo(true);
101             });
102         });
103     }
104
105     return service;
106 }])