diff --git a/backend/src/controllers/learning-paths.ts b/backend/src/controllers/learning-paths.ts index 7cd9e82c..86d2af76 100644 --- a/backend/src/controllers/learning-paths.ts +++ b/backend/src/controllers/learning-paths.ts @@ -1,4 +1,4 @@ -import { Request, Response } from 'express'; +import { Response } from 'express'; import { themes } from '../data/themes.js'; import { FALLBACK_LANG } from '../config.js'; import learningPathService from '../services/learning-paths/learning-path-service.js'; @@ -7,11 +7,13 @@ import { BadRequestException } from '../exceptions/bad-request-exception.js'; import { NotFoundException } from '../exceptions/not-found-exception.js'; import { Group } from '../entities/assignments/group.entity.js'; import { getAssignmentRepository, getGroupRepository } from '../data/repositories.js'; +import { AuthenticatedRequest } from '../middleware/auth/authenticated-request'; +import { LearningPath } from '@dwengo-1/common/interfaces/learning-content'; /** * Fetch learning paths based on query parameters. */ -export async function getLearningPaths(req: Request, res: Response): Promise { +export async function getLearningPaths(req: AuthenticatedRequest, res: Response): Promise { const hruids = req.query.hruid; const themeKey = req.query.theme as string; const searchQuery = req.query.search as string; @@ -50,14 +52,18 @@ export async function getLearningPaths(req: Request, res: Response): Promise theme.hruids); - const apiLearningPaths = await learningPathService.fetchLearningPaths(hruidList, language as Language, 'All themes', forGroup); - // TODO Remove hardcoding - const userLearningPaths = await learningPathService.searchLearningPathsByAdmin(['testleerkracht1'], language as Language, forGroup); - if (!apiLearningPaths.data) { - res.json(userLearningPaths); - return; + + const apiLearningPathResponse = await learningPathService.fetchLearningPaths(hruidList, language as Language, 'All themes', forGroup); + const apiLearningPaths: LearningPath[] = apiLearningPathResponse.data || []; + let allLearningPaths: LearningPath[] = apiLearningPaths; + + if (req.auth) { + const adminUsername = req.auth.username; + const userLearningPaths = await learningPathService.searchLearningPathsByAdmin([adminUsername], language as Language, forGroup) || []; + allLearningPaths = apiLearningPaths.concat(userLearningPaths); } - res.json(apiLearningPaths.data.concat(userLearningPaths)); + + res.json(allLearningPaths); return; } diff --git a/backend/src/services/learning-paths/learning-path-service.ts b/backend/src/services/learning-paths/learning-path-service.ts index b0985c05..68051bb2 100644 --- a/backend/src/services/learning-paths/learning-path-service.ts +++ b/backend/src/services/learning-paths/learning-path-service.ts @@ -12,6 +12,7 @@ import { base64ToArrayBuffer } from '../../util/base64-buffer-conversion.js'; import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; import { mapToTeacher } from '../../interfaces/teacher.js'; import { Collection } from '@mikro-orm/core'; +import { Teacher } from '../../entities/users/teacher.entity'; const userContentPrefix = getEnvVar(envVars.UserContentPrefix); const allProviders = [dwengoApiLearningPathProvider, databaseLearningPathProvider]; @@ -121,17 +122,12 @@ const learningPathService = { async searchLearningPathsByAdmin(adminsIds: string[], language: Language, personalizedFor?: Group): Promise { const teacherRepo = getTeacherRepository(); const admins = await Promise.all( - adminsIds.map(async (adminId) => { - const admin = await teacherRepo.findByUsername(adminId); - if (!admin) { - throw new Error(`Admin with ID ${adminId} not found.`); - } - return admin; - }), + adminsIds .map(async (adminId) => await teacherRepo.findByUsername(adminId)) ); + const adminsNotNull: Teacher[] = admins.filter((admin) => admin !== undefined) as Teacher[]; const providerResponses = await Promise.all( - allProviders.map(async (provider) => provider.searchLearningPathsByAdmin(admins, language, personalizedFor)), + allProviders.map(async (provider) => provider.searchLearningPathsByAdmin(adminsNotNull, language, personalizedFor)), ); return providerResponses.flat(); },