feat(backend): opvragen van leerobjecten van een leerkracht

This commit is contained in:
Gerald Schmittinger 2025-05-11 15:46:53 +02:00
parent 78353d6b65
commit 6600441b08
11 changed files with 152 additions and 38 deletions

View file

@ -109,6 +109,17 @@ const databaseLearningObjectProvider: LearningObjectProvider = {
);
return learningObjects.filter((it) => it !== null);
},
/**
* Returns all learning objects containing the given username as an admin.
*/
async getLearningObjectsAdministratedBy(adminUsername: string): Promise<FilteredLearningObject[]> {
const learningObjectRepo = getLearningObjectRepository();
const learningObjects = await learningObjectRepo.findAllByAdmin(adminUsername);
return learningObjects
.map(it => convertLearningObject(it))
.filter(it => it != null);
}
};
export default databaseLearningObjectProvider;

View file

@ -135,6 +135,13 @@ const dwengoApiLearningObjectProvider: LearningObjectProvider = {
return html;
},
/**
* Obtain all learning objects who have the user with the given username as an admin.
*/
async getLearningObjectsAdministratedBy(_adminUsername: string): Promise<FilteredLearningObject[]> {
return []; // The dwengo database does not contain any learning objects administrated by users.
}
};
export default dwengoApiLearningObjectProvider;

View file

@ -20,4 +20,9 @@ export interface LearningObjectProvider {
* Obtain a HTML-rendering of the learning object with the given identifier (as a string).
*/
getLearningObjectHTML(id: LearningObjectIdentifierDTO): Promise<string | null>;
/**
* Obtain all learning object who have the user with the given username as an admin.
*/
getLearningObjectsAdministratedBy(username: string): Promise<FilteredLearningObject[]>;
}

View file

@ -7,9 +7,10 @@ import {
LearningObjectIdentifierDTO,
LearningPathIdentifier
} from '@dwengo-1/common/interfaces/learning-content';
import {getLearningObjectRepository} from "../../data/repositories";
import {getLearningObjectRepository, getTeacherRepository} from "../../data/repositories";
import {processLearningObjectZip} from "./learning-object-zip-processing-service";
import {BadRequestException} from "../../exceptions/bad-request-exception";
import {LearningObject} from "../../entities/content/learning-object.entity";
function getProvider(id: LearningObjectIdentifierDTO): LearningObjectProvider {
if (id.hruid.startsWith(getEnvVar(envVars.UserContentPrefix))) {
@ -50,19 +51,40 @@ const learningObjectService = {
return getProvider(id).getLearningObjectHTML(id);
},
/**
* Obtain all learning objects administrated by the user with the given username.
*/
async getLearningObjectsAdministratedBy(adminUsername: string): Promise<FilteredLearningObject[]> {
return databaseLearningObjectProvider.getLearningObjectsAdministratedBy(adminUsername);
},
/**
* Store the learning object in the given zip file in the database.
* @param learningObjectPath The path where the uploaded learning object resides.
* @param admins The usernames of the users which should be administrators of the learning object.
*/
async storeLearningObject(learningObjectPath: string): Promise<void> {
async storeLearningObject(learningObjectPath: string, admins: string[]): Promise<LearningObject> {
const learningObjectRepository = getLearningObjectRepository();
const learningObject = await processLearningObjectZip(learningObjectPath);
console.log(learningObject);
if (!learningObject.hruid.startsWith(getEnvVar(envVars.UserContentPrefix))) {
throw new BadRequestException("Learning object name must start with the user content prefix!");
learningObject.hruid = getEnvVar(envVars.UserContentPrefix) + learningObject.hruid;
}
// Lookup the admin teachers based on their usernames and add them to the admins of the learning object.
const teacherRepo = getTeacherRepository();
const adminTeachers = await Promise.all(
admins.map(it => teacherRepo.findByUsername(it))
);
adminTeachers.forEach(it => {
if (it != null) {
learningObject.admins.add(it);
}
});
await learningObjectRepository.save(learningObject, {preventOverwrite: true});
return learningObject;
}
};

View file

@ -5,6 +5,9 @@ import {getAttachmentRepository, getLearningObjectRepository} from "../../data/r
import {BadRequestException} from "../../exceptions/bad-request-exception";
import {LearningObjectMetadata} from "@dwengo-1/common/dist/interfaces/learning-content";
const METADATA_PATH_REGEX = /.*[/^]metadata\.json$/;
const CONTENT_PATH_REGEX = /.*[/^]content\.[a-zA-Z]*$/;
/**
* Process an uploaded zip file and construct a LearningObject from its contents.
* @param filePath Path of the zip file to process.
@ -15,40 +18,62 @@ export async function processLearningObjectZip(filePath: string): Promise<Learni
const zip = await unzipper.Open.file(filePath);
let metadata: LearningObjectMetadata | null = null;
let metadata: LearningObjectMetadata | undefined = undefined;
const attachments: {name: string, content: Buffer}[] = [];
let content: Buffer | null = null;
let content: Buffer | undefined = undefined;
if (zip.files.length == 0) {
throw new BadRequestException("empty_zip")
}
for (const file of zip.files) {
if (file.type === "Directory") {
throw new BadRequestException("The learning object zip file should not contain directories.");
} else if (file.path === "metadata.json") {
metadata = await processMetadataJson(file);
} else if (file.path.startsWith("index.")) {
content = await processFile(file);
} else {
attachments.push({
name: file.path,
content: await processFile(file)
});
if (file.type !== "Directory") {
if (METADATA_PATH_REGEX.test(file.path)) {
metadata = await processMetadataJson(file);
} else if (CONTENT_PATH_REGEX.test(file.path)) {
content = await processFile(file);
} else {
attachments.push({
name: file.path,
content: await processFile(file)
});
}
}
}
if (!metadata) {
throw new BadRequestException("Missing metadata.json file");
throw new BadRequestException("missing_metadata");
}
if (!content) {
throw new BadRequestException("Missing index file");
throw new BadRequestException("missing_index");
}
const learningObject = learningObjectRepo.create(metadata);
const learningObject = learningObjectRepo.create({
admins: [],
available: metadata.available ?? true,
content: content,
contentType: metadata.content_type,
copyright: metadata.copyright,
description: metadata.description,
educationalGoals: metadata.educational_goals,
hruid: metadata.hruid,
keywords: metadata.keywords,
language: metadata.language,
license: "",
returnValue: metadata.return_value,
skosConcepts: metadata.skos_concepts,
teacherExclusive: metadata.teacher_exclusive,
title: metadata.title,
version: metadata.version
});
const attachmentEntities = attachments.map(it => attachmentRepo.create({
name: it.name,
content: it.content,
mimeType: mime.lookup(it.name) || "text/plain",
learningObject
}))
learningObject.attachments.push(...attachmentEntities);
attachmentEntities.forEach(it => learningObject.attachments.add(it));
return learningObject;
}