fix: format
This commit is contained in:
parent
5da0720a08
commit
887787de05
8 changed files with 107 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getLearningObjectsFromPath } from '../services/learningObjects.js';
|
||||
import {FALLBACK_LANG} from "../config";
|
||||
import { FALLBACK_LANG } from '../config';
|
||||
|
||||
export async function getAllLearningObjects(
|
||||
req: Request,
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { themes } from '../data/themes.js';
|
||||
import {DWENGO_API_BASE, FALLBACK_LANG} from '../config.js';
|
||||
import { fetchWithLogging } from "../util/apiHelper.js";
|
||||
import { fetchLearningPaths } from "../services/learningPaths.js";
|
||||
import {LearningPath} from "../interfaces/learningPath";
|
||||
import { DWENGO_API_BASE, FALLBACK_LANG } from '../config.js';
|
||||
import { fetchWithLogging } from '../util/apiHelper.js';
|
||||
import { fetchLearningPaths } from '../services/learningPaths.js';
|
||||
import { LearningPath } from '../interfaces/learningPath';
|
||||
|
||||
/**
|
||||
* Fetch learning paths based on HRUIDs or return all if no HRUIDs are provided.
|
||||
* - If `hruids` are given -> fetch specific learning paths.
|
||||
* - If `hruids` is missing -> return all available learning paths.
|
||||
*/
|
||||
export async function getLearningPaths(req: Request, res: Response): Promise<void> {
|
||||
export async function getLearningPaths(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const hruids = req.query.hruids; // Can be string or array
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
@ -18,13 +21,21 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi
|
|||
let hruidList: string[];
|
||||
|
||||
if (hruids) {
|
||||
hruidList = Array.isArray(hruids) ? hruids.map(String) : [String(hruids)];
|
||||
hruidList = Array.isArray(hruids)
|
||||
? hruids.map(String)
|
||||
: [String(hruids)];
|
||||
} else {
|
||||
// If no hruids are provided, fetch ALL learning paths
|
||||
hruidList = themes.flatMap((theme) => {return 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);
|
||||
} catch (error) {
|
||||
|
@ -33,37 +44,51 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetch all learning paths for a specific theme.
|
||||
*/
|
||||
export async function getLearningPathsByTheme(req: Request, res: Response): Promise<void> {
|
||||
export async function getLearningPathsByTheme(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const themeKey = req.params.theme;
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
||||
const theme = themes.find((t) => {return t.title === themeKey});
|
||||
const theme = themes.find((t) => {
|
||||
return t.title === themeKey;
|
||||
});
|
||||
if (!theme) {
|
||||
console.error(`⚠️ WARNING: Theme "${themeKey}" not found.`);
|
||||
res.status(404).json({ error: 'Theme not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetchLearningPaths(theme.hruids, language, `theme "${themeKey}"`);
|
||||
const response = await fetchLearningPaths(
|
||||
theme.hruids,
|
||||
language,
|
||||
`theme "${themeKey}"`
|
||||
);
|
||||
res.json({
|
||||
theme: themeKey,
|
||||
hruids: theme.hruids,
|
||||
...response,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ Unexpected error fetching learning paths by theme:', error);
|
||||
console.error(
|
||||
'❌ Unexpected error fetching learning paths by theme:',
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search learning paths by query.
|
||||
*/
|
||||
export async function searchLearningPaths(req: Request, res: Response): Promise<void> {
|
||||
export async function searchLearningPaths(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const query = req.query.query as string;
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
@ -76,7 +101,11 @@ export async function searchLearningPaths(req: Request, res: Response): Promise<
|
|||
const apiUrl = `${DWENGO_API_BASE}/learningPath/search`;
|
||||
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
|
||||
);
|
||||
res.json(searchResults ?? []);
|
||||
} catch (error) {
|
||||
console.error('❌ Unexpected error searching learning paths:', error);
|
||||
|
|
|
@ -3,7 +3,7 @@ import path from 'path';
|
|||
import yaml from 'js-yaml';
|
||||
import { Request, Response } from 'express';
|
||||
import { themes } from '../data/themes.js';
|
||||
import {FALLBACK_LANG} from "../config";
|
||||
import { FALLBACK_LANG } from '../config';
|
||||
|
||||
interface Translations {
|
||||
curricula_page: {
|
||||
|
@ -27,7 +27,8 @@ function loadTranslations(language: string): Translations {
|
|||
}
|
||||
|
||||
export function getThemes(req: Request, res: Response) {
|
||||
const language = (req.query.language as string)?.toLowerCase() || FALLBACK_LANG;
|
||||
const language =
|
||||
(req.query.language as string)?.toLowerCase() || FALLBACK_LANG;
|
||||
const translations = loadTranslations(language);
|
||||
|
||||
const themeList = themes.map((theme) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue