fix: handle invalid hashIds in UI server payload
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 30 Nov 2023 18:24:39 +0000 (19:24 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 30 Nov 2023 18:24:39 +0000 (19:24 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ui-server/ui-services/AbstractUIService.ts

index d0478b99573c2da29de611e05dba6a737bf21d84..e0078c19ff1e4d94a5cb6656d651a9013d9ad768 100644 (file)
@@ -140,7 +140,7 @@ export abstract class AbstractUIService {
   }
 
   public getBroadcastChannelExpectedResponses(uuid: string): number {
-    return this.broadcastChannelRequests.get(uuid) ?? 0;
+    return this.broadcastChannelRequests.get(uuid)!;
   }
 
   protected handleProtocolRequest(
@@ -166,21 +166,25 @@ export abstract class AbstractUIService {
           if (hashId !== undefined && this.uiServer.chargingStations.has(hashId) === true) {
             return hashId;
           }
-          const msg = `${this.logPrefix(
-            moduleName,
-            'sendBroadcastChannelRequest',
-          )} Charging station with hashId '${hashId}' not found`;
-          if (payload.hashIds?.length === 1) {
-            throw new BaseError(msg);
-          } else {
-            logger.warn(msg);
-          }
+          logger.warn(
+            `${this.logPrefix(
+              moduleName,
+              'sendBroadcastChannelRequest',
+            )} Charging station with hashId '${hashId}' not found`,
+          );
         })
         ?.filter((hashId) => !isNullOrUndefined(hashId)) as string[];
+    } else {
+      delete payload.hashIds;
     }
-    const expectedNumberOfResponses = isNotEmptyArray(payload.hashIds)
-      ? payload.hashIds!.length
+    const expectedNumberOfResponses = Array.isArray(payload.hashIds)
+      ? payload.hashIds.length
       : this.uiServer.chargingStations.size;
+    if (expectedNumberOfResponses === 0) {
+      throw new BaseError(
+        'hashIds array in the request payload does not contain any valid charging station hashId',
+      );
+    }
     this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]);
     this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses);
   }