Merge remote-tracking branch 'origin/dev' into feat/endpoints-beschermen-met-authenticatie-#105

# Conflicts:
#	backend/src/controllers/questions.ts
#	backend/src/data/questions/question-repository.ts
#	backend/src/interfaces/question.ts
#	backend/src/services/questions.ts
#	common/src/interfaces/question.ts
This commit is contained in:
Gerald Schmittinger 2025-04-09 12:50:02 +02:00
commit 8c387d6811
59 changed files with 2100 additions and 292 deletions

View file

@ -7,7 +7,7 @@
"main": "dist/app.js",
"scripts": {
"build": "cross-env NODE_ENV=production tsc --build",
"dev": "cross-env NODE_ENV=development tsx watch --env-file=.env.development.local src/app.ts",
"dev": "cross-env NODE_ENV=development tsx tool/seed.ts; tsx watch --env-file=.env.development.local src/app.ts",
"start": "cross-env NODE_ENV=production node --env-file=.env dist/app.js",
"format": "prettier --write src/",
"format-check": "prettier --check src/",

View file

@ -5,3 +5,4 @@ export const DWENGO_API_BASE = getEnvVar(envVars.LearningContentRepoApiBaseUrl);
export const FALLBACK_LANG = getEnvVar(envVars.FallbackLanguage);
export const FALLBACK_SEQ_NUM = 1;
export const FALLBACK_VERSION_NUM = 1;

View file

@ -0,0 +1,99 @@
import { Request, Response } from 'express';
import { requireFields } from './error-helper.js';
import { getLearningObjectId, getQuestionId } from './questions.js';
import { createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswer } from '../services/answers.js';
import { FALLBACK_SEQ_NUM } from '../config.js';
import { AnswerData } from '@dwengo-1/common/interfaces/answer';
export async function getAllAnswersHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const full = req.query.full === 'true';
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const answers = await getAnswersByQuestion(questionId, full);
res.json({ answers });
}
export async function getAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await getAnswer(questionId, sequenceNumber);
res.json({ answer });
}
export async function createAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const author = req.body.author as string;
const content = req.body.content as string;
requireFields({ author, content });
const answerData = req.body as AnswerData;
const answer = await createAnswer(questionId, answerData);
res.json({ answer });
}
export async function deleteAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await deleteAnswer(questionId, sequenceNumber);
res.json({ answer });
}
export async function updateAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const content = req.body.content as string;
requireFields({ content });
const answerData = req.body as AnswerData;
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await updateAnswer(questionId, sequenceNumber, answerData);
res.json({ answer });
}

View file

