Apply dependencies update
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
index 1b95f341e0ca8f5d05190e30dcdf996a3cc6a467..f7fe64bf5c7db4603e4b23751cd9d134ac4ac632 100644 (file)
@@ -25,9 +25,6 @@ export default class Configuration {
   }
 
   static getPerformanceStorage(): StorageConfiguration {
-    const defaultJSONFilePathURI = `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`;
-    const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
-    const defaultSQLiteFilePathURI = `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`;
     let storageConfiguration: StorageConfiguration;
     if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) {
       storageConfiguration =
@@ -36,14 +33,14 @@ export default class Configuration {
         ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'type') ? { type: Configuration.getConfig().performanceStorage.type } : { type: StorageType.JSON_FILE },
         ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'URI')
           ? { URI: Configuration.getConfig().performanceStorage.URI }
-          : { URI: (Configuration.getConfig().performanceStorage.type === StorageType.JSON_FILE) ? defaultJSONFilePathURI : defaultSQLiteFilePathURI }
+          : { URI: this.getDefaultPerformanceStorageURI(Configuration.getConfig()?.performanceStorage?.type ?? StorageType.JSON_FILE) }
       };
     } else {
       storageConfiguration =
       {
         enabled: false,
         type: StorageType.JSON_FILE,
-        URI: defaultJSONFilePathURI
+        URI: this.getDefaultPerformanceStorageURI(StorageType.JSON_FILE)
       };
     }
     return storageConfiguration;
@@ -178,6 +175,18 @@ export default class Configuration {
     }
   }
 
+  private static getDefaultPerformanceStorageURI(storageType: StorageType) {
+    const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
+    switch (storageType) {
+      case StorageType.JSON_FILE:
+        return `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`;
+      case StorageType.SQLITE:
+        return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`;
+      default:
+        throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`);
+    }
+  }
+
   private static objectHasOwnProperty(object: any, property: string): boolean {
     return Object.prototype.hasOwnProperty.call(object, property) as boolean;
   }