style: fix linting issues met Prettier

This commit is contained in:
Lint Action 2025-03-11 03:09:12 +00:00
parent aa1a85e64e
commit 2a2881ec30
84 changed files with 846 additions and 1013 deletions

View file

@ -1,19 +1,11 @@
import {LearningPathProvider} from "./learning-path-provider";
import {
FilteredLearningObject,
LearningObjectNode,
LearningPath,
LearningPathResponse,
Transition
} from "../../interfaces/learning-content";
import {
LearningPath as LearningPathEntity
} from "../../entities/content/learning-path.entity"
import {getLearningPathRepository} from "../../data/repositories";
import {Language} from "../../entities/content/language";
import learningObjectService from "../learning-objects/learning-object-service";
import { LearningPathNode } from "../../entities/content/learning-path-node.entity";
import {LearningPathTransition} from "../../entities/content/learning-path-transition.entity";
import { LearningPathProvider } from './learning-path-provider';
import { FilteredLearningObject, LearningObjectNode, LearningPath, LearningPathResponse, Transition } from '../../interfaces/learning-content';
import { LearningPath as LearningPathEntity } from '../../entities/content/learning-path.entity';
import { getLearningPathRepository } from '../../data/repositories';
import { Language } from '../../entities/content/language';
import learningObjectService from '../learning-objects/learning-object-service';
import { LearningPathNode } from '../../entities/content/learning-path-node.entity';
import { LearningPathTransition } from '../../entities/content/learning-path-transition.entity';
/**
* Fetches the corresponding learning object for each of the nodes and creates a map that maps each node to its
@ -25,19 +17,19 @@ async function getLearningObjectsForNodes(nodes: LearningPathNode[]): Promise<Ma
// Its corresponding learning object.
const nullableNodesToLearningObjects = new Map<LearningPathNode, FilteredLearningObject | null>(
await Promise.all(
nodes.map(node =>
learningObjectService.getLearningObjectById({
hruid: node.learningObjectHruid,
version: node.version,
language: node.language
}).then(learningObject =>
<[LearningPathNode, FilteredLearningObject | null]>[node, learningObject]
)
nodes.map((node) =>
learningObjectService
.getLearningObjectById({
hruid: node.learningObjectHruid,
version: node.version,
language: node.language,
})
.then((learningObject) => <[LearningPathNode, FilteredLearningObject | null]>[node, learningObject])
)
)
);
if (nullableNodesToLearningObjects.values().some(it => it === null)) {
throw new Error("At least one of the learning objects on this path could not be found.")
if (nullableNodesToLearningObjects.values().some((it) => it === null)) {
throw new Error('At least one of the learning objects on this path could not be found.');
}
return nullableNodesToLearningObjects as Map<LearningPathNode, FilteredLearningObject>;
}
@ -46,16 +38,19 @@ async function getLearningObjectsForNodes(nodes: LearningPathNode[]): Promise<Ma
* Convert the given learning path entity to an object which conforms to the learning path content.
*/
async function convertLearningPath(learningPath: LearningPathEntity, order: number): Promise<LearningPath> {
const nodesToLearningObjects: Map<LearningPathNode, FilteredLearningObject> =
await getLearningObjectsForNodes(learningPath.nodes);
const nodesToLearningObjects: Map<LearningPathNode, FilteredLearningObject> = await getLearningObjectsForNodes(learningPath.nodes);
const targetAges =
nodesToLearningObjects.values().flatMap(it => it.targetAges || []).toArray();
const targetAges = nodesToLearningObjects
.values()
.flatMap((it) => it.targetAges || [])
.toArray();
const keywords =
nodesToLearningObjects.values().flatMap(it => it.keywords || []).toArray();
const keywords = nodesToLearningObjects
.values()
.flatMap((it) => it.keywords || [])
.toArray();
const image = learningPath.image ? learningPath.image.toString("base64") : undefined;
const image = learningPath.image ? learningPath.image.toString('base64') : undefined;
return {
_id: `${learningPath.hruid}/${learningPath.language}`, // For backwards compatibility with the original Dwengo API.
@ -71,8 +66,8 @@ async function convertLearningPath(learningPath: LearningPathEntity, order: numb
keywords: keywords.join(' '),
target_ages: targetAges,
max_age: Math.max(...targetAges),
min_age: Math.min(...targetAges)
}
min_age: Math.min(...targetAges),
};
}
/**
@ -80,24 +75,23 @@ async function convertLearningPath(learningPath: LearningPathEntity, order: numb
* learning objects into a list of learning path nodes as they should be represented in the API.
* @param nodesToLearningObjects
*/
function convertNodes(
nodesToLearningObjects: Map<LearningPathNode, FilteredLearningObject>
): LearningObjectNode[] {
return nodesToLearningObjects.entries().map((entry) => {
const [node, learningObject] = entry;
return {
_id: learningObject.uuid,
language: learningObject.language,
start_node: node.startNode,
created_at: node.createdAt.toISOString(),
updatedAt: node.updatedAt.toISOString(),
learningobject_hruid: node.learningObjectHruid,
version: learningObject.version,
transitions: node.transitions.map((trans, i) =>
convertTransition(trans, i, nodesToLearningObjects)
)
}
}).toArray();
function convertNodes(nodesToLearningObjects: Map<LearningPathNode, FilteredLearningObject>): LearningObjectNode[] {
return nodesToLearningObjects
.entries()
.map((entry) => {
const [node, learningObject] = entry;
return {
_id: learningObject.uuid,
language: learningObject.language,
start_node: node.startNode,
created_at: node.createdAt.toISOString(),
updatedAt: node.updatedAt.toISOString(),
learningobject_hruid: node.learningObjectHruid,
version: learningObject.version,
transitions: node.transitions.map((trans, i) => convertTransition(trans, i, nodesToLearningObjects)),
};
})
.toArray();
}
/**
@ -115,17 +109,17 @@ function convertTransition(
): Transition {
const nextNode = nodesToLearningObjects.get(transition.next);
if (!nextNode) {
throw new Error(`Learning object ${transition.next.learningObjectHruid}/${transition.next.language}/${transition.next.version} not found!`)
throw new Error(`Learning object ${transition.next.learningObjectHruid}/${transition.next.language}/${transition.next.version} not found!`);
} else {
return {
_id: "" + index, // Retained for backwards compatibility. The index uniquely identifies the transition within the learning path.
_id: '' + index, // Retained for backwards compatibility. The index uniquely identifies the transition within the learning path.
default: false, // We don't work with default transitions but retain this for backwards compatibility.
next: {
_id: nextNode._id + index, // Construct a unique ID for the transition for backwards compatibility.
hruid: transition.next.learningObjectHruid,
language: nextNode.language,
version: nextNode.version
}
version: nextNode.version,
},
};
}
}
@ -140,19 +134,15 @@ const databaseLearningPathProvider: LearningPathProvider = {
async fetchLearningPaths(hruids: string[], language: Language, source: string): Promise<LearningPathResponse> {
const learningPathRepo = getLearningPathRepository();
const learningPaths = await Promise.all(
hruids.map(hruid => learningPathRepo.findByHruidAndLanguage(hruid, language))
);
const learningPaths = await Promise.all(hruids.map((hruid) => learningPathRepo.findByHruidAndLanguage(hruid, language)));
const filteredLearningPaths = await Promise.all(
learningPaths
.filter(learningPath => learningPath !== null)
.map((learningPath, index) => convertLearningPath(learningPath, index))
learningPaths.filter((learningPath) => learningPath !== null).map((learningPath, index) => convertLearningPath(learningPath, index))
);
return {
success: filteredLearningPaths.length > 0,
data: await Promise.all(filteredLearningPaths),
source
source,
};
},
@ -163,12 +153,8 @@ const databaseLearningPathProvider: LearningPathProvider = {
const learningPathRepo = getLearningPathRepository();
const searchResults = await learningPathRepo.findByQueryStringAndLanguage(query, language);
return await Promise.all(
searchResults.map((result, index) =>
convertLearningPath(result, index)
)
);
}
}
return await Promise.all(searchResults.map((result, index) => convertLearningPath(result, index)));
},
};
export default databaseLearningPathProvider;