Merge branch 'dev' into chore/logging
This commit is contained in:
		
						commit
						6d05978568
					
				
					 13 changed files with 133 additions and 32 deletions
				
			
		|  | @ -1,39 +1,17 @@ | |||
| import fs from 'fs'; | ||||
| import path from 'path'; | ||||
| import yaml from 'js-yaml'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { themes } from '../data/themes.js'; | ||||
| import { loadTranslations } from "../util/translationHelper.js"; | ||||
| import { FALLBACK_LANG } from '../config.js'; | ||||
| import { getLogger, Logger } from '../logging/initalize.js'; | ||||
| 
 | ||||
| const logger: Logger = getLogger(); | ||||
| 
 | ||||
| interface Translations { | ||||
|     curricula_page: { | ||||
|         [key: string]: { title: string; description?: string }; // Optioneel veld description
 | ||||
|         [key: string]: { title: string; description?: string }; | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| function loadTranslations(language: string): Translations { | ||||
|     try { | ||||
|         const filePath = path.join(process.cwd(), '_i18n', `${language}.yml`); | ||||
|         const yamlFile = fs.readFileSync(filePath, 'utf8'); | ||||
|         return yaml.load(yamlFile) as Translations; | ||||
|     } catch (error) { | ||||
|         logger.error( | ||||
|             `Cannot load translation for: ${language}, fallen back to Dutch` | ||||
|         ); | ||||
|         logger.error(error); | ||||
|         const fallbackPath = path.join(process.cwd(), '_i18n', 'nl.yml'); | ||||
|         return yaml.load(fs.readFileSync(fallbackPath, 'utf8')) as Translations; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| export function getThemes(req: Request, res: Response) { | ||||
|     const language = | ||||
|         (req.query.language as string)?.toLowerCase() || FALLBACK_LANG; | ||||
|     const translations = loadTranslations(language); | ||||
| 
 | ||||
|     const language = (req.query.language as string)?.toLowerCase() || 'nl'; | ||||
|     const translations = loadTranslations<Translations>(language); | ||||
|     const themeList = themes.map((theme) => { | ||||
|         return { | ||||
|             key: theme.title, | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| import { Entity, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core'; | ||||
| import { Question } from './question.entity'; | ||||
| import { Teacher } from '../users/teacher.entity'; | ||||
| import { Question } from './question.entity.js'; | ||||
| import { Teacher } from '../users/teacher.entity.js'; | ||||
| 
 | ||||
| @Entity() | ||||
| export class Answer { | ||||
|  |  | |||
|  | @ -5,8 +5,33 @@ import { SqliteDriver } from '@mikro-orm/sqlite'; | |||
| import { MikroOrmLogger } from './logging/mikroOrmLogger.js'; | ||||
| import { LOG_LEVEL } from './config.js'; | ||||
| 
 | ||||
| const entities = ['dist/**/*.entity.js']; | ||||
| const entitiesTs = ['src/**/*.entity.ts']; | ||||
| // Import alle entity-bestanden handmatig
 | ||||
| import { User } from './entities/users/user.entity.js'; | ||||
| import { Student } from './entities/users/student.entity.js'; | ||||
| import { Teacher } from './entities/users/teacher.entity.js'; | ||||
| 
 | ||||
| import { Assignment } from './entities/assignments/assignment.entity.js'; | ||||
| import { Group } from './entities/assignments/group.entity.js'; | ||||
| import { Submission } from './entities/assignments/submission.entity.js'; | ||||
| 
 | ||||
| import { Class } from './entities/classes/class.entity.js'; | ||||
| import { ClassJoinRequest } from './entities/classes/class-join-request.entity.js'; | ||||
| import { TeacherInvitation } from './entities/classes/teacher-invitation.entity.js'; | ||||
| 
 | ||||
| import { Attachment } from './entities/content/attachment.entity.js'; | ||||
| import { LearningObject } from './entities/content/learning-object.entity.js'; | ||||
| import { LearningPath } from './entities/content/learning-path.entity.js'; | ||||
| 
 | ||||
| import { Answer } from './entities/questions/answer.entity.js'; | ||||
| import { Question } from './entities/questions/question.entity.js'; | ||||
| 
 | ||||
| const entities = [ | ||||
|     User, Student, Teacher, | ||||
|     Assignment, Group, Submission, | ||||
|     Class, ClassJoinRequest, TeacherInvitation, | ||||
|     Attachment, LearningObject, LearningPath, | ||||
|     Answer, Question | ||||
| ]; | ||||
| 
 | ||||
| function config(testingMode: boolean = false): Options { | ||||
|     if (testingMode) { | ||||
|  | @ -14,7 +39,7 @@ function config(testingMode: boolean = false): Options { | |||
|             driver: SqliteDriver, | ||||
|             dbName: getEnvVar(EnvVars.DbName), | ||||
|             entities: entities, | ||||
|             entitiesTs: entitiesTs, | ||||
|             // entitiesTs: entitiesTs,
 | ||||
| 
 | ||||
|             // Workaround: vitest: `TypeError: Unknown file extension ".ts"` (ERR_UNKNOWN_FILE_EXTENSION)
 | ||||
|             // (see https://mikro-orm.io/docs/guide/project-setup#testing-the-endpoint)
 | ||||
|  | @ -32,7 +57,7 @@ function config(testingMode: boolean = false): Options { | |||
|         user: getEnvVar(EnvVars.DbUsername), | ||||
|         password: getEnvVar(EnvVars.DbPassword), | ||||
|         entities: entities, | ||||
|         entitiesTs: entitiesTs, | ||||
|         //entitiesTs: entitiesTs,
 | ||||
| 
 | ||||
|         // Logging
 | ||||
|         debug: LOG_LEVEL === 'debug', | ||||
|  |  | |||
							
								
								
									
										19
									
								
								backend/src/util/translationHelper.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								backend/src/util/translationHelper.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | |||
| import fs from 'fs'; | ||||
| import path from 'path'; | ||||
| import yaml from 'js-yaml'; | ||||
| import {FALLBACK_LANG} from "../../config"; | ||||
| 
 | ||||
| export function loadTranslations<T>(language: string): T { | ||||
|     try { | ||||
|         const filePath = path.join(process.cwd(), '_i18n', `${language}.yml`); | ||||
|         const yamlFile = fs.readFileSync(filePath, 'utf8'); | ||||
|         return yaml.load(yamlFile) as T; | ||||
|     } catch (error) { | ||||
|         console.error( | ||||
|             `Cannot load translation for ${language}, fallen back to dutch` | ||||
|         ); | ||||
|         console.error(error); | ||||
|         const fallbackPath = path.join(process.cwd(), '_i18n', `${FALLBACK_LANG}.yml`); | ||||
|         return yaml.load(fs.readFileSync(fallbackPath, 'utf8')) as T; | ||||
|     } | ||||
| } | ||||
		Reference in a new issue