From 938f9e7426ad16f5a5d2ec02c38a175b276a00a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <git@minelli.swiss>
Date: Mon, 7 Oct 2024 17:56:16 +0200
Subject: [PATCH] ClientSharedConfig => Add function to initiate config
 downloaded from server

---
 config/ClientsSharedConfig.ts | 116 ++++++++++++++++++++++++++++------
 1 file changed, 98 insertions(+), 18 deletions(-)

diff --git a/config/ClientsSharedConfig.ts b/config/ClientsSharedConfig.ts
index 77f1053..6e65968 100644
--- a/config/ClientsSharedConfig.ts
+++ b/config/ClientsSharedConfig.ts
@@ -1,48 +1,128 @@
+import axios               from 'axios';
+import DojoBackendResponse from '../../shared/types/Dojo/DojoBackendResponse';
+import ApiRoute            from '../types/Dojo/ApiRoute';
+
+
+interface ClientsConfig {
+    gitlabUrl: string,
+    gitlabAccountId: number,
+    gitlabAccountUsername: string,
+    loginGitlabClientId: string
+}
+
+
 class ClientsSharedConfig {
-    public apiURL: string;
+    private static config: ClientsSharedConfig | undefined = undefined;
 
-    public assignment: {
-        filename: string, neededFiles: Array<string>
+    public apiURL!: string;
+
+
+    public gitlab!: {
+        URL: string, dojoAccount: { id: number; username: string; };
     };
 
-    public gitlab: {
-        dojoAccount: { id: number; username: string; };
+    public login!: {
+        gitlab: {
+            client: {
+                id: string
+            }, url: {
+                redirect: string, token: string
+            }
+        }
     };
 
-    public readonly dockerCompose: {
+
+    public assignment!: {
+        filename: string, neededFiles: Array<string>
+    };
+
+    public dockerCompose!: {
         projectName: string
     };
 
-    public readonly exerciseResultsFolderMaxSizeInBytes: number;
+    public exerciseResultsFolderMaxSizeInBytes!: number;
 
-    public readonly filenames: {
+    public filenames!: {
         results: string;
     };
 
 
-    constructor() {
-        this.apiURL = process.env.API_URL ?? '';
+    public constructor() {
+        this.login = {
+            gitlab: {
+                client: {
+                    id: ''
+                },
+                url   : {
+                    redirect: '',
+                    token   : ''
+                }
+            }
+        };
+    }
 
-        this.assignment = {
-            filename   : process.env.ASSIGNMENT_FILENAME ?? '',
-            neededFiles: JSON.parse(process.env.EXERCISE_NEEDED_FILES ?? '[]')
+    public envVarGetter(): (envName: string, defaultValue: string) => string {
+        return (envName: string, defaultValue: string) => {
+            let value = process.env[envName] ?? defaultValue;
+
+            if ( value.includes('{{GITLAB_URL}}') ) {
+                value = value.replace('{{GITLAB_URL}}', this.gitlab.URL);
+            }
+
+            if ( value.includes('{{GITLAB_ACCOUNT_ID}}') ) {
+                value = value.replace('{{GITLAB_ACCOUNT_ID}}', String(this.gitlab.dojoAccount.id));
+            }
+
+            if ( value.includes('{{GITLAB_ACCOUNT_USERNAME}}') ) {
+                value = value.replace('{{GITLAB_ACCOUNT_USERNAME}}', this.gitlab.dojoAccount.username);
+            }
+
+            if ( value.includes('{{LOGIN_GITLAB_CLIENT_ID}}') ) {
+                value = value.replace('{{LOGIN_GITLAB_CLIENT_ID}}', this.login.gitlab.client.id);
+            }
+
+            return value;
         };
+    }
+
+    private async fetchConfigFromApi() {
+        const downloadedConfig: ClientsConfig = (await axios.get<DojoBackendResponse<ClientsConfig>>(`${ this.apiURL }${ ApiRoute.CLIENTS_CONFIG }`)).data.data;
 
         this.gitlab = {
+            URL        : downloadedConfig.gitlabUrl,
             dojoAccount: {
-                id      : Number(process.env.GITLAB_DOJO_ACCOUNT_ID ?? -1),
-                username: process.env.GITLAB_DOJO_ACCOUNT_USERNAME ?? ''
+                id      : downloadedConfig.gitlabAccountId,
+                username: downloadedConfig.gitlabAccountUsername
             }
         };
 
+        this.login.gitlab.client.id = downloadedConfig.loginGitlabClientId;
+    }
+
+    async init(apiUrl: string) {
+        this.apiURL = apiUrl;
+
+        await this.fetchConfigFromApi();
+        const getEnvVar = this.envVarGetter();
+
+        this.login.gitlab.url = {
+            redirect: getEnvVar('LOGIN_GITLAB_URL_REDIRECT', ''),
+            token   : getEnvVar('LOGIN_GITLAB_URL_TOKEN', '')
+        };
+
+        this.assignment = {
+            filename   : getEnvVar('ASSIGNMENT_FILENAME', ''),
+            neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]'))
+        };
+
         this.dockerCompose = {
-            projectName: process.env.DOCKER_COMPOSE_PROJECT_NAME ?? ''
+            projectName: getEnvVar('DOCKER_COMPOSE_PROJECT_NAME', '')
         };
 
-        this.exerciseResultsFolderMaxSizeInBytes = Number(process.env.EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES ?? 0);
+        this.exerciseResultsFolderMaxSizeInBytes = Number(getEnvVar('EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES', '0'));
 
         this.filenames = {
-            results: process.env.EXERCISE_RESULTS_FILENAME ?? ''
+            results: getEnvVar('EXERCISE_RESULTS_FILENAME', '')
         };
     }
 }
-- 
GitLab