refactor(backend): Changed folder structure for learning content services.

This commit is contained in:
Gerald Schmittinger 2025-03-04 22:38:21 +01:00
parent 18ee991ce3
commit cc6947dd3c
8 changed files with 13 additions and 13 deletions

View file

@ -0,0 +1,65 @@
import { fetchWithLogging } from '../../util/apiHelper.js';
import { DWENGO_API_BASE } from '../../config.js';
import {
LearningPath,
LearningPathResponse,
} from '../../interfaces/learning-content.js';
import {LearningPathProvider} from "./learning-path-provider";
const dwengoApiLearningPathProvider: LearningPathProvider = {
async fetchLearningPaths(
hruids: string[],
language: string,
source: string
): Promise<LearningPathResponse> {
if (hruids.length === 0) {
return {
success: false,
source,
data: null,
message: `No HRUIDs provided for ${source}.`,
};
}
const apiUrl = `${DWENGO_API_BASE}/learningPath/getPathsFromIdList`;
const params = { pathIdList: JSON.stringify({ hruids }), language };
const learningPaths = await fetchWithLogging<LearningPath[]>(
apiUrl,
`Learning paths for ${source}`,
{ params }
);
if (!learningPaths || learningPaths.length === 0) {
console.error(`⚠️ WARNING: No learning paths found for ${source}.`);
return {
success: false,
source,
data: [],
message: `No learning paths found for ${source}.`,
};
}
return {
success: true,
source,
data: learningPaths,
};
},
async searchLearningPaths(
query: string,
language: string
): Promise<LearningPath[]> {
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 }
);
return searchResults ?? [];
}
};
export default dwengoApiLearningPathProvider;

View file

@ -0,0 +1,16 @@
import {LearningPath, LearningPathResponse} from "../../interfaces/learning-content";
/**
* Generic interface for a service which provides access to learning paths from a data source.
*/
export interface LearningPathProvider {
/**
* Fetch the learning paths with the given hruids from the data source.
*/
fetchLearningPaths(hruids: string[], language: string, source: string): Promise<LearningPathResponse>;
/**
* Search learning paths in the data source using the given search string.
*/
searchLearningPaths(query: string, language: string): Promise<LearningPath[]>;
}

View file

@ -0,0 +1,23 @@
import {LearningPath, LearningPathResponse} from "../../interfaces/learning-content";
import dwengoApiLearningPathProvider from "./dwengo-api-learning-path-provider";
/**
* Service providing access to data about learning paths from the appropriate data source (database or Dwengo-api)
*/
const learningPathService = {
/**
* Fetch the learning paths with the given hruids from the data source.
*/
fetchLearningPaths(hruids: string[], language: string, source: string): Promise<LearningPathResponse> {
return dwengoApiLearningPathProvider.fetchLearningPaths(hruids, language, source);
},
/**
* Search learning paths in the data source using the given search string.
*/
searchLearningPaths(query: string, language: string): Promise<LearningPath[]> {
return dwengoApiLearningPathProvider.searchLearningPaths(query, language);
}
}
export default learningPathService;