From 29cc5259c42f868c5926516815ac11a35fdf296d Mon Sep 17 00:00:00 2001
From: juanf <juanf@ethz.ch>
Date: Tue, 16 May 2023 13:36:01 +0200
Subject: [PATCH] SSDM-12100: corner case, avoid reading same object twice

---
 .../eln-lims/html/js/util/BarcodeUtil.js      | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
index 85f6cc0ae00..feb7010ae61 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/BarcodeUtil.js
@@ -486,6 +486,12 @@ var BarcodeUtil = new function() {
         // Add local event
         var objects = [];
         var gatherReaded = function(object) {
+            // Avoid adding same item twice, check if is already on the list.
+            for(var oIdx = 0; oIdx < objects.length; oIdx++) {
+                if(objects[oIdx].identifier.identifier === object.identifier.identifier) {
+                    return; // Do nothing if the same objet is on the list
+                }
+            }
             objects.push(object);
             var displayName = "";
             var $container = $('<div>');
@@ -704,10 +710,17 @@ var BarcodeUtil = new function() {
 
         BarcodeUtil.readBarcodeFromScannerOrCamera($readerContainer, function(permId, error) {
             console.log(permId);
-            if(isScanner) {
-                return; //Scanner already types on the fields, do nothing
+            // We try to find the permId on the fields, if is not already we add it, this handles three corner cases:
+            //  - Scanner already typed on the field -> It avoids to repeat it
+            //  - Scanner not focused on fields -> It adds it to the field
+            //  - Camera doesn't act as a keyboard -> it adds to the field
+            for(var eIdx = 0; eIdx < $barcodeReaders.length; eIdx++) {
+                var $barcodeReader = $barcodeReaders[eIdx];
+                var value = $barcodeReader.val();
+                if(value === permId) {
+                    return; // Do nothing if the same objet is on the list
+                }
             }
-
             // Camera needs this code to set the permId on the first non-empty reader
             for(var eIdx = 0; eIdx < $barcodeReaders.length; eIdx++) {
                 var $barcodeReader = $barcodeReaders[eIdx];
-- 
GitLab