feat(backend): SearchByAdmin gebruikt ingelogde gebruiker
This commit is contained in:
		
							parent
							
								
									bf8d331253
								
							
						
					
					
						commit
						bc459114d8
					
				
					 2 changed files with 19 additions and 17 deletions
				
			
		|  | @ -1,4 +1,4 @@ | ||||||
| import { Request, Response } from 'express'; | import { 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 learningPathService from '../services/learning-paths/learning-path-service.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 { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
| import { Group } from '../entities/assignments/group.entity.js'; | import { Group } from '../entities/assignments/group.entity.js'; | ||||||
| import { getAssignmentRepository, getGroupRepository } from '../data/repositories.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. |  * Fetch learning paths based on query parameters. | ||||||
|  */ |  */ | ||||||
| export async function getLearningPaths(req: Request, res: Response): Promise<void> { | export async function getLearningPaths(req: AuthenticatedRequest, res: Response): Promise<void> { | ||||||
|     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; | ||||||
|  | @ -50,14 +52,18 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi | ||||||
|         return; |         return; | ||||||
|     } else { |     } else { | ||||||
|         hruidList = themes.flatMap((theme) => theme.hruids); |         hruidList = themes.flatMap((theme) => theme.hruids); | ||||||
|         const apiLearningPaths = await learningPathService.fetchLearningPaths(hruidList, language as Language, 'All themes', forGroup); | 
 | ||||||
|         // TODO Remove hardcoding
 |         const apiLearningPathResponse = await learningPathService.fetchLearningPaths(hruidList, language as Language, 'All themes', forGroup); | ||||||
|         const userLearningPaths = await learningPathService.searchLearningPathsByAdmin(['testleerkracht1'], language as Language, forGroup); |         const apiLearningPaths: LearningPath[] = apiLearningPathResponse.data || []; | ||||||
|         if (!apiLearningPaths.data) { |         let allLearningPaths: LearningPath[] = apiLearningPaths; | ||||||
|             res.json(userLearningPaths); | 
 | ||||||
|             return; |         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; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -12,6 +12,7 @@ import { base64ToArrayBuffer } from '../../util/base64-buffer-conversion.js'; | ||||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||||
| import { mapToTeacher } from '../../interfaces/teacher.js'; | import { mapToTeacher } from '../../interfaces/teacher.js'; | ||||||
| import { Collection } from '@mikro-orm/core'; | import { Collection } from '@mikro-orm/core'; | ||||||
|  | import { Teacher } from '../../entities/users/teacher.entity'; | ||||||
| 
 | 
 | ||||||
| const userContentPrefix = getEnvVar(envVars.UserContentPrefix); | const userContentPrefix = getEnvVar(envVars.UserContentPrefix); | ||||||
| const allProviders = [dwengoApiLearningPathProvider, databaseLearningPathProvider]; | const allProviders = [dwengoApiLearningPathProvider, databaseLearningPathProvider]; | ||||||
|  | @ -121,17 +122,12 @@ const learningPathService = { | ||||||
|     async searchLearningPathsByAdmin(adminsIds: string[], language: Language, personalizedFor?: Group): Promise<LearningPath[]> { |     async searchLearningPathsByAdmin(adminsIds: string[], language: Language, personalizedFor?: Group): Promise<LearningPath[]> { | ||||||
|         const teacherRepo = getTeacherRepository(); |         const teacherRepo = getTeacherRepository(); | ||||||
|         const admins = await Promise.all( |         const admins = await Promise.all( | ||||||
|             adminsIds.map(async (adminId) => { |             adminsIds .map(async (adminId) => await teacherRepo.findByUsername(adminId)) | ||||||
|                 const admin = await teacherRepo.findByUsername(adminId); |  | ||||||
|                 if (!admin) { |  | ||||||
|                     throw new Error(`Admin with ID ${adminId} not found.`); |  | ||||||
|                 } |  | ||||||
|                 return admin; |  | ||||||
|             }), |  | ||||||
|         ); |         ); | ||||||
|  |         const adminsNotNull: Teacher[] = admins.filter((admin) => admin !== undefined) as Teacher[]; | ||||||
| 
 | 
 | ||||||
|         const providerResponses = await Promise.all( |         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(); |         return providerResponses.flat(); | ||||||
|     }, |     }, | ||||||
|  |  | ||||||
		Reference in a new issue