@ -28,7 +28,7 @@ export async function createClassHandler(req: Request, res: Response): Promise<v
return;
}
res.status(201).json(cls);
res.status(201).json({ class: cls });
}
export async function getClassHandler(req: Request, res: Response): Promise<void> {
@ -40,7 +40,7 @@ export async function getClassHandler(req: Request, res: Response): Promise<void
return;
}
res.json(cls);
res.json({ class: cls });
}
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {

View file

@ -6,9 +6,9 @@ import attachmentService from '../services/learning-objects/attachment-service.j
import { BadRequestException } from '../exceptions/bad-request-exception.js';
import { NotFoundException } from '../exceptions/not-found-exception.js';
import { envVars, getEnvVar } from '../util/envVars.js';
import { FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifier {
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifierDTO {
if (!req.params.hruid) {
throw new BadRequestException('HRUID is required.');
}

View file

@ -5,51 +5,35 @@ import {
getAllQuestions,
getAnswersByQuestion,
getQuestion,
getQuestionsAboutLearningObjectInAssignment,
getQuestionsAboutLearningObjectInAssignment, updateQuestion,
} from '../services/questions.js';
import { FALLBACK_LANG, FALLBACK_SEQ_NUM } from '../config.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { Language } from '@dwengo-1/common/util/language';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
interface QuestionPathParams {
hruid: string;
version: string;
}
interface QuestionQueryParams {
lang: string;
}
function getObjectId<ResBody, ReqBody>(
req: Request<QuestionPathParams, ResBody, ReqBody, QuestionQueryParams>,
res: Response
): LearningObjectIdentifier | null {
const { hruid, version } = req.params;
const lang = req.query.lang;
if (!hruid || !version) {
res.status(400).json({ error: 'Missing required parameters.' });
return null;
}
import {requireFields} from "./error-helper";
export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier {
return {
hruid,
language: (lang as Language) || FALLBACK_LANG,
version: Number(version),
language: (lang || FALLBACK_LANG) as Language,
version: Number(version) || FALLBACK_VERSION_NUM,
};
}
interface GetQuestionIdPathParams extends QuestionPathParams {
seq: string;
export function getQuestionId(learningObjectIdentifier: LearningObjectIdentifier, seq: string): QuestionId {
return {
learningObjectIdentifier,
sequenceNumber: seq ? Number(seq) : FALLBACK_SEQ_NUM,
};
}
function getQuestionId<ReqBody, ResBody>(
req: Request<GetQuestionIdPathParams, ReqBody, ResBody, QuestionQueryParams>,
res: Response
): QuestionId | null {
function getQuestionId(req: Request, res: Response): QuestionId | null {
const seq = req.params.seq;
const learningObjectIdentifier = getObjectId(req, res);
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const learningObjectIdentifier = getLearningObjectId(hruid, version, language);
if (!learningObjectIdentifier) {
return null;
@ -61,117 +45,117 @@ function getQuestionId<ReqBody, ResBody>(
};
}
interface GetAllQuestionsQueryParams extends QuestionQueryParams {
classId?: string;
assignmentId?: number;
forStudent?: string;
full?: boolean;
}
export async function getAllQuestionsHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const full = req.query.full === 'true';
requireFields({ hruid });
export async function getAllQuestionsHandler(
req: Request<QuestionPathParams, QuestionDTO[] | QuestionId[], unknown, GetAllQuestionsQueryParams>,
res: Response
): Promise<void> {
const objectId = getObjectId(req, res);
const full = req.query.full;
const learningObjectId = getLearningObjectId(hruid, version, language);
if (!objectId) {
return;
}
let questions: QuestionDTO[] | QuestionId[];
if (req.query.classId && req.query.assignmentId) {
questions = await getQuestionsAboutLearningObjectInAssignment(
objectId,
learningObjectId,
req.query.classId,
req.query.assignmentId,
full ?? false,
req.query.forStudent
);
} else {
questions = await getAllQuestions(objectId, full ?? false);
questions = await getAllQuestions(learningObjectId, full ?? false);
}
if (!questions) {
res.status(404).json({ error: `Questions not found.` });
} else {
res.json({ questions: questions });
}
res.json({ questions });
}
export async function getQuestionHandler(
req: Request<GetQuestionIdPathParams, QuestionDTO[] | QuestionId[], unknown, QuestionQueryParams>,
res: Response
): Promise<void> {
const questionId = getQuestionId(req, res);
export async function getQuestionHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid });
if (!questionId) {
return;
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const question = await getQuestion(questionId);
res.json({ question });
}
const question = await getQuestion(questionId);
export async function getQuestionAnswersHandler(
req: Request<GetQuestionIdPathParams, { answers: AnswerDTO[] | AnswerId[] }, unknown, GetQuestionAnswersQueryParams>,
res: Response
): Promise<void> {
const questionId = getQuestionId(req, res);
const full = req.query.full;
if (!question) {
res.status(404).json({ error: `Question not found.` });
} else {
res.json(question);
if (!questionId) {
return;
}
const answers = await getAnswersByQuestion(questionId, full);
if (!answers) {
res.status(404).json({ error: `Questions not found` });
} else {
res.json({ answers: answers });
}
}
}
interface GetQuestionAnswersQueryParams extends QuestionQueryParams {
full: boolean;
}
export async function getQuestionAnswersHandler(
req: Request<GetQuestionIdPathParams, { answers: AnswerDTO[] | AnswerId[] }, unknown, GetQuestionAnswersQueryParams>,
res: Response
): Promise<void> {
const questionId = getQuestionId(req, res);
const full = req.query.full;
if (!questionId) {
return;
}
const answers = await getAnswersByQuestion(questionId, full);
if (!answers) {
res.status(404).json({ error: `Questions not found` });
} else {
res.json({ answers: answers });
}
}
export async function createQuestionHandler(req: Request, res: Response): Promise<void> {
const questionDTO = req.body as QuestionDTO;
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
requireFields({ hruid });
if (!questionDTO.learningObjectIdentifier || !questionDTO.author || !questionDTO.inGroup || !questionDTO.content) {
res.status(400).json({ error: 'Missing required fields: identifier, author, inGroup, and content' });
return;
}
const loId = getLearningObjectId(hruid, version, language);
const question = await createQuestion(questionDTO);
const author = req.body.author as string;
const content = req.body.content as string;
const inGroup = req.body.inGroup as string;
requireFields({ author, content, inGroup });
if (!question) {
res.status(400).json({ error: 'Could not create question' });
} else {
res.json(question);
}
const questionData = req.body as QuestionData;
const question = await createQuestion(loId, questionData);
res.json({ question });
}
export async function deleteQuestionHandler(
req: Request<GetQuestionIdPathParams, QuestionDTO, unknown, QuestionQueryParams>,
res: Response
): Promise<void> {
const questionId = getQuestionId(req, res);
export async function deleteQuestionHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid });
if (!questionId) {
return;
}
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const question = await deleteQuestion(questionId);
if (!question) {
res.status(400).json({ error: 'Could not find nor delete question' });
} else {
res.json(question);
}
res.json({ question });
}
export async function updateQuestionHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid });
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const content = req.body.content as string;
requireFields({ content });
const questionData = req.body as QuestionData;
const question = await updateQuestion(questionId, questionData);
res.json({ question });
}

View file

@ -2,6 +2,7 @@ import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
import { Answer } from '../../entities/questions/answer.entity.js';
import { Question } from '../../entities/questions/question.entity.js';
import { Teacher } from '../../entities/users/teacher.entity.js';
import { Loaded } from '@mikro-orm/core';
export class AnswerRepository extends DwengoEntityRepository<Answer> {
public async createAnswer(answer: { toQuestion: Question; author: Teacher; content: string }): Promise<Answer> {
@ -19,10 +20,21 @@ export class AnswerRepository extends DwengoEntityRepository<Answer> {
orderBy: { sequenceNumber: 'ASC' },
});
}
public async findAnswer(question: Question, sequenceNumber: number): Promise<Loaded<Answer> | null> {
return this.findOne({
toQuestion: question,
sequenceNumber,
});
}
public async removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise<void> {
return this.deleteWhere({
toQuestion: question,
sequenceNumber: sequenceNumber,
});
}
public async updateContent(answer: Answer, newContent: string): Promise<Answer> {
answer.content = newContent;
await this.save(answer);
return answer;
}
}

View file

@ -5,6 +5,7 @@ import { Student } from '../../entities/users/student.entity.js';
import { LearningObject } from '../../entities/content/learning-object.entity.js';
import { Group } from '../../entities/assignments/group.entity';
import { Assignment } from '../../entities/assignments/assignment.entity';
import { Loaded } from '@mikro-orm/core';
export class QuestionRepository extends DwengoEntityRepository<Question> {
public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; inGroup: Group; content: string }): Promise<Question> {
@ -66,6 +67,21 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
});
}
public async findByLearningObjectAndSequenceNumber(loId: LearningObjectIdentifier, sequenceNumber: number): Promise<Loaded<Question> | null> {
return this.findOne({
learningObjectHruid: loId.hruid,
learningObjectLanguage: loId.language,
learningObjectVersion: loId.version,
sequenceNumber,
});
}
public async updateContent(question: Question, newContent: string): Promise<Question> {
question.content = newContent;
await this.save(question);
return question;
}
/**
* Looks up all questions for the given learning object which were asked as part of the given assignment.
* When forStudentUsername is set, only the questions within the given user's group are shown.

View file

@ -1,14 +1,14 @@
import { mapToUserDTO } from './user.js';
import { mapToQuestionDTO, mapToQuestionDTOId } from './question.js';
import { Answer } from '../entities/questions/answer.entity.js';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import { mapToTeacherDTO } from './teacher.js';
/**
* Convert a Question entity to a DTO format.
*/
export function mapToAnswerDTO(answer: Answer): AnswerDTO {
return {
author: mapToUserDTO(answer.author),
author: mapToTeacherDTO(answer.author),
toQuestion: mapToQuestionDTO(answer.toQuestion),
sequenceNumber: answer.sequenceNumber!,
timestamp: answer.timestamp.toISOString(),

View file

@ -3,8 +3,9 @@ import { mapToStudentDTO } from './student.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { LearningObjectIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { mapToGroupDTOId } from './group';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
function getLearningObjectIdentifier(question: Question): LearningObjectIdentifier {
function getLearningObjectIdentifier(question: Question): LearningObjectIdentifierDTO {
return {
hruid: question.learningObjectHruid,
language: question.learningObjectLanguage,
@ -12,6 +13,14 @@ function getLearningObjectIdentifier(question: Question): LearningObjectIdentifi
};
}
export function mapToLearningObjectID(loID: LearningObjectIdentifierDTO): LearningObjectIdentifier {
return {
hruid: loID.hruid,
language: loID.language,
version: loID.version ?? 1,
};
}
/**
* Convert a Question entity to a DTO format.
*/

View file

@ -1,10 +1,10 @@
import { EntityManager, MikroORM } from '@mikro-orm/core';
import { EntityManager, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
import config from './mikro-orm.config.js';
import { envVars, getEnvVar } from './util/envVars.js';
import { getLogger, Logger } from './logging/initalize.js';
let orm: MikroORM | undefined;
export async function initORM(testingMode = false): Promise<void> {
export async function initORM(testingMode = false): Promise<MikroORM<IDatabaseDriver, EntityManager>> {
const logger: Logger = getLogger();
logger.info('Initializing ORM');
@ -25,6 +25,8 @@ export async function initORM(testingMode = false): Promise<void> {
);
}
}
return orm;
}
export function forkEntityManager(): EntityManager {
if (!orm) {

View file

@ -0,0 +1,16 @@
import express from 'express';
import { createAnswerHandler, deleteAnswerHandler, getAnswerHandler, getAllAnswersHandler, updateAnswerHandler } from '../controllers/answers.js';
const router = express.Router({ mergeParams: true });
router.get('/', getAllAnswersHandler);
router.post('/', createAnswerHandler);
router.get('/:seqAnswer', getAnswerHandler);
router.delete('/:seqAnswer', deleteAnswerHandler);
router.put('/:seqAnswer', updateAnswerHandler);
export default router;

View file

@ -1,11 +1,7 @@
import express from 'express';
import {
createQuestionHandler,
deleteQuestionHandler,
getAllQuestionsHandler,
getQuestionAnswersHandler,
getQuestionHandler,
} from '../controllers/questions.js';
import { createQuestionHandler, deleteQuestionHandler, getAllQuestionsHandler, getQuestionHandler } from '../controllers/questions.js';
import answerRoutes from './answers.js';
const router = express.Router({ mergeParams: true });
// Query language
@ -20,6 +16,6 @@ router.delete('/:seq', deleteQuestionHandler);
// Information about a question with id
router.get('/:seq', getQuestionHandler);
router.get('/answers/:seq', getQuestionAnswersHandler);
router.use('/:seq/answers', answerRoutes);
export default router;

View file

@ -0,0 +1,70 @@
import { getAnswerRepository } from '../data/repositories.js';
import { Answer } from '../entities/questions/answer.entity.js';
import { mapToAnswerDTO, mapToAnswerDTOId } from '../interfaces/answer.js';
import { fetchTeacher } from './teachers.js';
import { fetchQuestion } from './questions.js';
import { QuestionId } from '@dwengo-1/common/interfaces/question';
import { AnswerData, AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import { NotFoundException } from '../exceptions/not-found-exception.js';
export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
if (full) {
return answers.map(mapToAnswerDTO);
}
return answers.map(mapToAnswerDTOId);
}
export async function createAnswer(questionId: QuestionId, answerData: AnswerData): Promise<AnswerDTO> {
const answerRepository = getAnswerRepository();
const toQuestion = await fetchQuestion(questionId);
const author = await fetchTeacher(answerData.author);
const content = answerData.content;
const answer = await answerRepository.createAnswer({
toQuestion,
author,
content,
});
return mapToAnswerDTO(answer);
}
async function fetchAnswer(questionId: QuestionId, sequenceNumber: number): Promise<Answer> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answer = await answerRepository.findAnswer(question, sequenceNumber);
if (!answer) {
throw new NotFoundException('Answer with questionID and sequence number not found');
}
return answer;
}
export async function getAnswer(questionId: QuestionId, sequenceNumber: number): Promise<AnswerDTO> {
const answer = await fetchAnswer(questionId, sequenceNumber);
return mapToAnswerDTO(answer);
}
export async function deleteAnswer(questionId: QuestionId, sequenceNumber: number): Promise<AnswerDTO> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answer = await fetchAnswer(questionId, sequenceNumber);
await answerRepository.removeAnswerByQuestionAndSequenceNumber(question, sequenceNumber);
return mapToAnswerDTO(answer);
}
export async function updateAnswer(questionId: QuestionId, sequenceNumber: number, answerData: AnswerData): Promise<AnswerDTO> {
const answerRepository = getAnswerRepository();
const answer = await fetchAnswer(questionId, sequenceNumber);
const newAnswer = await answerRepository.updateContent(answer, answerData.content);
return mapToAnswerDTO(newAnswer);
}

View file

@ -1,10 +1,10 @@
import { getAttachmentRepository } from '../../data/repositories.js';
import { Attachment } from '../../entities/content/attachment.entity.js';
import { LearningObjectIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
const attachmentService = {
async getAttachment(learningObjectId: LearningObjectIdentifier, attachmentName: string): Promise<Attachment | null> {
async getAttachment(learningObjectId: LearningObjectIdentifierDTO, attachmentName: string): Promise<Attachment | null> {
const attachmentRepo = getAttachmentRepository();
if (learningObjectId.version) {

View file

@ -6,7 +6,7 @@ import processingService from './processing/processing-service.js';
import { NotFoundError } from '@mikro-orm/core';
import learningObjectService from './learning-object-service.js';
import { getLogger, Logger } from '../../logging/initalize.js';
import { FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
const logger: Logger = getLogger();
@ -40,7 +40,7 @@ function convertLearningObject(learningObject: LearningObject | null): FilteredL
};
}
async function findLearningObjectEntityById(id: LearningObjectIdentifier): Promise<LearningObject | null> {
async function findLearningObjectEntityById(id: LearningObjectIdentifierDTO): Promise<LearningObject | null> {
const learningObjectRepo = getLearningObjectRepository();
return learningObjectRepo.findLatestByHruidAndLanguage(id.hruid, id.language);
@ -53,7 +53,7 @@ const databaseLearningObjectProvider: LearningObjectProvider = {
/**
* Fetches a single learning object by its HRUID
*/
async getLearningObjectById(id: LearningObjectIdentifier): Promise<FilteredLearningObject | null> {
async getLearningObjectById(id: LearningObjectIdentifierDTO): Promise<FilteredLearningObject | null> {
const learningObject = await findLearningObjectEntityById(id);
return convertLearningObject(learningObject);
},
@ -61,7 +61,7 @@ const databaseLearningObjectProvider: LearningObjectProvider = {
/**
* Obtain a HTML-rendering of the learning object with the given identifier (as a string).
*/
async getLearningObjectHTML(id: LearningObjectIdentifier): Promise<string | null> {
async getLearningObjectHTML(id: LearningObjectIdentifierDTO): Promise<string | null> {
const learningObjectRepo = getLearningObjectRepository();
const learningObject = await learningObjectRepo.findLatestByHruidAndLanguage(id.hruid, id.language);

View file

@ -5,7 +5,7 @@ import { LearningObjectProvider } from './learning-object-provider.js';
import { getLogger, Logger } from '../../logging/initalize.js';
import {
FilteredLearningObject,
LearningObjectIdentifier,
LearningObjectIdentifierDTO,
LearningObjectMetadata,
LearningObjectNode,
LearningPathIdentifier,
@ -67,7 +67,7 @@ async function fetchLearningObjects(learningPathId: LearningPathIdentifier, full
const objects = await Promise.all(
nodes.map(async (node) => {
const learningObjectId: LearningObjectIdentifier = {
const learningObjectId: LearningObjectIdentifierDTO = {
hruid: node.learningobject_hruid,
language: learningPathId.language,
};
@ -85,7 +85,7 @@ const dwengoApiLearningObjectProvider: LearningObjectProvider = {
/**
* Fetches a single learning object by its HRUID
*/
async getLearningObjectById(id: LearningObjectIdentifier): Promise<FilteredLearningObject | null> {
async getLearningObjectById(id: LearningObjectIdentifierDTO): Promise<FilteredLearningObject | null> {
const metadataUrl = `${DWENGO_API_BASE}/learningObject/getMetadata`;
const metadata = await fetchWithLogging<LearningObjectMetadata>(
metadataUrl,
@ -121,7 +121,7 @@ const dwengoApiLearningObjectProvider: LearningObjectProvider = {
* Obtain a HTML-rendering of the learning object with the given identifier (as a string). For learning objects
* from the Dwengo API, this means passing through the HTML rendering from there.
*/
async getLearningObjectHTML(id: LearningObjectIdentifier): Promise<string | null> {
async getLearningObjectHTML(id: LearningObjectIdentifierDTO): Promise<string | null> {
const htmlUrl = `${DWENGO_API_BASE}/learningObject/getRaw`;
const html = await fetchWithLogging<string>(htmlUrl, `Metadata for Learning Object HRUID "${id.hruid}" (language ${id.language})`, {
params: { ...id },

View file

@ -1,10 +1,10 @@
import { FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
export interface LearningObjectProvider {
/**
* Fetches a single learning object by its HRUID
*/
getLearningObjectById(id: LearningObjectIdentifier): Promise<FilteredLearningObject | null>;
getLearningObjectById(id: LearningObjectIdentifierDTO): Promise<FilteredLearningObject | null>;
/**
* Fetch full learning object data (metadata)
@ -19,5 +19,5 @@ export interface LearningObjectProvider {
/**
* Obtain a HTML-rendering of the learning object with the given identifier (as a string).
*/
getLearningObjectHTML(id: LearningObjectIdentifier): Promise<string | null>;
getLearningObjectHTML(id: LearningObjectIdentifierDTO): Promise<string | null>;
}

View file

@ -2,9 +2,9 @@ import dwengoApiLearningObjectProvider from './dwengo-api-learning-object-provid
import { LearningObjectProvider } from './learning-object-provider.js';
import { envVars, getEnvVar } from '../../util/envVars.js';
import databaseLearningObjectProvider from './database-learning-object-provider.js';
import { FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
function getProvider(id: LearningObjectIdentifier): LearningObjectProvider {
function getProvider(id: LearningObjectIdentifierDTO): LearningObjectProvider {
if (id.hruid.startsWith(getEnvVar(envVars.UserContentPrefix))) {
return databaseLearningObjectProvider;
}
@ -18,7 +18,7 @@ const learningObjectService = {
/**
* Fetches a single learning object by its HRUID
*/
async getLearningObjectById(id: LearningObjectIdentifier): Promise<FilteredLearningObject | null> {
async getLearningObjectById(id: LearningObjectIdentifierDTO): Promise<FilteredLearningObject | null> {
return getProvider(id).getLearningObjectById(id);
},
@ -39,7 +39,7 @@ const learningObjectService = {
/**
* Obtain a HTML-rendering of the learning object with the given identifier (as a string).
*/
async getLearningObjectHTML(id: LearningObjectIdentifier): Promise<string | null> {
async getLearningObjectHTML(id: LearningObjectIdentifierDTO): Promise<string | null> {
return getProvider(id).getLearningObjectHTML(id);
},
};

View file

@ -12,7 +12,7 @@ import Image = marked.Tokens.Image;
import Heading = marked.Tokens.Heading;
import Link = marked.Tokens.Link;
import RendererObject = marked.RendererObject;
import { LearningObjectIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
const prefixes = {
@ -25,7 +25,7 @@ const prefixes = {
blockly: '@blockly',
};
function extractLearningObjectIdFromHref(href: string): LearningObjectIdentifier {
function extractLearningObjectIdFromHref(href: string): LearningObjectIdentifierDTO {
const [hruid, language, version] = href.split(/\/(.+)/, 2)[1].split('/');
return {
hruid,

View file

@ -14,7 +14,7 @@ import { LearningObject } from '../../../entities/content/learning-object.entity
import Processor from './processor.js';
import { DwengoContentType } from './content-type.js';
import { replaceAsync } from '../../../util/async.js';
import { LearningObjectIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
const EMBEDDED_LEARNING_OBJECT_PLACEHOLDER = /<learning-object hruid="([^"]+)" language="([^"]+)" version="([^"]+)"\/>/g;
@ -50,7 +50,7 @@ class ProcessingService {
*/
async render(
learningObject: LearningObject,
fetchEmbeddedLearningObjects?: (loId: LearningObjectIdentifier) => Promise<LearningObject | null>
fetchEmbeddedLearningObjects?: (loId: LearningObjectIdentifierDTO) => Promise<LearningObject | null>
): Promise<string> {
const html = this.processors.get(learningObject.contentType)!.renderLearningObject(learningObject);
if (fetchEmbeddedLearningObjects) {

View file

@ -1,5 +1,10 @@
import { getAnswerRepository, getAssignmentRepository, getClassRepository, getGroupRepository, getQuestionRepository } from '../data/repositories.js';
import { mapToQuestionDTO, mapToQuestionDTOId } from '../interfaces/question.js';
import {
getAnswerRepository, getAssignmentRepository,
getClassRepository,
getGroupRepository,
getQuestionRepository
} from '../data/repositories.js';
import {mapToLearningObjectID, mapToQuestionDTO, mapToQuestionDTOId} from '../interfaces/question.js';
import { Question } from '../entities/questions/question.entity.js';
import { Answer } from '../entities/questions/answer.entity.js';
import { mapToAnswerDTO, mapToAnswerDTOId } from '../interfaces/answer.js';
@ -8,8 +13,9 @@ import { LearningObjectIdentifier } from '../entities/content/learning-object-id
import { mapToStudent } from '../interfaces/student.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
import { mapToAssignment } from '../interfaces/assignment';
import {fetchStudent} from "./students";
import {mapToAssignment} from "../interfaces/assignment";
import { NotFoundException } from '../exceptions/not-found-exception.js';
export async function getQuestionsAboutLearningObjectInAssignment(
loId: LearningObjectIdentifier,
@ -32,10 +38,6 @@ export async function getAllQuestions(id: LearningObjectIdentifier, full: boolea
const questionRepository: QuestionRepository = getQuestionRepository();
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id);
if (!questions) {
return [];
}
if (full) {
return questions.map(mapToQuestionDTO);
}
@ -43,24 +45,22 @@ export async function getAllQuestions(id: LearningObjectIdentifier, full: boolea
return questions.map(mapToQuestionDTOId);
}
async function fetchQuestion(questionId: QuestionId): Promise<Question | null> {
export async function fetchQuestion(questionId: QuestionId): Promise<Question> {
const questionRepository = getQuestionRepository();
return await questionRepository.findOne({
learningObjectHruid: questionId.learningObjectIdentifier.hruid,
learningObjectLanguage: questionId.learningObjectIdentifier.language,
learningObjectVersion: questionId.learningObjectIdentifier.version,
sequenceNumber: questionId.sequenceNumber,
});
}
export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO | null> {
const question = await fetchQuestion(questionId);
const question = await questionRepository.findByLearningObjectAndSequenceNumber(
mapToLearningObjectID(questionId.learningObjectIdentifier),
questionId.sequenceNumber
);
if (!question) {
return null;
throw new NotFoundException('Question with loID and sequence number not found');
}
return question;
}
export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO> {
const question = await fetchQuestion(questionId);
return mapToQuestionDTO(question);
}
@ -85,53 +85,44 @@ export async function getAnswersByQuestion(questionId: QuestionId, full: boolean
return answers.map(mapToAnswerDTOId);
}
export async function createQuestion(questionDTO: QuestionDTO): Promise<QuestionDTO | null> {
export async function createQuestion(loId: LearningObjectIdentifier, questionData: QuestionData): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const author = mapToStudent(questionDTO.author);
const loId: LearningObjectIdentifier = {
...questionDTO.learningObjectIdentifier,
version: questionDTO.learningObjectIdentifier.version ?? 1,
};
const author = await fetchStudent(questionData.author!);
const content = questionData.content;
const clazz = await getClassRepository().findById((questionDTO.inGroup.assignment as AssignmentDTO).class);
let questionDTO;
const assignment = mapToAssignment(questionDTO.inGroup.assignment as AssignmentDTO, clazz!);
const inGroup = await getGroupRepository().findByAssignmentAndGroupNumber(assignment, questionDTO.inGroup.groupNumber);
try {
await questionRepository.createQuestion({
loId,
author,
inGroup: inGroup!,
content: questionDTO.content,
});
} catch (_) {
return null;
}
return questionDTO;
}
export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDTO | null> {
const questionRepository = getQuestionRepository();
const question = await fetchQuestion(questionId);
if (!question) {
return null;
}
const loId: LearningObjectIdentifier = {
...questionId.learningObjectIdentifier,
version: questionId.learningObjectIdentifier.version ?? 1,
};
try {
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber);
} catch (_) {
return null;
}
const question = await questionRepository.createQuestion({
loId,
inGroup,
author,
content,
});
return mapToQuestionDTO(question);
}
export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const question = await fetchQuestion(questionId); // Throws error if not found
const loId: LearningObjectIdentifier = {
hruid: questionId.learningObjectIdentifier.hruid,
language: questionId.learningObjectIdentifier.language,
version: questionId.learningObjectIdentifier.version || FALLBACK_VERSION_NUM,
};
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber);
return mapToQuestionDTO(question);
}
export async function updateQuestion(questionId: QuestionId, questionData: QuestionData): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const question = await fetchQuestion(questionId);
const newQuestion = await questionRepository.updateContent(question, questionData.content);
return mapToQuestionDTO(newQuestion);
}

View file

@ -1,4 +1,4 @@
import { LearningObjectIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
export function isValidHttpUrl(url: string): boolean {
try {
@ -9,7 +9,7 @@ export function isValidHttpUrl(url: string): boolean {
}
}
export function getUrlStringForLearningObject(learningObjectId: LearningObjectIdentifier): string {
export function getUrlStringForLearningObject(learningObjectId: LearningObjectIdentifierDTO): string {
let url = `/learningObject/${learningObjectId.hruid}/html?language=${learningObjectId.language}`;
if (learningObjectId.version) {
url += `&version=${learningObjectId.version}`;
@ -17,7 +17,7 @@ export function getUrlStringForLearningObject(learningObjectId: LearningObjectId
return url;
}
export function getUrlStringForLearningObjectHTML(learningObjectIdentifier: LearningObjectIdentifier): string {
export function getUrlStringForLearningObjectHTML(learningObjectIdentifier: LearningObjectIdentifierDTO): string {
let url = `/learningObject/${learningObjectIdentifier.hruid}/html?language=${learningObjectIdentifier.language}`;
if (learningObjectIdentifier.version) {
url += `&version=${learningObjectIdentifier.version}`;

View file

@ -0,0 +1,87 @@
import { Request, Response } from 'express';
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { setupTestApp } from '../setup-tests';
import { Language } from '@dwengo-1/common/util/language';
import { getAllAnswersHandler, getAnswerHandler, updateAnswerHandler } from '../../src/controllers/answers';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
describe('Questions controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(() => {
jsonMock = vi.fn();
res = {
json: jsonMock,
};
});
it('Get answers list', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '2' },
query: { lang: Language.English, full: 'true' },
};
await getAllAnswersHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answers: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.answers);
expect(result.answers).to.have.length.greaterThan(1);
});
it('Get answer', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' },
query: { lang: Language.English, full: 'true' },
};
await getAnswerHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answer: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.answer);
});
it('Get answer hruid does not exist', async () => {
req = {
params: { hruid: 'id_not_exist' },
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('Get answer no hruid given', async () => {
req = {
params: {},
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
});
it('Update question', async () => {
const newContent = 'updated question';
req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' },
query: { lang: Language.English },
body: { content: newContent },
};
await updateAnswerHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answer: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
expect(result.answer.content).to.eq(newContent);
});
});

View file

@ -0,0 +1,117 @@
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { Request, Response } from 'express';
import { setupTestApp } from '../setup-tests';
import { getAllQuestionsHandler, getQuestionHandler, updateQuestionHandler } from '../../src/controllers/questions';
import { Language } from '@dwengo-1/common/util/language';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
describe('Questions controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(() => {
jsonMock = vi.fn();
res = {
json: jsonMock,
};
});
it('Get question list', async () => {
req = {
params: { hruid: 'id05', version: '1' },
query: { lang: Language.English, full: 'true' },
};
await getAllQuestionsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ questions: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.questions);
expect(result.questions).to.have.length.greaterThan(1);
});
it('Get question', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '1' },
query: { lang: Language.English, full: 'true' },
};
await getQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
});
it('Get question with fallback sequence number and version', async () => {
req = {
params: { hruid: 'id05' },
query: { lang: Language.English, full: 'true' },
};
await getQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
});
it('Get question hruid does not exist', async () => {
req = {
params: { hruid: 'id_not_exist' },
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('Get question no hruid given', async () => {
req = {
params: {},
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
});
/*
It('Create and delete question', async() => {
req = {
params: { hruid: 'id05', version: '1', seq: '2'},
query: { lang: Language.English },
};
await deleteQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
console.log(result.question);
});
*/
it('Update question', async () => {
const newContent = 'updated question';
req = {
params: { hruid: 'id05', version: '1', seq: '1' },
query: { lang: Language.English },
body: { content: newContent },
};
await updateQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
expect(result.question.content).to.eq(newContent);
});
});

View file

@ -186,7 +186,7 @@ describe('Student controllers', () => {
it('Get join request by student and class', async () => {
req = {
params: { username: 'PinkFloyd', classId: 'id02' },
params: { username: 'PinkFloyd', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await getStudentRequestHandler(req as Request, res as Response);
@ -201,7 +201,7 @@ describe('Student controllers', () => {
it('Create join request', async () => {
req = {
params: { username: 'Noordkaap' },
body: { classId: 'id02' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await createStudentRequestHandler(req as Request, res as Response);
@ -212,7 +212,7 @@ describe('Student controllers', () => {
it('Create join request duplicate', async () => {
req = {
params: { username: 'Tool' },
body: { classId: 'id02' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);
@ -220,7 +220,7 @@ describe('Student controllers', () => {
it('Delete join request', async () => {
req = {
params: { username: 'Noordkaap', classId: 'id02' },
params: { username: 'Noordkaap', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await deleteClassJoinRequestHandler(req as Request, res as Response);

View file

@ -104,9 +104,9 @@ describe('Teacher controllers', () => {
const result = jsonMock.mock.lastCall?.[0];
const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username);
expect(teacherUsernames).toContain('FooFighters');
expect(teacherUsernames).toContain('testleerkracht1');
expect(result.teachers).toHaveLength(4);
expect(result.teachers).toHaveLength(5);
});
it('Deleting non-existent student', async () => {
@ -117,7 +117,7 @@ describe('Teacher controllers', () => {
it('Get teacher classes', async () => {
req = {
params: { username: 'FooFighters' },
params: { username: 'testleerkracht1' },
query: { full: 'true' },
};
@ -132,7 +132,7 @@ describe('Teacher controllers', () => {
it('Get teacher students', async () => {
req = {
params: { username: 'FooFighters' },
params: { username: 'testleerkracht1' },
query: { full: 'true' },
};
@ -169,7 +169,7 @@ describe('Teacher controllers', () => {
it('Get join requests by class', async () => {
req = {
query: { username: 'LimpBizkit' },
params: { classId: 'id02' },
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await getStudentJoinRequestHandler(req as Request, res as Response);
@ -184,7 +184,7 @@ describe('Teacher controllers', () => {
it('Update join request status', async () => {
req = {
query: { username: 'LimpBizkit', studentUsername: 'PinkFloyd' },
params: { classId: 'id02' },
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
body: { accepted: 'true' },
};

View file

@ -15,7 +15,7 @@ describe('AssignmentRepository', () => {
});
it('should return the requested assignment', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignment = await assignmentRepository.findByClassAndId(class_!, 2);
expect(assignment).toBeTruthy();
@ -23,7 +23,7 @@ describe('AssignmentRepository', () => {
});
it('should return all assignments for a class', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignments = await assignmentRepository.findAllAssignmentsInClass(class_!);
expect(assignments).toBeTruthy();

View file

@ -18,7 +18,7 @@ describe('GroupRepository', () => {
});
it('should return the requested group', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment!, 1);
@ -27,7 +27,7 @@ describe('GroupRepository', () => {
});
it('should return all groups for assignment', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const groups = await groupRepository.findAllGroupsForAssignment(assignment!);
@ -37,7 +37,7 @@ describe('GroupRepository', () => {
});
it('should not find removed group', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignment = await assignmentRepository.findByClassAndId(class_!, 2);
await groupRepository.deleteByAssignmentAndGroupNumber(assignment!, 1);

View file

@ -53,7 +53,7 @@ describe('SubmissionRepository', () => {
it('should find the most recent submission for a group', async () => {
const id = new LearningObjectIdentifier('id03', Language.English, 1);
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment!, 1);
const submission = await submissionRepository.findMostRecentSubmissionForGroup(id, group!);

View file

@ -26,7 +26,7 @@ describe('ClassJoinRequestRepository', () => {
});
it('should list all requests to a single class', async () => {
const class_ = await cassRepository.findById('id02');
const class_ = await cassRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const requests = await classJoinRequestRepository.findAllOpenRequestsTo(class_!);
expect(requests).toBeTruthy();
@ -35,7 +35,7 @@ describe('ClassJoinRequestRepository', () => {
it('should not find a removed request', async () => {
const student = await studentRepository.findByUsername('SmashingPumpkins');
const class_ = await cassRepository.findById('id03');
const class_ = await cassRepository.findById('80dcc3e0-1811-4091-9361-42c0eee91cfa');
await classJoinRequestRepository.deleteBy(student!, class_!);
const request = await classJoinRequestRepository.findAllRequestsBy(student!);

View file

@ -18,16 +18,16 @@ describe('ClassRepository', () => {
});
it('should return requested class', async () => {
const classVar = await classRepository.findById('id01');
const classVar = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
expect(classVar).toBeTruthy();
expect(classVar?.displayName).toBe('class01');
});
it('class should be gone after deletion', async () => {
await classRepository.deleteById('id04');
await classRepository.deleteById('33d03536-83b8-4880-9982-9bbf2f908ddf');
const classVar = await classRepository.findById('id04');
const classVar = await classRepository.findById('33d03536-83b8-4880-9982-9bbf2f908ddf');
expect(classVar).toBeNull();
});

View file

@ -34,7 +34,7 @@ describe('ClassRepository', () => {
});
it('should return all invitations for a class', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(class_!);
expect(invitations).toBeTruthy();
@ -42,7 +42,7 @@ describe('ClassRepository', () => {
});
it('should not find a removed invitation', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const sender = await teacherRepository.findByUsername('FooFighters');
const receiver = await teacherRepository.findByUsername('LimpBizkit');
await teacherInvitationRepository.deleteBy(class_!, sender!, receiver!);

View file

@ -7,11 +7,11 @@ import learningObjectService from '../../../src/services/learning-objects/learni
import { envVars, getEnvVar } from '../../../src/util/envVars';
import { LearningPath } from '../../../src/entities/content/learning-path.entity';
import learningPathExample from '../../test-assets/learning-paths/pn-werking-example';
import { LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
const EXPECTED_DWENGO_LEARNING_OBJECT_TITLE = 'Werken met notebooks';
const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifier = {
const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifierDTO = {
hruid: 'pn_werkingnotebooks',
language: Language.Dutch,
version: 3,

View file

@ -4,11 +4,11 @@ import { Student } from '../../../src/entities/users/student.entity';
import { Teacher } from '../../../src/entities/users/teacher.entity';
export function makeTestClasses(em: EntityManager, students: Student[], teachers: Teacher[]): Class[] {
const studentsClass01 = students.slice(0, 7);
const teacherClass01: Teacher[] = teachers.slice(0, 1);
const studentsClass01 = students.slice(0, 8);
const teacherClass01: Teacher[] = teachers.slice(4, 5);
const class01 = em.create(Class, {
classId: 'id01',
classId: '8764b861-90a6-42e5-9732-c0d9eb2f55f9',
displayName: 'class01',
teachers: teacherClass01,
students: studentsClass01,
@ -18,7 +18,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass02: Teacher[] = teachers.slice(1, 2);
const class02 = em.create(Class, {
classId: 'id02',
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
displayName: 'class02',
teachers: teacherClass02,
students: studentsClass02,
@ -28,7 +28,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass03: Teacher[] = teachers.slice(2, 3);
const class03 = em.create(Class, {
classId: 'id03',
classId: '80dcc3e0-1811-4091-9361-42c0eee91cfa',
displayName: 'class03',
teachers: teacherClass03,
students: studentsClass03,
@ -38,7 +38,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass04: Teacher[] = teachers.slice(2, 3);
const class04 = em.create(Class, {
classId: 'id04',
classId: '33d03536-83b8-4880-9982-9bbf2f908ddf',
displayName: 'class04',
teachers: teacherClass04,
students: studentsClass04,

View file

@ -11,6 +11,8 @@ export const TEST_STUDENTS = [
{ username: 'TheDoors', firstName: 'Jim', lastName: 'Morisson' },
// ⚠️ Deze mag niet gebruikt worden in elke test!
{ username: 'Nirvana', firstName: 'Kurt', lastName: 'Cobain' },
// Makes sure when logged in as leerling1, there exists a corresponding user
{ username: 'testleerling1', firstName: 'Gerald', lastName: 'Schmittinger' },
];
// 🏗️ Functie die ORM entities maakt uit de data array

View file

@ -27,5 +27,12 @@ export function makeTestTeachers(em: EntityManager): Teacher[] {
lastName: 'Cappelle',
});
return [teacher01, teacher02, teacher03, teacher04];
// Makes sure when logged in as testleerkracht1, there exists a corresponding user
const teacher05 = em.create(Teacher, {
username: 'testleerkracht1',
firstName: 'Bob',
lastName: 'Dylan',
});
return [teacher01, teacher02, teacher03, teacher04, teacher05];
}

72
backend/tool/seed.ts Normal file
View file

@ -0,0 +1,72 @@
import { forkEntityManager, initORM } from '../src/orm.js';
import dotenv from 'dotenv';
import { makeTestAssignemnts } from '../tests/test_assets/assignments/assignments.testdata.js';
import { makeTestGroups } from '../tests/test_assets/assignments/groups.testdata.js';
import { makeTestSubmissions } from '../tests/test_assets/assignments/submission.testdata.js';
import { makeTestClassJoinRequests } from '../tests/test_assets/classes/class-join-requests.testdata.js';
import { makeTestClasses } from '../tests/test_assets/classes/classes.testdata.js';
import { makeTestTeacherInvitations } from '../tests/test_assets/classes/teacher-invitations.testdata.js';
import { makeTestAttachments } from '../tests/test_assets/content/attachments.testdata.js';
import { makeTestLearningObjects } from '../tests/test_assets/content/learning-objects.testdata.js';
import { makeTestLearningPaths } from '../tests/test_assets/content/learning-paths.testdata.js';
import { makeTestAnswers } from '../tests/test_assets/questions/answers.testdata.js';
import { makeTestQuestions } from '../tests/test_assets/questions/questions.testdata.js';
import { makeTestStudents } from '../tests/test_assets/users/students.testdata.js';
import { makeTestTeachers } from '../tests/test_assets/users/teachers.testdata.js';
import { getLogger, Logger } from '../src/logging/initalize.js';
const logger: Logger = getLogger();
export async function seedDatabase(): Promise<void> {
dotenv.config({ path: '.env.development.local' });
const orm = await initORM();
await orm.schema.clearDatabase();
const em = forkEntityManager();
logger.info('seeding database...');
const students = makeTestStudents(em);
const teachers = makeTestTeachers(em);
const learningObjects = makeTestLearningObjects(em);
const learningPaths = makeTestLearningPaths(em);
const classes = makeTestClasses(em, students, teachers);
const assignments = makeTestAssignemnts(em, classes);
const groups = makeTestGroups(em, students, assignments);
assignments[0].groups = groups.slice(0, 3);
assignments[1].groups = groups.slice(3, 4);
const teacherInvitations = makeTestTeacherInvitations(em, teachers, classes);
const classJoinRequests = makeTestClassJoinRequests(em, students, classes);
const attachments = makeTestAttachments(em, learningObjects);
learningObjects[1].attachments = attachments;
const questions = makeTestQuestions(em, students);
const answers = makeTestAnswers(em, teachers, questions);
const submissions = makeTestSubmissions(em, students, groups);
// Persist all entities
await em.persistAndFlush([
...students,
...teachers,
...learningObjects,
...learningPaths,
...classes,
...assignments,
...groups,
...teacherInvitations,
...classJoinRequests,
...attachments,
...questions,
...answers,
...submissions,
]);
logger.info('Development database seeded successfully!');
await orm.close();
}
seedDatabase().catch(logger.error);