refactor: cleanup null checks and helpers
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationConfigurationUtils.ts
index f3f043cfb4a2bf509f0ac11699571d1bbd13e973..b56411e310b13e32473afb4a3bc3290a786f87cc 100644 (file)
@@ -1,10 +1,20 @@
-import type { ChargingStation } from './internal';
+import type { ChargingStation } from './ChargingStation';
 import type { ConfigurationKey, ConfigurationKeyType } from '../types';
-import { Constants, logger } from '../utils';
+import { logger } from '../utils';
 
-type ConfigurationKeyOptions = { readonly?: boolean; visible?: boolean; reboot?: boolean };
-type DeleteConfigurationKeyParams = { save?: boolean; caseInsensitive?: boolean };
-type AddConfigurationKeyParams = { overwrite?: boolean; save?: boolean };
+interface ConfigurationKeyOptions {
+  readonly?: boolean;
+  visible?: boolean;
+  reboot?: boolean;
+}
+interface DeleteConfigurationKeyParams {
+  save?: boolean;
+  caseInsensitive?: boolean;
+}
+interface AddConfigurationKeyParams {
+  overwrite?: boolean;
+  save?: boolean;
+}
 
 export class ChargingStationConfigurationUtils {
   private constructor() {
@@ -14,7 +24,7 @@ export class ChargingStationConfigurationUtils {
   public static getConfigurationKey(
     chargingStation: ChargingStation,
     key: ConfigurationKeyType,
-    caseInsensitive = false
+    caseInsensitive = false,
   ): ConfigurationKey | undefined {
     return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => {
       if (caseInsensitive) {
@@ -33,12 +43,17 @@ export class ChargingStationConfigurationUtils {
       visible: true,
       reboot: false,
     },
-    params: AddConfigurationKeyParams = { overwrite: false, save: false }
+    params: AddConfigurationKeyParams = { overwrite: false, save: false },
   ): void {
-    options = options ?? ({} as ConfigurationKeyOptions);
-    options.readonly = options?.readonly ?? false;
-    options.visible = options?.visible ?? true;
-    options.reboot = options?.reboot ?? false;
+    options = {
+      ...{
+        readonly: false,
+        visible: true,
+        reboot: false,
+      },
+      ...options,
+    };
+    params = { ...{ overwrite: false, save: false }, ...params };
     let keyFound = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key);
     if (keyFound && params?.overwrite) {
       ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, {
@@ -49,7 +64,7 @@ export class ChargingStationConfigurationUtils {
     if (!keyFound) {
       chargingStation.ocppConfiguration?.configurationKey?.push({
         key,
-        readonly: options.readonly,
+        readonly: options.readonly!,
         value,
         visible: options.visible,
         reboot: options.reboot,
@@ -58,7 +73,7 @@ export class ChargingStationConfigurationUtils {
     } else {
       logger.error(
         `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
-        keyFound
+        keyFound,
       );
     }
   }
@@ -67,22 +82,22 @@ export class ChargingStationConfigurationUtils {
     chargingStation: ChargingStation,
     key: ConfigurationKeyType,
     value: string,
-    caseInsensitive = false
+    caseInsensitive = false,
   ): void {
     const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
       chargingStation,
       key,
-      caseInsensitive
+      caseInsensitive,
     );
     if (keyFound) {
-      chargingStation.ocppConfiguration.configurationKey[
-        chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound)
+      chargingStation.ocppConfiguration!.configurationKey![
+        chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound)
       ].value = value;
       chargingStation.saveOcppConfiguration();
     } else {
       logger.error(
         `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
-        { key, value }
+        { key, value },
       );
     }
   }
@@ -90,17 +105,18 @@ export class ChargingStationConfigurationUtils {
   public static deleteConfigurationKey(
     chargingStation: ChargingStation,
     key: ConfigurationKeyType,
-    params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false }
+    params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false },
   ): ConfigurationKey[] | undefined {
+    params = { ...{ save: true, caseInsensitive: false }, ...params };
     const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
       chargingStation,
       key,
-      params?.caseInsensitive
+      params?.caseInsensitive,
     );
     if (keyFound) {
       const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice(
         chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
-        1
+        1,
       );
       params?.save && chargingStation.saveOcppConfiguration();
       return deletedConfigurationKey;