From 18f484348b18fab8c41fb6b779eb9b8af4a0f5e4 Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Thu, 18 Mar 2021 17:17:59 +0100 Subject: [PATCH] Store and Load Config File automatically. --- modules/wamo/src/wamo.carrierMapper.module.ts | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/modules/wamo/src/wamo.carrierMapper.module.ts b/modules/wamo/src/wamo.carrierMapper.module.ts index b623fbb..63b5b23 100644 --- a/modules/wamo/src/wamo.carrierMapper.module.ts +++ b/modules/wamo/src/wamo.carrierMapper.module.ts @@ -8,11 +8,18 @@ /* eslint-disable no-fallthrough */ +import { readFile } from "fs/promises"; import { injectable } from "inversify"; +import { join } from "path"; import { exportMethod, exportProperty } from "../../../lib/decorators/moduleDecorators"; +import { + createFile, + createPath, + exists +} from "../../../lib/helpers/fileMethods"; import { getNopeLogger } from "../../../lib/logger/getLogger"; import { InjectableNopeBaseModule } from "../../../lib/module/BaseModule.injectable"; import { NopeObservable } from "../../../lib/observables/nopeObservable"; @@ -53,6 +60,7 @@ export class WaMOCarrierMapper }) public carriers = new NopeObservable(); protected _carriers = new Set(); + protected _file: string; /** * The Logger of the Module. @@ -77,11 +85,60 @@ export class WaMOCarrierMapper }; this._logger = getNopeLogger("wamo-carrier-mapper-" + this.identifier); - this.mapping.setContent({}); + + // Create a temporary File to store the Elements + const _path = join(process.cwd(), "temp"); + await createPath(_path); + + // Define the File Name. It is based on the identifier. + this._file = join(_path, this.identifier + "_file.json"); + + // Now Test if the File exists: + if (!(await exists(this._file))) { + + this._logger.warn("No configuration File defined. Creating a new one under:",this._file); + + this.carriers.setContent([]); + this.mapping.setContent({}); + + // Create an Empty File. + await this.storeConfig(); + } + + // We now try to load the configuration + const loadedConfig = JSON.parse( + await readFile(this._file, { + encoding: "utf-8" + }) + ); + + // Load the Mapping and register the Carriers. + for (const carrier in loadedConfig.carriers){ + await this.registerCarrier(carrier); + } + // Add the Products to the Carrier. + for (const carrier in loadedConfig.mapping){ + await this.addProductToCarrier(carrier, loadedConfig.mapping[carrier]); + } await super.init(); } + /** + * Function, wich will + * + * @memberof WaMOCarrierMapper + */ + @exportMethod({ + paramsHasNoCallback: true + }) + public async storeConfig(){ + await createFile(this._file, JSON.stringify({ + carriers: this.carriers.getContent(), + mapping: this.mapping.getContent() + })); + } + /** * Helper Function to Store new Carriers. If the carrier * is already present => Nothing happens @@ -93,16 +150,33 @@ export class WaMOCarrierMapper @exportMethod({ paramsHasNoCallback: true }) - public async registerCarrier(carrier: string) { + public async registerCarrier(carrier: string): Promise { // Add the Carrier if required: if (!this._carriers.has(carrier)) { this._carriers.add(carrier); this.carriers.setContent(Array.from(this._carriers)); this._logger.info(`New Carrier with ID "${carrier}" has been detected`); + + // Update the Config. + await this.storeConfig(); } } + /** + * Function, to check if there exists a mapping for the carrier or not. + * + * @param {string} carrier + * @return {*} {Promise} + * @memberof WaMOCarrierMapper + */ + @exportMethod({ + paramsHasNoCallback: true + }) + public async isProductLinkedToCarrier(carrier: string): Promise { + return this.mapping.getContent()[carrier] !== undefined; + } + @exportMethod({ paramsHasNoCallback: true }) @@ -112,6 +186,9 @@ export class WaMOCarrierMapper // Delete all Carriers this._carriers.clear(); this.carriers.setContent([]); + + // Update the Config. + await this.storeConfig(); } @exportMethod({ @@ -132,6 +209,9 @@ export class WaMOCarrierMapper // Add the Carrier await this.registerCarrier(carrier); + + // Update the Config. + await this.storeConfig(); } @exportMethod({ @@ -146,6 +226,9 @@ export class WaMOCarrierMapper // Add the Carrier await this.registerCarrier(carrier); + + // Update the Config. + await this.storeConfig(); } @exportMethod({