LP#1378383-WebStaff Fix Item circ history to obey max setting
authorCesar Velez <Cesar.Velez@equinoxinitiative.org>
Tue, 18 Jul 2017 17:56:53 +0000 (18:56 +0100)
committerCesar Velez <cesar.velez@equinoxinitiative.org>
Wed, 19 Jul 2017 15:51:15 +0000 (11:51 -0400)
The circ.item_checkout_history.max lib setting should be respected
in both Circ History tabs, and also check for the permission to
VIEW_COPY_CHECKOUT_HISTORY for the current user.

Signed-off by: Cesar Velez <cesar.velez@equinoxinitiative.org>

Open-ILS/web/js/ui/default/staff/cat/item/app.js

index 002061e..8f34c67 100644 (file)
@@ -1424,61 +1424,80 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         return deferred.promise;
     }
 
-    // if loadPrev load the two most recent circulations
-    function loadCurrentCirc(loadPrev) {
+    // load two most recent circulations in /circs tab
+    function loadCurrentCirc() {
         delete $scope.circ;
         delete $scope.circ_summary;
         delete $scope.prev_circ_summary;
         delete $scope.prev_circ_usr;
         if (!copyId) return;
         
-        egCore.pcrud.search('circ', 
-            {target_copy : copyId},
-            {   flesh : 2,
-                flesh_fields : {
-                    circ : [
-                        'usr',
-                        'workstation',                                         
-                        'checkin_workstation',                                 
-                        'duration_rule',                                       
-                        'max_fine_rule',                                       
-                        'recurring_fine_rule'   
-                    ],
-                    au : ['card']
-                },
-                order_by : {circ : 'xact_start desc'}, 
-                limit :  1
+        var copy_org =
+            itemSvc.copy.call_number().id() == -1 ?
+            itemSvc.copy.circ_lib().id() :
+            itemSvc.copy.call_number().owning_lib().id();
+
+        // since a user can still view patron checkout history here, check perms
+        egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
+        .then(function(orgIds){
+            if(orgIds.indexOf(copy_org) == -1){
+                console.warn('User is not allowed to view circ history!');
+                return $q.when(0);
             }
 
-        ).then(null, null, function(circ) {
-            $scope.circ = circ;
+            return fetchMaxCircHistory();
+        })
+        .then(function(maxHistCount){
+            egCore.pcrud.search('circ',
+                {target_copy : copyId},
+                {   flesh : 2,
+                    flesh_fields : {
+                        circ : [
+                            'usr',
+                            'workstation',
+                            'checkin_workstation',
+                            'duration_rule',
+                            'max_fine_rule',
+                            'recurring_fine_rule'
+                        ],
+                        au : ['card']
+                    },
+                    order_by : {circ : 'xact_start desc'},
+                    limit :  maxHistCount
+                }
 
-            // load the chain for this circ
-            egCore.net.request(
-                'open-ils.circ',
-                'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
-                egCore.auth.token(), $scope.circ.id()
-            ).then(function(summary) {
-                $scope.circ_summary = summary.summary;
-            });
+            ).then(null, null, function(circ) {
+                $scope.circ = circ;
 
-            if (!loadPrev) return;
+                if (!circ) return $q.when();
 
-            // load the chain for the previous circ, plus the user
-            egCore.net.request(
-                'open-ils.circ',
-                'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
-                egCore.auth.token(), $scope.circ.id()
+                // load the chain for this circ
+                egCore.net.request(
+                    'open-ils.circ',
+                    'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
+                    egCore.auth.token(), $scope.circ.id()
+                ).then(function(summary) {
+                    $scope.circ_summary = summary.summary;
+                });
 
-            ).then(null, null, function(summary) {
-                $scope.prev_circ_summary = summary.summary;
+                if (maxHistCount <= 1) return;
 
-                if (summary.usr) { // aged circs have no 'usr'.
-                    egCore.pcrud.retrieve('au', summary.usr,
-                        {flesh : 1, flesh_fields : {au : ['card']}})
+                // load the chain for the previous circ, plus the user
+                egCore.net.request(
+                    'open-ils.circ',
+                    'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
+                    egCore.auth.token(), $scope.circ.id()
 
-                    .then(function(user) { $scope.prev_circ_usr = user });
-                }
+                ).then(null, null, function(summary) {
+                    $scope.prev_circ_summary = summary.summary;
+
+                    if (summary.usr) { // aged circs have no 'usr'.
+                        egCore.pcrud.retrieve('au', summary.usr,
+                            {flesh : 1, flesh_fields : {au : ['card']}})
+
+                        .then(function(user) { $scope.prev_circ_usr = user });
+                    }
+                });
             });
         });
     }
@@ -1517,6 +1536,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
         });
     }
 
+    // load data for /circ_list tab
     function loadCircHistory() {
         $scope.circ_list = [];
 
@@ -1654,7 +1674,7 @@ function($scope , $q , $location , $routeParams , $timeout , $window , egCore ,
                 break;
 
             case 'circs':
-                loadCurrentCirc(true);
+                loadCurrentCirc();
                 break;
 
             case 'circ_list':