refactor: add sanity checks at adding charging station(s)
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 7 Mar 2024 14:18:22 +0000 (15:18 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 7 Mar 2024 14:18:22 +0000 (15:18 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts

index 80470d00f3e5069dcf498111f32cc8b28f009a02..40d451ed5e0218bbddc7c31b88aa2e8db1f5d692 100644 (file)
@@ -539,7 +539,7 @@ export class Bootstrap extends EventEmitter {
   ): Promise<void> {
     if (!this.started && !this.starting) {
       throw new BaseError(
-        'Cannot add charging station while charging stations simulator is not started'
+        'Cannot add charging station while charging stations simulator is not started'
       )
     }
     await this.workerImplementation?.addElement({
index de83c6b7e4eb85adaa3e2d5297393482c3d8feb6..07bb2cdb150888d9623d60eb1355531a67c24e71 100644 (file)
@@ -242,6 +242,19 @@ export abstract class AbstractUIService {
       numberOfStations: number
       options?: ChargingStationOptions
     }
+    if (!Bootstrap.getInstance().getState().started) {
+      return {
+        status: ResponseStatus.FAILURE,
+        errorMessage:
+          'Cannot add charging station(s) while the charging stations simulator is not started'
+      } satisfies ResponsePayload
+    }
+    if (typeof template !== 'string' || typeof numberOfStations !== 'number') {
+      return {
+        status: ResponseStatus.FAILURE,
+        errorMessage: 'Invalid request payload'
+      } satisfies ResponsePayload
+    }
     if (!this.uiServer.chargingStationTemplates.has(template)) {
       return {
         status: ResponseStatus.FAILURE,