Simplify stop transaction payload handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / UIServiceWorkerBroadcastChannel.ts
index a2b209220ae084ba84e1fa7accda1e782443929b..650a8b9e77905aa7049ce292b609762b4b7b5f4b 100644 (file)
@@ -29,7 +29,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
   }
 
   private responseHandler(messageEvent: MessageEvent): void {
-    if (this.isRequest(messageEvent.data)) {
+    if (this.isRequest(messageEvent.data) === true) {
       return;
     }
     const [uuid, responsePayload] = this.validateMessageEvent(messageEvent)
@@ -41,16 +41,14 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
         responses: [responsePayload],
       });
     } else if (
-      this.responses.get(uuid)?.responsesReceived + 1 <
-      this.responses.get(uuid)?.responsesExpected
+      this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected
     ) {
       this.responses.get(uuid).responsesReceived++;
       this.responses.get(uuid).responses.push(responsePayload);
-    } else if (
-      this.responses.get(uuid)?.responsesReceived + 1 ===
-      this.responses.get(uuid)?.responsesExpected
+    }
+    if (
+      this.responses.get(uuid)?.responsesReceived === this.responses.get(uuid)?.responsesExpected
     ) {
-      this.responses.get(uuid).responses.push(responsePayload);
       this.uiService.sendResponse(uuid, this.buildResponsePayload(uuid));
       this.responses.delete(uuid);
       this.uiService.deleteBroadcastChannelRequest(uuid);
@@ -60,7 +58,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
   private buildResponsePayload(uuid: string): ResponsePayload {
     const responsesStatus = this.responses
       .get(uuid)
-      ?.responses.every((response) => response.status === ResponseStatus.SUCCESS)
+      ?.responses.every(({ status }) => status === ResponseStatus.SUCCESS)
       ? ResponseStatus.SUCCESS
       : ResponseStatus.FAILURE;
     return {
@@ -83,6 +81,16 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
           })
           .filter((hashId) => hashId !== undefined),
       }),
+      ...(responsesStatus === ResponseStatus.FAILURE && {
+        responsesFailed: this.responses
+          .get(uuid)
+          ?.responses.map((response) => {
+            if (response.status === ResponseStatus.FAILURE) {
+              return response;
+            }
+          })
+          .filter((response) => response !== undefined),
+      }),
     };
   }