build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ConfigurationKeyUtils.ts
index 39606b66e1ea8d08cacc61b53d58607d53d9c73d..4b9eb40aa211f9c5d5c651aae0203bc8f330fb74 100644 (file)
-import type { ChargingStation } from './ChargingStation';
-import type { ConfigurationKey, ConfigurationKeyType } from '../types';
-import { logger } from '../utils';
+import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js'
+import { logger } from '../utils/index.js'
+import type { ChargingStation } from './ChargingStation.js'
 
 interface ConfigurationKeyOptions {
-  readonly?: boolean;
-  visible?: boolean;
-  reboot?: boolean;
+  readonly?: boolean
+  visible?: boolean
+  reboot?: boolean
 }
 interface DeleteConfigurationKeyParams {
-  save?: boolean;
-  caseInsensitive?: boolean;
+  save?: boolean
+  caseInsensitive?: boolean
 }
 interface AddConfigurationKeyParams {
-  overwrite?: boolean;
-  save?: boolean;
+  overwrite?: boolean
+  save?: boolean
 }
 
 export const getConfigurationKey = (
   chargingStation: ChargingStation,
   key: ConfigurationKeyType,
-  caseInsensitive = false,
+  caseInsensitive = false
 ): ConfigurationKey | undefined => {
-  return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => {
+  return chargingStation.ocppConfiguration?.configurationKey?.find(configElement => {
     if (caseInsensitive) {
-      return configElement.key.toLowerCase() === key.toLowerCase();
+      return configElement.key.toLowerCase() === key.toLowerCase()
     }
-    return configElement.key === key;
-  });
-};
+    return configElement.key === key
+  })
+}
 
 export const addConfigurationKey = (
   chargingStation: ChargingStation,
   key: ConfigurationKeyType,
   value: string,
-  options: ConfigurationKeyOptions = {
-    readonly: false,
-    visible: true,
-    reboot: false,
-  },
-  params: AddConfigurationKeyParams = { overwrite: false, save: false },
+  options?: ConfigurationKeyOptions,
+  params?: AddConfigurationKeyParams
 ): void => {
   options = {
     ...{
       readonly: false,
       visible: true,
-      reboot: false,
+      reboot: false
     },
-    ...options,
-  };
-  params = { ...{ overwrite: false, save: false }, ...params };
-  let keyFound = getConfigurationKey(chargingStation, key);
-  if (keyFound && params?.overwrite) {
+    ...options
+  }
+  params = { ...{ overwrite: false, save: false }, ...params }
+  let keyFound = getConfigurationKey(chargingStation, key)
+  if (keyFound != null && params.overwrite === true) {
     deleteConfigurationKey(chargingStation, keyFound.key, {
-      save: false,
-    });
-    keyFound = undefined;
+      save: false
+    })
+    keyFound = undefined
   }
-  if (!keyFound) {
+  if (keyFound == null) {
     chargingStation.ocppConfiguration?.configurationKey?.push({
       key,
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       readonly: options.readonly!,
       value,
       visible: options.visible,
-      reboot: options.reboot,
-    });
-    params?.save && chargingStation.saveOcppConfiguration();
+      reboot: options.reboot
+    })
+    params.save === true && chargingStation.saveOcppConfiguration()
   } else {
     logger.error(
       `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
-      keyFound,
-    );
+      keyFound
+    )
   }
-};
+}
 
 export const setConfigurationKeyValue = (
   chargingStation: ChargingStation,
   key: ConfigurationKeyType,
   value: string,
-  caseInsensitive = false,
-): void => {
-  const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive);
-  if (keyFound) {
+  caseInsensitive = false
+): ConfigurationKey | undefined => {
+  const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive)
+  if (keyFound != null) {
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     chargingStation.ocppConfiguration!.configurationKey![
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound)
-    ].value = value;
-    chargingStation.saveOcppConfiguration();
+    ].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 }
+    )
   }
-};
+  return keyFound
+}
 
 export const deleteConfigurationKey = (
   chargingStation: ChargingStation,
   key: ConfigurationKeyType,
-  params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false },
+  params?: DeleteConfigurationKeyParams
 ): ConfigurationKey[] | undefined => {
-  params = { ...{ save: true, caseInsensitive: false }, ...params };
-  const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive);
-  if (keyFound) {
+  params = { ...{ save: true, caseInsensitive: false }, ...params }
+  const keyFound = getConfigurationKey(chargingStation, key, params.caseInsensitive)
+  if (keyFound != null) {
     const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice(
       chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
-      1,
-    );
-    params?.save && chargingStation.saveOcppConfiguration();
-    return deletedConfigurationKey;
+      1
+    )
+    params.save === true && chargingStation.saveOcppConfiguration()
+    return deletedConfigurationKey
   }
-};
+}