LP#2009281 Recent patrons duplication issues user/dbriem/lp2009281_recent_patrons_duplicates
authorDan Briem <dbriem@wlsmail.org>
Wed, 10 May 2023 15:04:27 +0000 (15:04 +0000)
committerDan Briem <dbriem@wlsmail.org>
Wed, 10 May 2023 15:04:27 +0000 (15:04 +0000)
Converts the user ID to a number to avoid duplicates caused
by type mismatches.

Removes duplicates before the ID is added or the array is
trimmed to avoid prematurely removing IDs.

To test:
1. Set "Number of Retrievable Recent Patrons" setting to 2
2. Open a patron record
3. Open a different patron record
4. Open Circulation->Retrieve Last Patron
5. Open Circulation->Retrieve Recent Patrons
   Note: both patrons are present without duplicates

Signed-off-by: Dan Briem <dbriem@wlsmail.org>
Open-ILS/src/eg2/src/app/staff/circ/patron/patron.service.ts
Open-ILS/web/js/ui/default/staff/services/patron_search.js

index 2b69f59..b476002 100644 (file)
@@ -112,16 +112,13 @@ export class PatronContextService {
         .then(num => {
             if (num) { this.maxRecentPatrons = num; }
 
-            const patrons: number[] =
+            let patrons: number[] =
                 this.store.getLoginSessionItem('eg.circ.recent_patrons') || [];
 
+            // remove potential existing duplicates
+            patrons = patrons.filter(id => patronId !== id);
             patrons.splice(0, 0, patronId);  // put this user at front
-            patrons.splice(this.maxRecentPatrons, 1); // remove excess
-
-            // remove any other occurrences of this user, which may have been
-            // added before the most recent user.
-            const idx = patrons.indexOf(patronId, 1);
-            if (idx > 0) { patrons.splice(idx, 1); }
+            patrons.splice(this.maxRecentPatrons); // remove excess
 
             this.store.setLoginSessionItem('eg.circ.recent_patrons', patrons);
         });
index d2ebe6c..798bd9d 100644 (file)
@@ -134,18 +134,21 @@ function($q , $timeout , $location , egCore,  egUser , egConfirmDialog , $locale
     service.addRecentPatron = function(user_id) {
         if (service.maxRecentPatrons < 1) return;
 
+        // ensure ID is a number if pulled from route data
+        user_id = Number(user_id);
+
         // no need to re-track same user
         if (service.current && service.current.id() == user_id) return;
 
         var patrons = 
             egCore.hatch.getLoginSessionItem('eg.circ.recent_patrons') || [];
-        patrons.splice(0, 0, user_id);  // put this user at front
-        patrons.splice(service.maxRecentPatrons, 1); // remove excess
 
-        // remove any other occurrences of this user, which may have been
-        // added before the most recent user.
-        var idx = patrons.indexOf(user_id, 1);
-        if (idx > 0) patrons.splice(idx, 1);
+        // remove potential existing duplicates
+        patrons = patrons.filter(function(id) {
+            return user_id !== id
+        });
+        patrons.splice(0, 0, user_id);  // put this user at front
+        patrons.splice(service.maxRecentPatrons); // remove excess
 
         egCore.hatch.setLoginSessionItem('eg.circ.recent_patrons', patrons);
     }