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:
parent
4092f1f617
commit
202cf4e33c
32 changed files with 691 additions and 493 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue