fix: format + lint

This commit is contained in:
Gabriellvl 2025-03-02 12:18:08 +01:00
parent 308661d72b
commit e762621287
5 changed files with 87 additions and 39 deletions

View file

@ -2,17 +2,17 @@ import { Request, Response } from 'express';
import { import {
getLearningObjectById, getLearningObjectById,
getLearningObjectIdsFromPath, getLearningObjectIdsFromPath,
getLearningObjectsFromPath getLearningObjectsFromPath,
} from '../services/learningObjects.js'; } from '../services/learningObjects.js';
import { FALLBACK_LANG } from '../config.js'; import { FALLBACK_LANG } from '../config.js';
import {FilteredLearningObject} from "../interfaces/learningPath"; import { FilteredLearningObject } from '../interfaces/learningPath';
export async function getAllLearningObjects( export async function getAllLearningObjects(
req: Request, req: Request,
res: Response res: Response
): Promise<void> { ): Promise<void> {
try { try {
const hruid = (req.query.hruid as string); const hruid = req.query.hruid as string;
const full = req.query.full === 'true'; const full = req.query.full === 'true';
const language = (req.query.language as string) || FALLBACK_LANG; const language = (req.query.language as string) || FALLBACK_LANG;
@ -22,10 +22,14 @@ export async function getAllLearningObjects(
} }
let learningObjects: FilteredLearningObject[] | string[]; let learningObjects: FilteredLearningObject[] | string[];
if (full) if (full) {
learningObjects = await getLearningObjectsFromPath(hruid,language); learningObjects = await getLearningObjectsFromPath(hruid, language);
else } else {
learningObjects = await getLearningObjectIdsFromPath(hruid, language) learningObjects = await getLearningObjectIdsFromPath(
hruid,
language
);
}
res.json(learningObjects); res.json(learningObjects);
} catch (error) { } catch (error) {
@ -34,7 +38,6 @@ export async function getAllLearningObjects(
} }
} }
export async function getLearningObject( export async function getLearningObject(
req: Request, req: Request,
res: Response res: Response
@ -50,7 +53,7 @@ export async function getLearningObject(
const learningObject = await getLearningObjectById(hruid, language); const learningObject = await getLearningObjectById(hruid, language);
res.json(learningObject); res.json(learningObject);
} catch (error){ } catch (error) {
console.error('Error fetching learning object:', error); console.error('Error fetching learning object:', error);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }

View file

@ -1,41 +1,59 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { themes } from '../data/themes.js'; import { themes } from '../data/themes.js';
import { FALLBACK_LANG } from '../config.js'; import { FALLBACK_LANG } from '../config.js';
import { fetchLearningPaths, searchLearningPaths } from '../services/learningPaths.js'; import {
fetchLearningPaths,
searchLearningPaths,
} from '../services/learningPaths.js';
/** /**
* Fetch learning paths based on query parameters. * Fetch learning paths based on query parameters.
*/ */
export async function getLearningPaths( export async function getLearningPaths(
req: Request, req: Request,
res: Response res: Response
): Promise<void> { ): Promise<void> {
try { try {
const hruids = req.query.hruid; const hruids = req.query.hruid;
const themeKey = (req.query.theme as string); const themeKey = req.query.theme as string;
const searchQuery = (req.query.search as string); const searchQuery = req.query.search as string;
const language = (req.query.language as string) || FALLBACK_LANG; const language = (req.query.language as string) || FALLBACK_LANG;
let hruidList = []; let hruidList;
if (hruids) { if (hruids) {
hruidList = Array.isArray(hruids) ? hruids.map(String) : [String(hruids)]; hruidList = Array.isArray(hruids)
? hruids.map(String)
: [String(hruids)];
} else if (themeKey) { } else if (themeKey) {
const theme = themes.find((t) => t.title === themeKey); const theme = themes.find((t) => {
return t.title === themeKey;
});
if (theme) { if (theme) {
hruidList = theme.hruids; hruidList = theme.hruids;
} else { } else {
res.status(404).json({ error: `Theme "${themeKey}" not found.` }); res.status(404).json({
error: `Theme "${themeKey}" not found.`,
});
return; return;
} }
} else if (searchQuery) { } else if (searchQuery) {
const searchResults = await searchLearningPaths(searchQuery, language); const searchResults = await searchLearningPaths(
searchQuery,
language
);
res.json(searchResults); res.json(searchResults);
return; return;
} else { } else {
hruidList = themes.flatMap((theme) => theme.hruids); hruidList = themes.flatMap((theme) => {
return theme.hruids;
});
} }
const learningPaths = await fetchLearningPaths(hruidList, language, `HRUIDs: ${hruidList.join(', ')}`); const learningPaths = await fetchLearningPaths(
hruidList,
language,
`HRUIDs: ${hruidList.join(', ')}`
);
res.json(learningPaths.data); res.json(learningPaths.data);
} catch (error) { } catch (error) {
console.error('❌ Unexpected error fetching learning paths:', error); console.error('❌ Unexpected error fetching learning paths:', error);

View file

@ -1,5 +1,8 @@
import express from 'express'; import express from 'express';
import {getAllLearningObjects, getLearningObject} from '../controllers/learningObjects.js'; import {
getAllLearningObjects,
getLearningObject,
} from '../controllers/learningObjects.js';
const router = express.Router(); const router = express.Router();
@ -15,7 +18,6 @@ const router = express.Router();
// Example 2: http://localhost:3000/learningObject?full=true&hruid=un_artificiele_intelligentie // Example 2: http://localhost:3000/learningObject?full=true&hruid=un_artificiele_intelligentie
router.get('/', getAllLearningObjects); router.get('/', getAllLearningObjects);
// Parameter: hruid of learning object // Parameter: hruid of learning object
// Query: language // Query: language
// Route to fetch data of one learning object based on its hruid // Route to fetch data of one learning object based on its hruid

View file

@ -3,7 +3,8 @@ import { fetchWithLogging } from '../util/apiHelper.js';
import { import {
FilteredLearningObject, FilteredLearningObject,
LearningObjectMetadata, LearningObjectMetadata,
LearningObjectNode, LearningPathResponse, LearningObjectNode,
LearningPathResponse,
} from '../interfaces/learningPath.js'; } from '../interfaces/learningPath.js';
import { fetchLearningPaths } from './learningPaths.js'; import { fetchLearningPaths } from './learningPaths.js';
@ -40,7 +41,7 @@ function filterData(
export async function getLearningObjectById( export async function getLearningObjectById(
hruid: string, hruid: string,
language: string language: string
): Promise<FilteredLearningObject | null> { ): Promise<FilteredLearningObject | null> {
const metadataUrl = `${DWENGO_API_BASE}/learningObject/getMetadata?hruid=${hruid}&language=${language}`; const metadataUrl = `${DWENGO_API_BASE}/learningObject/getMetadata?hruid=${hruid}&language=${language}`;
const metadata = await fetchWithLogging<LearningObjectMetadata>( const metadata = await fetchWithLogging<LearningObjectMetadata>(
metadataUrl, metadataUrl,
@ -65,26 +66,43 @@ async function fetchLearningObjects(
language: string language: string
): Promise<FilteredLearningObject[] | string[]> { ): Promise<FilteredLearningObject[] | string[]> {
try { try {
const learningPathResponse: LearningPathResponse = await fetchLearningPaths( const learningPathResponse: LearningPathResponse =
[hruid], await fetchLearningPaths(
language, [hruid],
`Learning path for HRUID "${hruid}"` language,
); `Learning path for HRUID "${hruid}"`
);
if (!learningPathResponse.success || !learningPathResponse.data?.length) { if (
console.error(`⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.`); !learningPathResponse.success ||
!learningPathResponse.data?.length
) {
console.error(
`⚠️ WARNING: Learning path "${hruid}" exists but contains no learning objects.`
);
return []; return [];
} }
const nodes: LearningObjectNode[] = learningPathResponse.data[0].nodes; const nodes: LearningObjectNode[] = learningPathResponse.data[0].nodes;
if (!full) { if (!full) {
return nodes.map(node => node.learningobject_hruid); return nodes.map((node) => {
return node.learningobject_hruid;
});
} }
return await Promise.all( return await Promise.all(
nodes.map(async (node) => getLearningObjectById(node.learningobject_hruid, language)) nodes.map(async (node) => {
).then((objects) => objects.filter((obj): obj is FilteredLearningObject => obj !== null)); return getLearningObjectById(
node.learningobject_hruid,
language
);
})
).then((objects) => {
return objects.filter((obj): obj is FilteredLearningObject => {
return obj !== null;
});
});
} catch (error) { } catch (error) {
console.error('❌ Error fetching learning objects:', error); console.error('❌ Error fetching learning objects:', error);
return []; return [];
@ -98,7 +116,11 @@ export async function getLearningObjectsFromPath(
hruid: string, hruid: string,
language: string language: string
): Promise<FilteredLearningObject[]> { ): Promise<FilteredLearningObject[]> {
return await fetchLearningObjects(hruid, true, language) as FilteredLearningObject[]; return (await fetchLearningObjects(
hruid,
true,
language
)) as FilteredLearningObject[];
} }
/** /**
@ -108,5 +130,5 @@ export async function getLearningObjectIdsFromPath(
hruid: string, hruid: string,
language: string language: string
): Promise<string[]> { ): Promise<string[]> {
return await fetchLearningObjects(hruid, false, language) as string[]; return (await fetchLearningObjects(hruid, false, language)) as string[];
} }

View file

@ -45,14 +45,17 @@ export async function fetchLearningPaths(
}; };
} }
export async function searchLearningPaths( export async function searchLearningPaths(
query: string, query: string,
language: string language: string
): Promise<LearningPath[]> { ): Promise<LearningPath[]> {
const apiUrl = `${DWENGO_API_BASE}/learningPath/search`; const apiUrl = `${DWENGO_API_BASE}/learningPath/search`;
const params = { all: query, language }; const params = { all: query, language };
const searchResults = await fetchWithLogging<LearningPath[]>(apiUrl, `Search learning paths with query "${query}"`, params); const searchResults = await fetchWithLogging<LearningPath[]>(
apiUrl,
`Search learning paths with query "${query}"`,
params
);
return searchResults ?? []; return searchResults ?? [];
} }