refactor(backend): Streamlining van de testdata voor leerpaden en leerobjecten + integratie in seed

Hierbij ook testdata functionaliteit toegevoegd om makkelijk nieuwe leerpaden aan te maken.
This commit is contained in:
Gerald Schmittinger 2025-04-15 23:43:30 +02:00
parent 4092f1f617
commit 202cf4e33c
32 changed files with 691 additions and 493 deletions

View file

@ -14,13 +14,14 @@ import {
} from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
import {Group} from "../../entities/assignments/group.entity";
import {Collection} from "@mikro-orm/core";
/**
* Fetches the corresponding learning object for each of the nodes and creates a map that maps each node to its
* corresponding learning object.
* @param nodes The nodes to find the learning object for.
*/
async function getLearningObjectsForNodes(nodes: LearningPathNode[]): Promise<Map<LearningPathNode, FilteredLearningObject>> {
async function getLearningObjectsForNodes(nodes: Collection<LearningPathNode>): Promise<Map<LearningPathNode, FilteredLearningObject>> {
// Fetching the corresponding learning object for each of the nodes and creating a map that maps each node to
// Its corresponding learning object.
const nullableNodesToLearningObjects = new Map<LearningPathNode, FilteredLearningObject | null>(
@ -208,7 +209,7 @@ const databaseLearningPathProvider: LearningPathProvider = {
const searchResults = await learningPathRepo.findByQueryStringAndLanguage(query, language);
return await Promise.all(searchResults.map(async (result, index) => convertLearningPath(result, index, personalizedFor)));
},
}
};
export default databaseLearningPathProvider;

View file

@ -1,13 +1,79 @@
import dwengoApiLearningPathProvider from './dwengo-api-learning-path-provider.js';
import databaseLearningPathProvider from './database-learning-path-provider.js';
import { envVars, getEnvVar } from '../../util/envVars.js';
import { LearningPath, LearningPathResponse } from '@dwengo-1/common/interfaces/learning-content';
import {LearningObjectNode, LearningPath, LearningPathResponse} from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
import {Group} from "../../entities/assignments/group.entity";
import {LearningPath as LearningPathEntity} from "../../entities/content/learning-path.entity";
import {getLearningPathRepository} from "../../data/repositories";
import {LearningPathNode} from "../../entities/content/learning-path-node.entity";
import {LearningPathTransition} from "../../entities/content/learning-path-transition.entity";
import {base64ToArrayBuffer} from "../../util/base64-buffer-conversion";
import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher";
import {mapToTeacher} from "../../interfaces/teacher";
import {Collection} from "@mikro-orm/core";
const userContentPrefix = getEnvVar(envVars.UserContentPrefix);
const allProviders = [dwengoApiLearningPathProvider, databaseLearningPathProvider];
export function mapToLearningPath(
dto: LearningPath, adminsDto: TeacherDTO[]
): LearningPathEntity {
const admins = adminsDto.map(admin => mapToTeacher(admin));
const repo = getLearningPathRepository();
const path = repo.create({
hruid: dto.hruid,
language: dto.language as Language,
description: dto.description,
title: dto.title,
admins,
image: dto.image ? Buffer.from(base64ToArrayBuffer(dto.image)) : null
});
const nodes = dto.nodes.map((nodeDto: LearningObjectNode, i: number) =>
repo.createNode({
learningPath: path,
learningObjectHruid: nodeDto.learningobject_hruid,
language: nodeDto.language,
version: nodeDto.version,
startNode: nodeDto.start_node ?? false,
nodeNumber: i,
createdAt: new Date(),
updatedAt: new Date()
})
);
dto.nodes.forEach(nodeDto => {
const fromNode = nodes.find(it =>
it.learningObjectHruid === nodeDto.learningobject_hruid
&& it.language === nodeDto.language
&& it.version === nodeDto.version
)!;
const transitions = nodeDto.transitions.map((transDto, i) => {
const toNode = nodes.find(it =>
it.learningObjectHruid === transDto.next.hruid
&& it.language === transDto.next.language
&& it.version === transDto.next.version
);
if (toNode) {
return repo.createTransition({
transitionNumber: i,
node: fromNode,
next: toNode,
condition: transDto.condition ?? "true"
});
} else {
return undefined;
}
}).filter(it => it).map(it => it!);
fromNode.transitions = new Collection<LearningPathTransition>(transitions);
});
path.nodes = new Collection<LearningPathNode>(nodes);
return path;
}
/**
* Service providing access to data about learning paths from the appropriate data source (database or Dwengo-api)
*/
@ -54,6 +120,17 @@ const learningPathService = {
);
return providerResponses.flat();
},
/**
* Add a new learning path to the database.
* @param dto Learning path DTO from which the learning path will be created.
* @param admins Teachers who should become an admin of the learning path.
*/
async createNewLearningPath(dto: LearningPath, admins: TeacherDTO[]): Promise<void> {
const repo = getLearningPathRepository();
const path = mapToLearningPath(dto, admins);
await repo.save(path, {preventOverwrite: true})
}
};
export default learningPathService;