feat(backend): SearchByAdmin gebruikt ingelogde gebruiker

This commit is contained in:
Tibo De Peuter 2025-05-17 19:40:28 +02:00
parent bf8d331253
commit bc459114d8
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
2 changed files with 19 additions and 17 deletions

View file

@ -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<void> {
export async function getLearningPaths(req: AuthenticatedRequest, res: Response): Promise<void> {
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<voi
return;
} else {
hruidList = themes.flatMap((theme) => 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;
}

View file

@ -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<LearningPath[]> {
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();
},