Merge remote-tracking branch 'origin/dev' into feat/indieningen-kunnen-posten-en-bekijken-#194
# Conflicts: # backend/tests/setup-tests.ts
This commit is contained in:
commit
dd2cdf3fe9
46 changed files with 1670 additions and 123 deletions
|
@ -6,7 +6,7 @@ import { Language } from '@dwengo-1/common/util/language';
|
|||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import {Group} from "../entities/assignments/group.entity";
|
||||
import {getGroupRepository} from "../data/repositories";
|
||||
import {getAssignmentRepository, getGroupRepository} from "../data/repositories";
|
||||
|
||||
/**
|
||||
* Fetch learning paths based on query parameters.
|
||||
|
@ -27,15 +27,14 @@ export async function getLearningPaths(req: Request, res: Response): Promise<voi
|
|||
if (!assignmentNo || !classId) {
|
||||
throw new BadRequestException('If forGroupNo is specified, assignmentNo and classId must also be specified.');
|
||||
}
|
||||
forGroup = await getGroupRepository().findOne({
|
||||
assignment: {
|
||||
id: parseInt(assignmentNo),
|
||||
within: {
|
||||
classId
|
||||
}
|
||||
},
|
||||
groupNumber: parseInt(forGroupNo)
|
||||
}) ?? undefined;
|
||||
const assignment = await getAssignmentRepository().findByClassIdAndAssignmentId(
|
||||
classId, parseInt(assignmentNo)
|
||||
);
|
||||
if (assignment) {
|
||||
forGroup = await getGroupRepository().findByAssignmentAndGroupNumber(
|
||||
assignment, parseInt(forGroupNo)
|
||||
) ?? undefined;
|
||||
}
|
||||
}
|
||||
|
||||
let hruidList;
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
deleteSubmission,
|
||||
getAllSubmissions,
|
||||
getSubmission,
|
||||
getSubmissionsForLearningObjectAndAssignment,
|
||||
getSubmissionsForLearningObjectAndAssignment, getSubmissionsForLearningObjectAndGroup,
|
||||
} from '../services/submissions.js';
|
||||
import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission';
|
||||
import { Language, languageMap } from '@dwengo-1/common/util/language';
|
||||
|
@ -17,13 +17,25 @@ export async function getSubmissionsHandler(req: Request, res: Response): Promis
|
|||
const lang = languageMap[req.query.language as string] || Language.Dutch;
|
||||
const version = parseInt(req.query.version as string) ?? 1;
|
||||
|
||||
const submissions = await getSubmissionsForLearningObjectAndAssignment(
|
||||
loHruid,
|
||||
lang,
|
||||
version,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string)
|
||||
);
|
||||
let submissions: SubmissionDTO[]
|
||||
if (req.query.groupId) {
|
||||
submissions = await getSubmissionsForLearningObjectAndGroup(
|
||||
loHruid,
|
||||
lang,
|
||||
version,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string),
|
||||
parseInt(req.query.groupId as string)
|
||||
);
|
||||
} else {
|
||||
submissions = await getSubmissionsForLearningObjectAndAssignment(
|
||||
loHruid,
|
||||
lang,
|
||||
version,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string)
|
||||
)
|
||||
}
|
||||
|
||||
res.json(submissions);
|
||||
}
|
||||
|
|
66
backend/src/controllers/teacher-invitations.ts
Normal file
66
backend/src/controllers/teacher-invitations.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { requireFields } from './error-helper';
|
||||
import { createInvitation, deleteInvitation, getAllInvitations, getInvitation, updateInvitation } from '../services/teacher-invitations';
|
||||
import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
|
||||
export async function getAllInvitationsHandler(req: Request, res: Response): Promise<void> {
|
||||
const username = req.params.username;
|
||||
const by = req.query.sent === 'true';
|
||||
requireFields({ username });
|
||||
|
||||
const invitations = await getAllInvitations(username, by);
|
||||
|
||||
res.json({ invitations });
|
||||
}
|
||||
|
||||
export async function getInvitationHandler(req: Request, res: Response): Promise<void> {
|
||||
const sender = req.params.sender;
|
||||
const receiver = req.params.receiver;
|
||||
const classId = req.params.classId;
|
||||
requireFields({ sender, receiver, classId });
|
||||
|
||||
const invitation = await getInvitation(sender, receiver, classId);
|
||||
|
||||
res.json({ invitation });
|
||||
}
|
||||
|
||||
export async function createInvitationHandler(req: Request, res: Response): Promise<void> {
|
||||
const sender = req.body.sender;
|
||||
const receiver = req.body.receiver;
|
||||
const classId = req.body.class;
|
||||
requireFields({ sender, receiver, classId });
|
||||
|
||||
const data = req.body as TeacherInvitationData;
|
||||
const invitation = await createInvitation(data);
|
||||
|
||||
res.json({ invitation });
|
||||
}
|
||||
|
||||
export async function updateInvitationHandler(req: Request, res: Response): Promise<void> {
|
||||
const sender = req.body.sender;
|
||||
const receiver = req.body.receiver;
|
||||
const classId = req.body.class;
|
||||
req.body.accepted = req.body.accepted !== 'false';
|
||||
requireFields({ sender, receiver, classId });
|
||||
|
||||
const data = req.body as TeacherInvitationData;
|
||||
const invitation = await updateInvitation(data);
|
||||
|
||||
res.json({ invitation });
|
||||
}
|
||||
|
||||
export async function deleteInvitationHandler(req: Request, res: Response): Promise<void> {
|
||||
const sender = req.params.sender;
|
||||
const receiver = req.params.receiver;
|
||||
const classId = req.params.classId;
|
||||
requireFields({ sender, receiver, classId });
|
||||
|
||||
const data: TeacherInvitationData = {
|
||||
sender,
|
||||
receiver,
|
||||
class: classId,
|
||||
};
|
||||
const invitation = await deleteInvitation(data);
|
||||
|
||||
res.json({ invitation });
|
||||
}
|
|
@ -2,14 +2,14 @@ import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
|
|||
import { Class } from '../../entities/classes/class.entity.js';
|
||||
import { ClassJoinRequest } from '../../entities/classes/class-join-request.entity.js';
|
||||
import { Student } from '../../entities/users/student.entity.js';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export class ClassJoinRequestRepository extends DwengoEntityRepository<ClassJoinRequest> {
|
||||
public async findAllRequestsBy(requester: Student): Promise<ClassJoinRequest[]> {
|
||||
return this.findAll({ where: { requester: requester } });
|
||||
}
|
||||
public async findAllOpenRequestsTo(clazz: Class): Promise<ClassJoinRequest[]> {
|
||||
return this.findAll({ where: { class: clazz, status: ClassJoinRequestStatus.Open } }); // TODO check if works like this
|
||||
return this.findAll({ where: { class: clazz, status: ClassStatus.Open } }); // TODO check if works like this
|
||||
}
|
||||
public async findByStudentAndClass(requester: Student, clazz: Class): Promise<ClassJoinRequest | null> {
|
||||
return this.findOne({ requester, class: clazz });
|
||||
|
|
|
@ -2,6 +2,7 @@ import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
|
|||
import { Class } from '../../entities/classes/class.entity.js';
|
||||
import { TeacherInvitation } from '../../entities/classes/teacher-invitation.entity.js';
|
||||
import { Teacher } from '../../entities/users/teacher.entity.js';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export class TeacherInvitationRepository extends DwengoEntityRepository<TeacherInvitation> {
|
||||
public async findAllInvitationsForClass(clazz: Class): Promise<TeacherInvitation[]> {
|
||||
|
@ -11,7 +12,7 @@ export class TeacherInvitationRepository extends DwengoEntityRepository<TeacherI
|
|||
return this.findAll({ where: { sender: sender } });
|
||||
}
|
||||
public async findAllInvitationsFor(receiver: Teacher): Promise<TeacherInvitation[]> {
|
||||
return this.findAll({ where: { receiver: receiver } });
|
||||
return this.findAll({ where: { receiver: receiver, status: ClassStatus.Open } });
|
||||
}
|
||||
public async deleteBy(clazz: Class, sender: Teacher, receiver: Teacher): Promise<void> {
|
||||
return this.deleteWhere({
|
||||
|
@ -20,4 +21,11 @@ export class TeacherInvitationRepository extends DwengoEntityRepository<TeacherI
|
|||
class: clazz,
|
||||
});
|
||||
}
|
||||
public async findBy(clazz: Class, sender: Teacher, receiver: Teacher): Promise<TeacherInvitation | null> {
|
||||
return this.findOne({
|
||||
sender: sender,
|
||||
receiver: receiver,
|
||||
class: clazz,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ import { Language } from '@dwengo-1/common/util/language';
|
|||
|
||||
@Entity({ repository: () => SubmissionRepository })
|
||||
export class Submission {
|
||||
@PrimaryKey({ type: 'integer', autoincrement: true })
|
||||
submissionNumber?: number;
|
||||
|
||||
@PrimaryKey({ type: 'string' })
|
||||
learningObjectHruid!: string;
|
||||
|
||||
|
@ -18,9 +21,6 @@ export class Submission {
|
|||
@PrimaryKey({ type: 'numeric' })
|
||||
learningObjectVersion = 1;
|
||||
|
||||
@PrimaryKey({ type: 'integer', autoincrement: true })
|
||||
submissionNumber?: number;
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Group,
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@ import { Entity, Enum, ManyToOne } from '@mikro-orm/core';
|
|||
import { Student } from '../users/student.entity.js';
|
||||
import { Class } from './class.entity.js';
|
||||
import { ClassJoinRequestRepository } from '../../data/classes/class-join-request-repository.js';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
@Entity({
|
||||
repository: () => ClassJoinRequestRepository,
|
||||
|
@ -20,6 +20,6 @@ export class ClassJoinRequest {
|
|||
})
|
||||
class!: Class;
|
||||
|
||||
@Enum(() => ClassJoinRequestStatus)
|
||||
status!: ClassJoinRequestStatus;
|
||||
@Enum(() => ClassStatus)
|
||||
status!: ClassStatus;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import { Entity, ManyToOne } from '@mikro-orm/core';
|
||||
import { Entity, Enum, ManyToOne } from '@mikro-orm/core';
|
||||
import { Teacher } from '../users/teacher.entity.js';
|
||||
import { Class } from './class.entity.js';
|
||||
import { TeacherInvitationRepository } from '../../data/classes/teacher-invitation-repository.js';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
/**
|
||||
* Invitation of a teacher into a class (in order to teach it).
|
||||
|
@ -25,4 +26,7 @@ export class TeacherInvitation {
|
|||
primary: true,
|
||||
})
|
||||
class!: Class;
|
||||
|
||||
@Enum(() => ClassStatus)
|
||||
status!: ClassStatus;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import { getClassJoinRequestRepository } from '../data/repositories.js';
|
|||
import { Student } from '../entities/users/student.entity.js';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export function mapToStudentRequestDTO(request: ClassJoinRequest): ClassJoinRequestDTO {
|
||||
return {
|
||||
|
@ -18,6 +18,6 @@ export function mapToStudentRequest(student: Student, cls: Class): ClassJoinRequ
|
|||
return getClassJoinRequestRepository().create({
|
||||
requester: student,
|
||||
class: cls,
|
||||
status: ClassJoinRequestStatus.Open,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
import { TeacherInvitation } from '../entities/classes/teacher-invitation.entity.js';
|
||||
import { mapToClassDTO } from './class.js';
|
||||
import { mapToUserDTO } from './user.js';
|
||||
import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
import { getTeacherInvitationRepository } from '../data/repositories';
|
||||
import { Teacher } from '../entities/users/teacher.entity';
|
||||
import { Class } from '../entities/classes/class.entity';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export function mapToTeacherInvitationDTO(invitation: TeacherInvitation): TeacherInvitationDTO {
|
||||
return {
|
||||
sender: mapToUserDTO(invitation.sender),
|
||||
receiver: mapToUserDTO(invitation.receiver),
|
||||
class: mapToClassDTO(invitation.class),
|
||||
classId: invitation.class.classId!,
|
||||
status: invitation.status,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -15,6 +19,16 @@ export function mapToTeacherInvitationDTOIds(invitation: TeacherInvitation): Tea
|
|||
return {
|
||||
sender: invitation.sender.username,
|
||||
receiver: invitation.receiver.username,
|
||||
class: invitation.class.classId!,
|
||||
classId: invitation.class.classId!,
|
||||
status: invitation.status,
|
||||
};
|
||||
}
|
||||
|
||||
export function mapToInvitation(sender: Teacher, receiver: Teacher, cls: Class): TeacherInvitation {
|
||||
return getTeacherInvitationRepository().create({
|
||||
sender,
|
||||
receiver,
|
||||
class: cls,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ const router = express.Router({ mergeParams: true });
|
|||
// Root endpoint used to search objects
|
||||
router.get('/', getSubmissionsHandler);
|
||||
|
||||
router.post('/:id', createSubmissionHandler);
|
||||
router.post('/', createSubmissionHandler);
|
||||
|
||||
// Information about an submission with id 'id'
|
||||
router.get('/:id', getSubmissionHandler);
|
||||
|
|
22
backend/src/routes/teacher-invitations.ts
Normal file
22
backend/src/routes/teacher-invitations.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import express from 'express';
|
||||
import {
|
||||
createInvitationHandler,
|
||||
deleteInvitationHandler,
|
||||
getAllInvitationsHandler,
|
||||
getInvitationHandler,
|
||||
updateInvitationHandler,
|
||||
} from '../controllers/teacher-invitations';
|
||||
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
router.get('/:username', getAllInvitationsHandler);
|
||||
|
||||
router.get('/:sender/:receiver/:classId', getInvitationHandler);
|
||||
|
||||
router.post('/', createInvitationHandler);
|
||||
|
||||
router.put('/', updateInvitationHandler);
|
||||
|
||||
router.delete('/:sender/:receiver/:classId', deleteInvitationHandler);
|
||||
|
||||
export default router;
|
|
@ -10,6 +10,8 @@ import {
|
|||
getTeacherStudentHandler,
|
||||
updateStudentJoinRequestHandler,
|
||||
} from '../controllers/teachers.js';
|
||||
import invitationRouter from './teacher-invitations.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Root endpoint used to search objects
|
||||
|
@ -32,10 +34,6 @@ router.get('/:username/joinRequests/:classId', getStudentJoinRequestHandler);
|
|||
router.put('/:username/joinRequests/:classId/:studentUsername', updateStudentJoinRequestHandler);
|
||||
|
||||
// Invitations to other classes a teacher received
|
||||
router.get('/:id/invitations', (_req, res) => {
|
||||
res.json({
|
||||
invitations: ['0'],
|
||||
});
|
||||
});
|
||||
router.get('/invitations', invitationRouter);
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -111,6 +111,7 @@ async function convertNode(
|
|||
updatedAt: node.updatedAt.toISOString(),
|
||||
learningobject_hruid: node.learningObjectHruid,
|
||||
version: learningObject.version,
|
||||
done: personalizedFor ? lastSubmission !== null : undefined,
|
||||
transitions,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { getAssignmentRepository, getSubmissionRepository } from '../data/repositories.js';
|
||||
import {getAssignmentRepository, getGroupRepository, getSubmissionRepository} from '../data/repositories.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { mapToSubmission, mapToSubmissionDTO } from '../interfaces/submission.js';
|
||||
|
@ -69,3 +69,24 @@ export async function getSubmissionsForLearningObjectAndAssignment(
|
|||
|
||||
return submissions.map((s) => mapToSubmissionDTO(s));
|
||||
}
|
||||
|
||||
export async function getSubmissionsForLearningObjectAndGroup(
|
||||
learningObjectHruid: string,
|
||||
language: Language,
|
||||
version: number,
|
||||
classId: string,
|
||||
assignmentNo: number,
|
||||
groupNumber: number
|
||||
): Promise<SubmissionDTO[]> {
|
||||
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
|
||||
const assignment = await getAssignmentRepository().findByClassIdAndAssignmentId(classId, assignmentNo);
|
||||
if (!assignment) {
|
||||
throw new NotFoundException("Assignment not found!");
|
||||
}
|
||||
const group = await getGroupRepository().findByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
if (!group) {
|
||||
throw new NotFoundException("Group not found!");
|
||||
}
|
||||
const submissions = await getSubmissionRepository().findAllSubmissionsForGroup(group);
|
||||
return submissions.map((s) => mapToSubmissionDTO(s));
|
||||
}
|
||||
|
|
87
backend/src/services/teacher-invitations.ts
Normal file
87
backend/src/services/teacher-invitations.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
import { fetchTeacher } from './teachers';
|
||||
import { getTeacherInvitationRepository } from '../data/repositories';
|
||||
import { mapToInvitation, mapToTeacherInvitationDTO } from '../interfaces/teacher-invitation';
|
||||
import { addClassTeacher, fetchClass } from './classes';
|
||||
import { TeacherInvitationData, TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
import { ConflictException } from '../exceptions/conflict-exception';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception';
|
||||
import { TeacherInvitation } from '../entities/classes/teacher-invitation.entity';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export async function getAllInvitations(username: string, sent: boolean): Promise<TeacherInvitationDTO[]> {
|
||||
const teacher = await fetchTeacher(username);
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
|
||||
let invitations;
|
||||
if (sent) {
|
||||
invitations = await teacherInvitationRepository.findAllInvitationsBy(teacher);
|
||||
} else {
|
||||
invitations = await teacherInvitationRepository.findAllInvitationsFor(teacher);
|
||||
}
|
||||
return invitations.map(mapToTeacherInvitationDTO);
|
||||
}
|
||||
|
||||
export async function createInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const sender = await fetchTeacher(data.sender);
|
||||
const receiver = await fetchTeacher(data.receiver);
|
||||
|
||||
const cls = await fetchClass(data.class);
|
||||
|
||||
if (!cls.teachers.contains(sender)) {
|
||||
throw new ConflictException('The teacher sending the invite is not part of the class');
|
||||
}
|
||||
|
||||
const newInvitation = mapToInvitation(sender, receiver, cls);
|
||||
await teacherInvitationRepository.save(newInvitation, { preventOverwrite: true });
|
||||
|
||||
return mapToTeacherInvitationDTO(newInvitation);
|
||||
}
|
||||
|
||||
async function fetchInvitation(usernameSender: string, usernameReceiver: string, classId: string): Promise<TeacherInvitation> {
|
||||
const sender = await fetchTeacher(usernameSender);
|
||||
const receiver = await fetchTeacher(usernameReceiver);
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const invite = await teacherInvitationRepository.findBy(cls, sender, receiver);
|
||||
|
||||
if (!invite) {
|
||||
throw new NotFoundException('Teacher invite not found');
|
||||
}
|
||||
|
||||
return invite;
|
||||
}
|
||||
|
||||
export async function getInvitation(sender: string, receiver: string, classId: string): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(sender, receiver, classId);
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
||||
|
||||
export async function updateInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(data.sender, data.receiver, data.class);
|
||||
invitation.status = ClassStatus.Declined;
|
||||
|
||||
if (data.accepted) {
|
||||
invitation.status = ClassStatus.Accepted;
|
||||
await addClassTeacher(data.class, data.receiver);
|
||||
}
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
await teacherInvitationRepository.save(invitation);
|
||||
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
||||
|
||||
export async function deleteInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(data.sender, data.receiver, data.class);
|
||||
|
||||
const sender = await fetchTeacher(data.sender);
|
||||
const receiver = await fetchTeacher(data.receiver);
|
||||
const cls = await fetchClass(data.class);
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
await teacherInvitationRepository.deleteBy(cls, sender, receiver);
|
||||
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
|
@ -28,7 +28,7 @@ import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
|||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ConflictException } from '../exceptions/conflict-exception.js';
|
||||
|
||||
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
|
||||
|
@ -160,10 +160,10 @@ export async function updateClassJoinRequestStatus(studentUsername: string, clas
|
|||
throw new NotFoundException('Join request not found');
|
||||
}
|
||||
|
||||
request.status = ClassJoinRequestStatus.Declined;
|
||||
request.status = ClassStatus.Declined;
|
||||
|
||||
if (accepted) {
|
||||
request.status = ClassJoinRequestStatus.Accepted;
|
||||
request.status = ClassStatus.Accepted;
|
||||
await addClassStudent(classId, studentUsername);
|
||||
}
|
||||
|
||||
|
|
123
backend/tests/controllers/teacher-invitations.test.ts
Normal file
123
backend/tests/controllers/teacher-invitations.test.ts
Normal file
|
@ -0,0 +1,123 @@
|
|||
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
|
||||
import { Request, Response } from 'express';
|
||||
import { setupTestApp } from '../setup-tests.js';
|
||||
import {
|
||||
createInvitationHandler,
|
||||
deleteInvitationHandler,
|
||||
getAllInvitationsHandler,
|
||||
getInvitationHandler,
|
||||
updateInvitationHandler,
|
||||
} from '../../src/controllers/teacher-invitations';
|
||||
import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
import { getClassHandler } from '../../src/controllers/classes';
|
||||
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
describe('Teacher 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 teacher invitations by', async () => {
|
||||
req = { params: { username: 'LimpBizkit' }, query: { sent: 'true' } };
|
||||
|
||||
await getAllInvitationsHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() }));
|
||||
|
||||
const result = jsonMock.mock.lastCall?.[0];
|
||||
// Console.log(result.invitations);
|
||||
expect(result.invitations).to.have.length.greaterThan(0);
|
||||
});
|
||||
|
||||
it('Get teacher invitations for', async () => {
|
||||
req = { params: { username: 'FooFighters' }, query: { by: 'false' } };
|
||||
|
||||
await getAllInvitationsHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() }));
|
||||
|
||||
const result = jsonMock.mock.lastCall?.[0];
|
||||
expect(result.invitations).to.have.length.greaterThan(0);
|
||||
});
|
||||
|
||||
it('Create and delete invitation', async () => {
|
||||
const body = {
|
||||
sender: 'LimpBizkit',
|
||||
receiver: 'testleerkracht1',
|
||||
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
} as TeacherInvitationData;
|
||||
req = { body };
|
||||
|
||||
await createInvitationHandler(req as Request, res as Response);
|
||||
|
||||
req = {
|
||||
params: {
|
||||
sender: 'LimpBizkit',
|
||||
receiver: 'testleerkracht1',
|
||||
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
},
|
||||
body: { accepted: 'false' },
|
||||
};
|
||||
|
||||
await deleteInvitationHandler(req as Request, res as Response);
|
||||
});
|
||||
|
||||
it('Get invitation', async () => {
|
||||
req = {
|
||||
params: {
|
||||
sender: 'LimpBizkit',
|
||||
receiver: 'FooFighters',
|
||||
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
},
|
||||
};
|
||||
await getInvitationHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitation: expect.anything() }));
|
||||
});
|
||||
|
||||
it('Get invitation error', async () => {
|
||||
req = {
|
||||
params: { no: 'no params' },
|
||||
};
|
||||
|
||||
await expect(async () => getInvitationHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException);
|
||||
});
|
||||
|
||||
it('Accept invitation', async () => {
|
||||
const body = {
|
||||
sender: 'LimpBizkit',
|
||||
receiver: 'FooFighters',
|
||||
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
} as TeacherInvitationData;
|
||||
req = { body };
|
||||
|
||||
await updateInvitationHandler(req as Request, res as Response);
|
||||
|
||||
const result1 = jsonMock.mock.lastCall?.[0];
|
||||
expect(result1.invitation.status).toEqual(ClassStatus.Accepted);
|
||||
|
||||
req = {
|
||||
params: {
|
||||
id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
},
|
||||
};
|
||||
|
||||
await getClassHandler(req as Request, res as Response);
|
||||
|
||||
const result = jsonMock.mock.lastCall?.[0];
|
||||
expect(result.class.teachers).toContain('FooFighters');
|
||||
});
|
||||
});
|
|
@ -2,9 +2,11 @@ import { EntityManager } from '@mikro-orm/core';
|
|||
import { Assignment } from '../../../src/entities/assignments/assignment.entity';
|
||||
import { Class } from '../../../src/entities/classes/class.entity';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import {testLearningPathWithConditions} from "../content/learning-paths.testdata";
|
||||
import {getClassWithTestleerlingAndTestleerkracht} from "../classes/classes.testdata";
|
||||
|
||||
export function makeTestAssignemnts(em: EntityManager, classes: Class[]): Assignment[] {
|
||||
const assignment01 = em.create(Assignment, {
|
||||
assignment01 = em.create(Assignment, {
|
||||
within: classes[0],
|
||||
id: 1,
|
||||
title: 'dire straits',
|
||||
|
@ -14,7 +16,7 @@ export function makeTestAssignemnts(em: EntityManager, classes: Class[]): Assign
|
|||
groups: [],
|
||||
});
|
||||
|
||||
const assignment02 = em.create(Assignment, {
|
||||
assignment02 = em.create(Assignment, {
|
||||
within: classes[1],
|
||||
id: 2,
|
||||
title: 'tool',
|
||||
|
@ -24,7 +26,7 @@ export function makeTestAssignemnts(em: EntityManager, classes: Class[]): Assign
|
|||
groups: [],
|
||||
});
|
||||
|
||||
const assignment03 = em.create(Assignment, {
|
||||
assignment03 = em.create(Assignment, {
|
||||
within: classes[0],
|
||||
id: 3,
|
||||
title: 'delete',
|
||||
|
@ -34,7 +36,7 @@ export function makeTestAssignemnts(em: EntityManager, classes: Class[]): Assign
|
|||
groups: [],
|
||||
});
|
||||
|
||||
const assignment04 = em.create(Assignment, {
|
||||
assignment04 = em.create(Assignment, {
|
||||
within: classes[0],
|
||||
id: 4,
|
||||
title: 'another assignment',
|
||||
|
@ -44,5 +46,41 @@ export function makeTestAssignemnts(em: EntityManager, classes: Class[]): Assign
|
|||
groups: [],
|
||||
});
|
||||
|
||||
return [assignment01, assignment02, assignment03, assignment04];
|
||||
conditionalPathAssignment = em.create(Assignment, {
|
||||
within: getClassWithTestleerlingAndTestleerkracht(),
|
||||
id: 1,
|
||||
title: 'Assignment: Conditional Learning Path',
|
||||
description: 'You have to do the testing learning path with a condition.',
|
||||
learningPathHruid: testLearningPathWithConditions.hruid,
|
||||
learningPathLanguage: testLearningPathWithConditions.language as Language,
|
||||
groups: [],
|
||||
});
|
||||
|
||||
return [assignment01, assignment02, assignment03, assignment04, conditionalPathAssignment];
|
||||
}
|
||||
|
||||
let assignment01: Assignment;
|
||||
let assignment02: Assignment;
|
||||
let assignment03: Assignment;
|
||||
let assignment04: Assignment;
|
||||
let conditionalPathAssignment: Assignment;
|
||||
|
||||
export function getAssignment01(): Assignment {
|
||||
return assignment01;
|
||||
}
|
||||
|
||||
export function getAssignment02(): Assignment {
|
||||
return assignment02;
|
||||
}
|
||||
|
||||
export function getAssignment03(): Assignment {
|
||||
return assignment03;
|
||||
}
|
||||
|
||||
export function getAssignment04(): Assignment {
|
||||
return assignment04;
|
||||
}
|
||||
|
||||
export function getConditionalPathAssignment(): Assignment {
|
||||
return conditionalPathAssignment;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ import {EntityManager} from '@mikro-orm/core';
|
|||
import { Group } from '../../../src/entities/assignments/group.entity';
|
||||
import { Assignment } from '../../../src/entities/assignments/assignment.entity';
|
||||
import { Student } from '../../../src/entities/users/student.entity';
|
||||
import {getConditionalPathAssignment} from "./assignments.testdata";
|
||||
import {getTestleerling1} from "../users/students.testdata";
|
||||
|
||||
export function makeTestGroups(em: EntityManager, students: Student[], assignments: Assignment[]): Group[] {
|
||||
/*
|
||||
|
@ -54,7 +56,16 @@ export function makeTestGroups(em: EntityManager, students: Student[], assignmen
|
|||
members: students.slice(0, 2),
|
||||
});
|
||||
|
||||
return [group01, group02, group03, group04, group05];
|
||||
/**
|
||||
* Group 1 for the assignment of the testing learning path with conditions.
|
||||
*/
|
||||
group1ConditionalLearningPath = em.create(Group, {
|
||||
assignment: getConditionalPathAssignment(),
|
||||
groupNumber: 1,
|
||||
members: [getTestleerling1()]
|
||||
})
|
||||
|
||||
return [group01, group02, group03, group04, group05, group1ConditionalLearningPath];
|
||||
}
|
||||
|
||||
let group01: Group;
|
||||
|
@ -62,6 +73,7 @@ let group02: Group;
|
|||
let group03: Group;
|
||||
let group04: Group;
|
||||
let group05: Group;
|
||||
let group1ConditionalLearningPath: Group;
|
||||
|
||||
export function getTestGroup01() {
|
||||
return group01;
|
||||
|
@ -83,3 +95,6 @@ export function getTestGroup05() {
|
|||
return group05;
|
||||
}
|
||||
|
||||
export function getGroup1ConditionalLearningPath() {
|
||||
return group1ConditionalLearningPath;
|
||||
}
|
||||
|
|
|
@ -2,31 +2,31 @@ import { EntityManager } from '@mikro-orm/core';
|
|||
import { ClassJoinRequest } from '../../../src/entities/classes/class-join-request.entity';
|
||||
import { Student } from '../../../src/entities/users/student.entity';
|
||||
import { Class } from '../../../src/entities/classes/class.entity';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export function makeTestClassJoinRequests(em: EntityManager, students: Student[], classes: Class[]): ClassJoinRequest[] {
|
||||
const classJoinRequest01 = em.create(ClassJoinRequest, {
|
||||
requester: students[4],
|
||||
class: classes[1],
|
||||
status: ClassJoinRequestStatus.Open,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const classJoinRequest02 = em.create(ClassJoinRequest, {
|
||||
requester: students[2],
|
||||
class: classes[1],
|
||||
status: ClassJoinRequestStatus.Open,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const classJoinRequest03 = em.create(ClassJoinRequest, {
|
||||
requester: students[4],
|
||||
class: classes[2],
|
||||
status: ClassJoinRequestStatus.Open,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const classJoinRequest04 = em.create(ClassJoinRequest, {
|
||||
requester: students[3],
|
||||
class: classes[2],
|
||||
status: ClassJoinRequestStatus.Open,
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
return [classJoinRequest01, classJoinRequest02, classJoinRequest03, classJoinRequest04];
|
||||
|
|
|
@ -2,12 +2,14 @@ import { EntityManager } from '@mikro-orm/core';
|
|||
import { Class } from '../../../src/entities/classes/class.entity';
|
||||
import { Student } from '../../../src/entities/users/student.entity';
|
||||
import { Teacher } from '../../../src/entities/users/teacher.entity';
|
||||
import {getTestleerkracht1} from "../users/teachers.testdata";
|
||||
import {getTestleerling1} from "../users/students.testdata";
|
||||
|
||||
export function makeTestClasses(em: EntityManager, students: Student[], teachers: Teacher[]): Class[] {
|
||||
const studentsClass01 = students.slice(0, 8);
|
||||
const teacherClass01: Teacher[] = teachers.slice(4, 5);
|
||||
|
||||
const class01 = em.create(Class, {
|
||||
class01 = em.create(Class, {
|
||||
classId: '8764b861-90a6-42e5-9732-c0d9eb2f55f9',
|
||||
displayName: 'class01',
|
||||
teachers: teacherClass01,
|
||||
|
@ -17,7 +19,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
|
|||
const studentsClass02: Student[] = students.slice(0, 2).concat(students.slice(3, 4));
|
||||
const teacherClass02: Teacher[] = teachers.slice(1, 2);
|
||||
|
||||
const class02 = em.create(Class, {
|
||||
class02 = em.create(Class, {
|
||||
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
|
||||
displayName: 'class02',
|
||||
teachers: teacherClass02,
|
||||
|
@ -27,7 +29,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
|
|||
const studentsClass03: Student[] = students.slice(1, 4);
|
||||
const teacherClass03: Teacher[] = teachers.slice(2, 3);
|
||||
|
||||
const class03 = em.create(Class, {
|
||||
class03 = em.create(Class, {
|
||||
classId: '80dcc3e0-1811-4091-9361-42c0eee91cfa',
|
||||
displayName: 'class03',
|
||||
teachers: teacherClass03,
|
||||
|
@ -37,12 +39,45 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
|
|||
const studentsClass04: Student[] = students.slice(0, 2);
|
||||
const teacherClass04: Teacher[] = teachers.slice(2, 3);
|
||||
|
||||
const class04 = em.create(Class, {
|
||||
class04 = em.create(Class, {
|
||||
classId: '33d03536-83b8-4880-9982-9bbf2f908ddf',
|
||||
displayName: 'class04',
|
||||
teachers: teacherClass04,
|
||||
students: studentsClass04,
|
||||
});
|
||||
|
||||
return [class01, class02, class03, class04];
|
||||
classWithTestleerlingAndTestleerkracht = em.create(Class, {
|
||||
classId: "a75298b5-18aa-471d-8eeb-5d77eb989393",
|
||||
displayName: 'Testklasse',
|
||||
teachers: [getTestleerkracht1()],
|
||||
students: [getTestleerling1()]
|
||||
});
|
||||
|
||||
return [class01, class02, class03, class04, classWithTestleerlingAndTestleerkracht];
|
||||
}
|
||||
|
||||
let class01: Class;
|
||||
let class02: Class;
|
||||
let class03: Class;
|
||||
let class04: Class;
|
||||
let classWithTestleerlingAndTestleerkracht: Class;
|
||||
|
||||
export function getClass01(): Class {
|
||||
return class01;
|
||||
}
|
||||
|
||||
export function getClass02(): Class {
|
||||
return class02;
|
||||
}
|
||||
|
||||
export function getClass03(): Class {
|
||||
return class03;
|
||||
}
|
||||
|
||||
export function getClass04(): Class {
|
||||
return class04;
|
||||
}
|
||||
|
||||
export function getClassWithTestleerlingAndTestleerkracht(): Class {
|
||||
return classWithTestleerlingAndTestleerkracht;
|
||||
}
|
||||
|
|
|
@ -2,30 +2,35 @@ import { EntityManager } from '@mikro-orm/core';
|
|||
import { TeacherInvitation } from '../../../src/entities/classes/teacher-invitation.entity';
|
||||
import { Teacher } from '../../../src/entities/users/teacher.entity';
|
||||
import { Class } from '../../../src/entities/classes/class.entity';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export function makeTestTeacherInvitations(em: EntityManager, teachers: Teacher[], classes: Class[]): TeacherInvitation[] {
|
||||
const teacherInvitation01 = em.create(TeacherInvitation, {
|
||||
sender: teachers[1],
|
||||
receiver: teachers[0],
|
||||
class: classes[1],
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const teacherInvitation02 = em.create(TeacherInvitation, {
|
||||
sender: teachers[1],
|
||||
receiver: teachers[2],
|
||||
class: classes[1],
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const teacherInvitation03 = em.create(TeacherInvitation, {
|
||||
sender: teachers[2],
|
||||
receiver: teachers[0],
|
||||
class: classes[2],
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
const teacherInvitation04 = em.create(TeacherInvitation, {
|
||||
sender: teachers[0],
|
||||
receiver: teachers[1],
|
||||
class: classes[0],
|
||||
status: ClassStatus.Open,
|
||||
});
|
||||
|
||||
return [teacherInvitation01, teacherInvitation02, teacherInvitation03, teacherInvitation04];
|
||||
|
|
|
@ -15,7 +15,14 @@ export const TEST_STUDENTS = [
|
|||
{ username: 'testleerling1', firstName: 'Gerald', lastName: 'Schmittinger' },
|
||||
];
|
||||
|
||||
let testStudents: Student[];
|
||||
|
||||
// 🏗️ Functie die ORM entities maakt uit de data array
|
||||
export function makeTestStudents(em: EntityManager): Student[] {
|
||||
return TEST_STUDENTS.map((data) => em.create(Student, data));
|
||||
testStudents = TEST_STUDENTS.map((data) => em.create(Student, data));
|
||||
return testStudents;
|
||||
}
|
||||
|
||||
export function getTestleerling1(): Student {
|
||||
return testStudents.find(it => it.username == "testleerling1");
|
||||
}
|
||||
|
|
|
@ -2,37 +2,64 @@ import { Teacher } from '../../../src/entities/users/teacher.entity';
|
|||
import { EntityManager } from '@mikro-orm/core';
|
||||
|
||||
export function makeTestTeachers(em: EntityManager): Teacher[] {
|
||||
const teacher01 = em.create(Teacher, {
|
||||
teacher01 = em.create(Teacher, {
|
||||
username: 'FooFighters',
|
||||
firstName: 'Dave',
|
||||
lastName: 'Grohl',
|
||||
});
|
||||
|
||||
const teacher02 = em.create(Teacher, {
|
||||
teacher02 = em.create(Teacher, {
|
||||
username: 'LimpBizkit',
|
||||
firstName: 'Fred',
|
||||
lastName: 'Durst',
|
||||
});
|
||||
|
||||
const teacher03 = em.create(Teacher, {
|
||||
teacher03 = em.create(Teacher, {
|
||||
username: 'Staind',
|
||||
firstName: 'Aaron',
|
||||
lastName: 'Lewis',
|
||||
});
|
||||
|
||||
// Should not be used, gets deleted in a unit test
|
||||
const teacher04 = em.create(Teacher, {
|
||||
teacher04 = em.create(Teacher, {
|
||||
username: 'ZesdeMetaal',
|
||||
firstName: 'Wannes',
|
||||
lastName: 'Cappelle',
|
||||
});
|
||||
|
||||
// Makes sure when logged in as testleerkracht1, there exists a corresponding user
|
||||
const teacher05 = em.create(Teacher, {
|
||||
testleerkracht1 = em.create(Teacher, {
|
||||
username: 'testleerkracht1',
|
||||
firstName: 'Bob',
|
||||
lastName: 'Dylan',
|
||||
firstName: 'Kris',
|
||||
lastName: 'Coolsaet',
|
||||
});
|
||||
|
||||
return [teacher01, teacher02, teacher03, teacher04, teacher05];
|
||||
return [teacher01, teacher02, teacher03, teacher04, testleerkracht1];
|
||||
}
|
||||
|
||||
let teacher01: Teacher;
|
||||
let teacher02: Teacher;
|
||||
let teacher03: Teacher;
|
||||
let teacher04: Teacher;
|
||||
let testleerkracht1: Teacher;
|
||||
|
||||
export function getTeacher01(): Teacher {
|
||||
return teacher01;
|
||||
}
|
||||
|
||||
export function getTeacher02(): Teacher {
|
||||
return teacher02;
|
||||
}
|
||||
|
||||
export function getTeacher03(): Teacher {
|
||||
return teacher03;
|
||||
}
|
||||
|
||||
export function getTeacher04(): Teacher {
|
||||
return teacher04;
|
||||
}
|
||||
|
||||
export function getTestleerkracht1(): Teacher {
|
||||
return testleerkracht1;
|
||||
}
|
||||
|
||||
|
|
|
@ -49,7 +49,6 @@ export async function seedDatabase(): Promise<void> {
|
|||
const answers = makeTestAnswers(em, teachers, questions);
|
||||
const submissions = makeTestSubmissions(em, students, groups);
|
||||
|
||||
|
||||
// Persist all entities
|
||||
await em.persistAndFlush([
|
||||
...students,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue