From 9334ee4668bddef271d59f722618b1022978efda Mon Sep 17 00:00:00 2001
From: mechtelm <nicht@mehr.fragen>
Date: Mon, 31 May 2021 08:31:47 +0200
Subject: [PATCH] Change appConfig data model

The API will send the appConfig as key value pairing, so we need some transform procedures. The getAppConfig function prepares for super user setting edit.
---
 src/app/config/app.config.ts | 151 ++++++++++++++++++++++++-----------
 1 file changed, 104 insertions(+), 47 deletions(-)

diff --git a/src/app/config/app.config.ts b/src/app/config/app.config.ts
index 7b499e65..66564e14 100644
--- a/src/app/config/app.config.ts
+++ b/src/app/config/app.config.ts
@@ -10,14 +10,7 @@ export interface SysConfig {
   testConfig: KeyValuePairs;
   serverTimestamp: number;
   broadcastingService: BroadCastingServiceInfo;
-  app_title: string;
-  background_body: string;
-  background_box: string;
-  intro_html: string;
-  impressum_html: string;
-  global_warning: string;
-  global_warning_expired_day: Date;
-  global_warning_expired_hour: number;
+  appConfig: Map<string, string>;
 }
 
 export interface BroadCastingServiceInfo {
@@ -43,9 +36,11 @@ export class AppConfig {
   impressum_html = 'Impressum/Datenschutz nicht definiert';
   trusted_impressum_html: SafeUrl = null;
   global_warning = '';
-  global_warning_expired_day: Date;
-  global_warning_expired_hour: number;
+  global_warning_expired_day = '';
+  global_warning_expired_hour = '';
   isValidApiVersion = false;
+  sanitizer: DomSanitizer = null;
+  cts: CustomtextService = null;
 
   constructor(
     sysConfig: SysConfig,
@@ -53,55 +48,103 @@ export class AppConfig {
     expectedApiVersion: string,
     sanitizer: DomSanitizer
   ) {
-    const ctSettings = {};
-    Object.keys(customTextsDefault).forEach(k => {
-      ctSettings[k] = customTextsDefault[k].defaultvalue;
-      if (k === 'app_title') this.app_title = customTextsDefault[k].defaultvalue;
-      if (k === 'app_intro1') {
-        this.intro_html = customTextsDefault[k].defaultvalue;
-        this.impressum_html = customTextsDefault[k].defaultvalue;
-      }
-    });
+    this.sanitizer = sanitizer;
+    this.cts = cts;
     if (sysConfig) {
-      Object.keys(sysConfig.customTexts).forEach(k => {
-        ctSettings[k] = sysConfig.customTexts[k];
-        if (k === 'app_title') this.app_title = sysConfig.customTexts[k];
-        if (k === 'app_intro1') {
-          this.intro_html = sysConfig.customTexts[k];
-          this.impressum_html = sysConfig.customTexts[k];
-        }
-      });
-      if (sysConfig.app_title) this.app_title = sysConfig.app_title;
-      if (sysConfig.mainLogo) this.mainLogo = sysConfig.mainLogo;
-      if (sysConfig.background_body) {
-        this.background_body = sysConfig.background_body;
-        document.documentElement.style.setProperty('--tc-body-background', this.background_body);
-      }
-      if (sysConfig.background_box) {
-        this.background_box = sysConfig.background_box;
-        document.documentElement.style.setProperty('--tc-box-background', this.background_box);
-      }
-      this.isValidApiVersion = AppConfig.checkApiVersion(sysConfig.version, expectedApiVersion);
-      this.detectedApiVersion = sysConfig.version;
-      if (sysConfig.intro_html) this.intro_html = sysConfig.intro_html;
-      if (sysConfig.impressum_html) this.impressum_html = sysConfig.impressum_html;
-      this.global_warning = sysConfig.global_warning;
-      this.global_warning_expired_day = sysConfig.global_warning_expired_day;
-      this.global_warning_expired_hour = sysConfig.global_warning_expired_hour;
+      this.setCustomTexts(sysConfig.customTexts);
+      this.setAppConfig(sysConfig.appConfig);
       this.testConfig = sysConfig.testConfig;
       this.serverTimestamp = sysConfig.serverTimestamp;
       if (sysConfig.broadcastingService && sysConfig.broadcastingService.status) {
         this.broadcastingService = sysConfig.broadcastingService;
       }
+      this.detectedApiVersion = sysConfig.version;
+    } else {
+      this.setCustomTexts(null);
+      this.setAppConfig(null);
     }
-    cts.addCustomTexts(ctSettings);
-    this.trusted_intro_html = sanitizer.bypassSecurityTrustHtml(this.intro_html);
-    this.trusted_impressum_html = sanitizer.bypassSecurityTrustHtml(this.impressum_html);
+    this.isValidApiVersion = AppConfig.checkApiVersion(this.detectedApiVersion, expectedApiVersion);
     if (this.testConfig) {
       localStorage.setItem(localStorageTestConfigKey, JSON.stringify(this.testConfig));
     } else {
       localStorage.removeItem(localStorageTestConfigKey);
     }
+    this.applyBackgroundColors();
+  }
+
+  private setCustomTexts(customTexts: KeyValuePairs): void {
+    const ctSettings = {};
+    Object.keys(customTextsDefault).forEach(k => {
+      ctSettings[k] = customTextsDefault[k].defaultvalue;
+    });
+    if (customTexts) {
+      Object.keys(customTexts).forEach(k => {
+        ctSettings[k] = customTexts[k];
+      });
+    }
+    this.cts.addCustomTexts(ctSettings);
+  }
+
+  private setAppConfig(appConfig: Map<string, string>): void {
+    this.app_title = this.cts.getCustomText('app_title');
+    if (!this.app_title) this.app_title = 'IQB-Testcenter';
+    this.intro_html = this.cts.getCustomText('app_intro1');
+    if (this.intro_html) {
+      this.impressum_html = this.intro_html;
+    } else {
+      this.intro_html = 'Einführungstext nicht definiert';
+      this.impressum_html = 'Impressum/Datenschutz nicht definiert';
+    }
+    this.mainLogo = 'assets/IQB-LogoA.png';
+    this.background_body = '';
+    this.background_box = '';
+    this.trusted_intro_html = null;
+    this.trusted_impressum_html = null;
+    this.global_warning = '';
+    this.global_warning_expired_day = '';
+    this.global_warning_expired_hour = '';
+    if (appConfig) {
+      Object.keys(appConfig).forEach(k => {
+        switch (k) {
+          case 'app_title':
+            this.app_title = appConfig[k];
+            break;
+          case 'mainLogo':
+            this.mainLogo = appConfig[k];
+            break;
+          case 'background_body':
+            this.background_body = appConfig[k];
+            break;
+          case 'background_box':
+            this.background_box = appConfig[k];
+            break;
+          case 'intro_html':
+            this.intro_html = appConfig[k];
+            break;
+          case 'impressum_html':
+            this.impressum_html = appConfig[k];
+            break;
+          case 'global_warning':
+            this.global_warning = appConfig[k];
+            break;
+          case 'global_warning_expired_day':
+            this.global_warning_expired_day = appConfig[k];
+            break;
+          case 'global_warning_expired_hour':
+            this.global_warning_expired_hour = appConfig[k];
+            break;
+          default:
+            console.warn(`unknown key in appConfig "${k}"`);
+        }
+      });
+    }
+    this.trusted_intro_html = this.sanitizer.bypassSecurityTrustHtml(this.intro_html);
+    this.trusted_impressum_html = this.sanitizer.bypassSecurityTrustHtml(this.impressum_html);
+  }
+
+  private applyBackgroundColors(): void {
+    document.documentElement.style.setProperty('--tc-body-background', this.background_body);
+    document.documentElement.style.setProperty('--tc-box-background', this.background_box);
   }
 
   private static checkApiVersion(versionToCheck: string, expectedVersion: string): boolean {
@@ -139,4 +182,18 @@ export class AppConfig {
     }
     return this.global_warning;
   }
+
+  getAppConfig(): Map<string, string> {
+    const appConfig = new Map<string, string>();
+    appConfig.set('app_title', this.app_title);
+    appConfig.set('mainLogo', this.mainLogo);
+    appConfig.set('background_body', this.background_body);
+    appConfig.set('background_box', this.background_box);
+    appConfig.set('intro_html', this.intro_html);
+    appConfig.set('impressum_html', this.impressum_html);
+    appConfig.set('global_warning', this.global_warning);
+    appConfig.set('global_warning_expired_day', this.global_warning_expired_day);
+    appConfig.set('global_warning_expired_hour', this.global_warning_expired_hour);
+    return appConfig;
+  }
 }
-- 
GitLab