diff --git a/frontend/src/data-objects/learning-paths/learning-path-node.ts b/frontend/src/data-objects/learning-paths/learning-path-node.ts index 69c0ee01..35dc42de 100644 --- a/frontend/src/data-objects/learning-paths/learning-path-node.ts +++ b/frontend/src/data-objects/learning-paths/learning-path-node.ts @@ -34,24 +34,21 @@ export class LearningPathNode { version: dto.version, language: dto.language, transitions: dto.transitions.map((transDto) => { - const nextNodeDto = otherNodes.filter( + const nextNodeDto = otherNodes.find( (it) => it.learningobject_hruid === transDto.next.hruid && it.language === transDto.next.language && it.version === transDto.next.version, ); - if (nextNodeDto.length !== 1) { - throw new Error( - `Invalid learning path! There is a transition to node` + - `${transDto.next.hruid}/${transDto.next.language}/${transDto.next.version}, but there are` + - `${nextNodeDto.length} such nodes.`, - ); + if (nextNodeDto) { + return { + next: LearningPathNode.fromDTOAndOtherNodes(nextNodeDto, otherNodes), + default: transDto.default, + }; + } else { + return undefined } - return { - next: LearningPathNode.fromDTOAndOtherNodes(nextNodeDto[0], otherNodes), - default: transDto.default, - }; - }), + }).filter(it => it !== undefined), createdAt: new Date(dto.created_at), updatedAt: new Date(dto.updatedAt), done: dto.done, diff --git a/frontend/src/data-objects/learning-paths/learning-path.ts b/frontend/src/data-objects/learning-paths/learning-path.ts index a018eadf..229f3ff7 100644 --- a/frontend/src/data-objects/learning-paths/learning-path.ts +++ b/frontend/src/data-objects/learning-paths/learning-path.ts @@ -72,11 +72,6 @@ export class LearningPath { } static fromDTO(dto: LearningPathDTO): LearningPath { - const startNodeDto = dto.nodes.filter((it) => it.start_node === true); - if (startNodeDto.length !== 1) { - throw new Error(`Invalid learning path: ${dto.hruid}/${dto.language}! - Expected precisely one start node, but there were ${startNodeDto.length}.`); - } return new LearningPath({ language: dto.language, hruid: dto.hruid, @@ -86,8 +81,17 @@ export class LearningPath { amountOfNodesLeft: dto.num_nodes_left, keywords: dto.keywords.split(" "), targetAges: { min: dto.min_age, max: dto.max_age }, - startNode: LearningPathNode.fromDTOAndOtherNodes(startNodeDto[0], dto.nodes), + startNode: LearningPathNode.fromDTOAndOtherNodes(LearningPath.getStartNode(dto), dto.nodes), image: dto.image, }); } + + static getStartNode(dto: LearningPathDTO): LearningPathNodeDTO { + const startNodeDtos = dto.nodes.filter((it) => it.start_node === true); + if (startNodeDtos.length < 1) { // The learning path has no starting node -> use the first node. + return dto.nodes[0]; + } else { // The learning path has 1 or more starting nodes -> use the first start node. + return startNodeDtos[0]; + } + } }