Merge remote-tracking branch 'origin/dev' into feat/endpoints-beschermen-met-authenticatie-#105
# Conflicts: # backend/src/controllers/assignments.ts # backend/src/controllers/questions.ts # backend/src/data/questions/question-repository.ts # backend/src/interfaces/question.ts # backend/src/routes/assignments.ts # backend/src/routes/classes.ts # backend/src/routes/groups.ts # backend/src/routes/teachers.ts # backend/src/services/questions.ts # common/src/interfaces/question.ts
This commit is contained in:
commit
ac399153b6
71 changed files with 2075 additions and 2603 deletions
21
backend/.env-old
Normal file
21
backend/.env-old
Normal file
|
@ -0,0 +1,21 @@
|
|||
PORT=3000
|
||||
DWENGO_DB_HOST=db
|
||||
DWENGO_DB_PORT=5432
|
||||
DWENGO_DB_USERNAME=postgres
|
||||
DWENGO_DB_PASSWORD=postgres
|
||||
DWENGO_DB_UPDATE=false
|
||||
|
||||
DWENGO_AUTH_STUDENT_URL=http://localhost/idp/realms/student
|
||||
DWENGO_AUTH_STUDENT_CLIENT_ID=dwengo
|
||||
DWENGO_AUTH_STUDENT_JWKS_ENDPOINT=http://idp:7080/idp/realms/student/protocol/openid-connect/certs
|
||||
DWENGO_AUTH_TEACHER_URL=http://localhost/idp/realms/teacher
|
||||
DWENGO_AUTH_TEACHER_CLIENT_ID=dwengo
|
||||
DWENGO_AUTH_TEACHER_JWKS_ENDPOINT=http://idp:7080/idp/realms/teacher/protocol/openid-connect/certs
|
||||
|
||||
# Allow Vite dev-server to access the backend (for testing purposes). Don't forget to remove this in production!
|
||||
#DWENGO_CORS_ALLOWED_ORIGINS=http://localhost/,127.0.0.1:80,http://127.0.0.1,http://localhost:80,http://127.0.0.1:80,localhost
|
||||
DWENGO_CORS_ALLOWED_ORIGINS=http://localhost/*,http://idp:7080,https://idp:7080
|
||||
|
||||
# Logging and monitoring
|
||||
|
||||
LOKI_HOST=http://logging:3102
|
|
@ -6,8 +6,9 @@ WORKDIR /app/dwengo
|
|||
|
||||
COPY package*.json ./
|
||||
COPY backend/package.json ./backend/
|
||||
# Backend depends on common
|
||||
# Backend depends on common and docs
|
||||
COPY common/package.json ./common/
|
||||
COPY docs/package.json ./docs/
|
||||
|
||||
RUN npm install --silent
|
||||
|
||||
|
@ -34,6 +35,7 @@ COPY ./backend/i18n ./i18n
|
|||
|
||||
COPY --from=build-stage /app/dwengo/common/dist ./common/dist
|
||||
COPY --from=build-stage /app/dwengo/backend/dist ./backend/dist
|
||||
COPY --from=build-stage /app/dwengo/docs/api/swagger.json ./docs/api/swagger.json
|
||||
|
||||
COPY package*.json ./
|
||||
COPY backend/package.json ./backend/
|
||||
|
@ -42,7 +44,6 @@ COPY common/package.json ./common/
|
|||
|
||||
RUN npm install --silent --only=production
|
||||
|
||||
COPY ./docs ./docs
|
||||
COPY ./backend/i18n ./backend/i18n
|
||||
|
||||
EXPOSE 3000
|
||||
|
|
|
@ -1,77 +1,94 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js';
|
||||
import {
|
||||
createAssignment,
|
||||
deleteAssignment,
|
||||
getAllAssignments,
|
||||
getAssignment,
|
||||
getAssignmentsSubmissions,
|
||||
putAssignment,
|
||||
} from '../services/assignments.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { Assignment } from '../entities/assignments/assignment.entity.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
|
||||
// Typescript is annoying with parameter forwarding from class.ts
|
||||
export interface AssignmentParams {
|
||||
classid: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export async function getAllAssignmentsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
|
||||
const classid = req.params.classid;
|
||||
export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const full = req.query.full === 'true';
|
||||
|
||||
const assignments = await getAllAssignments(classid, full);
|
||||
const assignments = await getAllAssignments(classId, full);
|
||||
|
||||
res.json({
|
||||
assignments: assignments,
|
||||
});
|
||||
res.json({ assignments });
|
||||
}
|
||||
|
||||
export async function createAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
|
||||
export async function createAssignmentHandler(req: Request, res: Response): Promise<void> {
|
||||
const classid = req.params.classid;
|
||||
const description = req.body.description;
|
||||
const language = req.body.language;
|
||||
const learningPath = req.body.learningPath;
|
||||
const title = req.body.title;
|
||||
|
||||
requireFields({ description, language, learningPath, title });
|
||||
|
||||
const assignmentData = req.body as AssignmentDTO;
|
||||
|
||||
if (!assignmentData.description || !assignmentData.language || !assignmentData.learningPath || !assignmentData.title) {
|
||||
res.status(400).json({
|
||||
error: 'Missing one or more required fields: title, description, learningPath, language',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const assignment = await createAssignment(classid, assignmentData);
|
||||
|
||||
if (!assignment) {
|
||||
res.status(500).json({ error: 'Could not create assignment ' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(assignment);
|
||||
res.json({ assignment });
|
||||
}
|
||||
|
||||
export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
|
||||
export async function getAssignmentHandler(req: Request, res: Response): Promise<void> {
|
||||
const id = Number(req.params.id);
|
||||
const classid = req.params.classid;
|
||||
requireFields({ id, classid });
|
||||
|
||||
if (isNaN(id)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id should be a number');
|
||||
}
|
||||
|
||||
const assignment = await getAssignment(classid, id);
|
||||
|
||||
if (!assignment) {
|
||||
res.status(404).json({ error: 'Assignment not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.json(assignment);
|
||||
res.json({ assignment });
|
||||
}
|
||||
|
||||
export async function getAssignmentsSubmissionsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
|
||||
export async function putAssignmentHandler(req: Request, res: Response): Promise<void> {
|
||||
const id = Number(req.params.id);
|
||||
const classid = req.params.classid;
|
||||
requireFields({ id, classid });
|
||||
|
||||
if (isNaN(id)) {
|
||||
throw new BadRequestException('Assignment id should be a number');
|
||||
}
|
||||
|
||||
const assignmentData = req.body as Partial<EntityDTO<Assignment>>;
|
||||
const assignment = await putAssignment(classid, id, assignmentData);
|
||||
|
||||
res.json({ assignment });
|
||||
}
|
||||
|
||||
export async function deleteAssignmentHandler(req: Request, _res: Response): Promise<void> {
|
||||
const id = Number(req.params.id);
|
||||
const classid = req.params.classid;
|
||||
requireFields({ id, classid });
|
||||
|
||||
if (isNaN(id)) {
|
||||
throw new BadRequestException('Assignment id should be a number');
|
||||
}
|
||||
|
||||
await deleteAssignment(classid, id);
|
||||
}
|
||||
|
||||
export async function getAssignmentsSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classid = req.params.classid;
|
||||
const assignmentNumber = Number(req.params.id);
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ assignmentNumber, classid });
|
||||
|
||||
if (isNaN(assignmentNumber)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id should be a number');
|
||||
}
|
||||
|
||||
const submissions = await getAssignmentsSubmissions(classid, assignmentNumber, full);
|
||||
|
||||
res.json({
|
||||
submissions: submissions,
|
||||
});
|
||||
res.json({ submissions });
|
||||
}
|
||||
|
|
|
@ -1,44 +1,62 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/classes.js';
|
||||
import {
|
||||
addClassStudent,
|
||||
addClassTeacher,
|
||||
createClass,
|
||||
deleteClass,
|
||||
deleteClassStudent,
|
||||
deleteClassTeacher,
|
||||
getAllClasses,
|
||||
getClass,
|
||||
getClassStudents,
|
||||
getClassTeacherInvitations,
|
||||
getClassTeachers,
|
||||
putClass,
|
||||
} from '../services/classes.js';
|
||||
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
|
||||
export async function getAllClassesHandler(req: Request, res: Response): Promise<void> {
|
||||
const full = req.query.full === 'true';
|
||||
const classes = await getAllClasses(full);
|
||||
|
||||
res.json({
|
||||
classes: classes,
|
||||
});
|
||||
res.json({ classes });
|
||||
}
|
||||
|
||||
export async function createClassHandler(req: Request, res: Response): Promise<void> {
|
||||
const displayName = req.body.displayName;
|
||||
requireFields({ displayName });
|
||||
|
||||
const classData = req.body as ClassDTO;
|
||||
|
||||
if (!classData.displayName) {
|
||||
res.status(400).json({
|
||||
error: 'Missing one or more required fields: displayName',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const cls = await createClass(classData);
|
||||
|
||||
if (!cls) {
|
||||
res.status(500).json({ error: 'Something went wrong while creating class' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json({ class: cls });
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function getClassHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
requireFields({ classId });
|
||||
|
||||
const cls = await getClass(classId);
|
||||
|
||||
if (!cls) {
|
||||
res.status(404).json({ error: 'Class not found' });
|
||||
return;
|
||||
}
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function putClassHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
requireFields({ classId });
|
||||
|
||||
const newData = req.body as Partial<EntityDTO<Class>>;
|
||||
const cls = await putClass(classId, newData);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function deleteClassHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const cls = await deleteClass(classId);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
@ -46,21 +64,69 @@ export async function getClassHandler(req: Request, res: Response): Promise<void
|
|||
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ classId });
|
||||
|
||||
const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId);
|
||||
const students = await getClassStudents(classId, full);
|
||||
|
||||
res.json({
|
||||
students: students,
|
||||
});
|
||||
res.json({ students });
|
||||
}
|
||||
|
||||
export async function getClassTeachersHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ classId });
|
||||
|
||||
const teachers = await getClassTeachers(classId, full);
|
||||
|
||||
res.json({ teachers });
|
||||
}
|
||||
|
||||
export async function getTeacherInvitationsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ classId });
|
||||
|
||||
const invitations = await getClassTeacherInvitations(classId, full);
|
||||
|
||||
res.json({
|
||||
invitations: invitations,
|
||||
});
|
||||
res.json({ invitations });
|
||||
}
|
||||
|
||||
export async function deleteClassStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const username = req.params.username;
|
||||
requireFields({ classId, username });
|
||||
|
||||
const cls = await deleteClassStudent(classId, username);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function deleteClassTeacherHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const username = req.params.username;
|
||||
requireFields({ classId, username });
|
||||
|
||||
const cls = await deleteClassTeacher(classId, username);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function addClassStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const username = req.body.username;
|
||||
requireFields({ classId, username });
|
||||
|
||||
const cls = await addClassStudent(classId, username);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function addClassTeacherHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const username = req.body.username;
|
||||
requireFields({ classId, username });
|
||||
|
||||
const cls = await addClassTeacher(classId, username);
|
||||
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
|
|
@ -1,100 +1,104 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createGroup, getAllGroups, getGroup, getGroupSubmissions } from '../services/groups.js';
|
||||
import { createGroup, deleteGroup, getAllGroups, getGroup, getGroupSubmissions, putGroup } from '../services/groups.js';
|
||||
import { GroupDTO } from '@dwengo-1/common/interfaces/group';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
|
||||
// Typescript is annoywith with parameter forwarding from class.ts
|
||||
interface GroupParams {
|
||||
classid: string;
|
||||
assignmentid: string;
|
||||
groupid?: string;
|
||||
}
|
||||
|
||||
export async function getGroupHandler(req: Request<GroupParams>, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const full = req.query.full === 'true';
|
||||
const assignmentId = Number(req.params.assignmentid);
|
||||
function checkGroupFields(classId: string, assignmentId: number, groupId: number): void {
|
||||
requireFields({ classId, assignmentId, groupId });
|
||||
|
||||
if (isNaN(assignmentId)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id must be a number');
|
||||
}
|
||||
|
||||
const groupId = Number(req.params.groupid!); // Can't be undefined
|
||||
|
||||
if (isNaN(groupId)) {
|
||||
res.status(400).json({ error: 'Group id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Group id must be a number');
|
||||
}
|
||||
}
|
||||
|
||||
const group = await getGroup(classId, assignmentId, groupId, full);
|
||||
export async function getGroupHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const assignmentId = parseInt(req.params.assignmentid);
|
||||
const groupId = parseInt(req.params.groupid);
|
||||
checkGroupFields(classId, assignmentId, groupId);
|
||||
|
||||
if (!group) {
|
||||
res.status(404).json({ error: 'Group not found' });
|
||||
return;
|
||||
}
|
||||
const group = await getGroup(classId, assignmentId, groupId);
|
||||
|
||||
res.json(group);
|
||||
res.json({ group });
|
||||
}
|
||||
|
||||
export async function putGroupHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const assignmentId = parseInt(req.params.assignmentid);
|
||||
const groupId = parseInt(req.params.groupid);
|
||||
checkGroupFields(classId, assignmentId, groupId);
|
||||
|
||||
const group = await putGroup(classId, assignmentId, groupId, req.body as Partial<EntityDTO<Group>>);
|
||||
|
||||
res.json({ group });
|
||||
}
|
||||
|
||||
export async function deleteGroupHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const assignmentId = parseInt(req.params.assignmentid);
|
||||
const groupId = parseInt(req.params.groupid);
|
||||
checkGroupFields(classId, assignmentId, groupId);
|
||||
|
||||
const group = await deleteGroup(classId, assignmentId, groupId);
|
||||
|
||||
res.json({ group });
|
||||
}
|
||||
|
||||
export async function getAllGroupsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const full = req.query.full === 'true';
|
||||
|
||||
const assignmentId = Number(req.params.assignmentid);
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ classId, assignmentId });
|
||||
|
||||
if (isNaN(assignmentId)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id must be a number');
|
||||
}
|
||||
|
||||
const groups = await getAllGroups(classId, assignmentId, full);
|
||||
|
||||
res.json({
|
||||
groups: groups,
|
||||
});
|
||||
res.json({ groups });
|
||||
}
|
||||
|
||||
export async function createGroupHandler(req: Request, res: Response): Promise<void> {
|
||||
const classid = req.params.classid;
|
||||
const assignmentId = Number(req.params.assignmentid);
|
||||
|
||||
requireFields({ classid, assignmentId });
|
||||
|
||||
if (isNaN(assignmentId)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id must be a number');
|
||||
}
|
||||
|
||||
const groupData = req.body as GroupDTO;
|
||||
const group = await createGroup(groupData, classid, assignmentId);
|
||||
|
||||
if (!group) {
|
||||
res.status(500).json({ error: 'Something went wrong while creating group' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(group);
|
||||
res.status(201).json({ group });
|
||||
}
|
||||
|
||||
export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
const assignmentId = Number(req.params.assignmentid);
|
||||
const groupId = Number(req.params.groupid);
|
||||
const full = req.query.full === 'true';
|
||||
|
||||
const assignmentId = Number(req.params.assignmentid);
|
||||
requireFields({ classId, assignmentId, groupId });
|
||||
|
||||
if (isNaN(assignmentId)) {
|
||||
res.status(400).json({ error: 'Assignment id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Assignment id must be a number');
|
||||
}
|
||||
|
||||
const groupId = Number(req.params.groupid); // Can't be undefined
|
||||
|
||||
if (isNaN(groupId)) {
|
||||
res.status(400).json({ error: 'Group id must be a number' });
|
||||
return;
|
||||
throw new BadRequestException('Group id must be a number');
|
||||
}
|
||||
|
||||
const submissions = await getGroupSubmissions(classId, assignmentId, groupId, full);
|
||||
|
||||
res.json({
|
||||
submissions: submissions,
|
||||
});
|
||||
res.json({ submissions });
|
||||
}
|
||||
|
|
|
@ -3,16 +3,15 @@ import {
|
|||
createQuestion,
|
||||
deleteQuestion,
|
||||
getAllQuestions,
|
||||
getAnswersByQuestion,
|
||||
getQuestion,
|
||||
getQuestionsAboutLearningObjectInAssignment, updateQuestion,
|
||||
getQuestionsAboutLearningObjectInAssignment,
|
||||
updateQuestion,
|
||||
} from '../services/questions.js';
|
||||
import {FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM} from '../config.js';
|
||||
import { FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM } from '../config.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import {requireFields} from "./error-helper";
|
||||
import {BadRequestException} from "../exceptions/bad-request-exception";
|
||||
import { requireFields } from './error-helper.js';
|
||||
|
||||
export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier {
|
||||
return {
|
||||
|
@ -29,20 +28,6 @@ export function getQuestionId(learningObjectIdentifier: LearningObjectIdentifier
|
|||
};
|
||||
}
|
||||
|
||||
function getQuestionIdFromRequest(req: Request): QuestionId | null {
|
||||
const seq = req.params.seq;
|
||||
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;
|
||||
}
|
||||
|
||||
return getQuestionId(learningObjectIdentifier, seq);
|
||||
}
|
||||
|
||||
export async function getAllQuestionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const hruid = req.params.hruid;
|
||||
const version = req.params.version;
|
||||
|
@ -50,12 +35,6 @@ export async function getAllQuestionsHandler(req: Request, res: Response): Promi
|
|||
const full = req.query.full === 'true';
|
||||
requireFields({ hruid });
|
||||
|
||||
const assignmentId = parseInt(req.query.assignmentId as string);
|
||||
|
||||
if (isNaN(assignmentId)) {
|
||||
throw new BadRequestException("The assignment ID must be a number.");
|
||||
}
|
||||
|
||||
const learningObjectId = getLearningObjectId(hruid, version, language);
|
||||
|
||||
let questions: QuestionDTO[] | QuestionId[];
|
||||
|
@ -89,23 +68,6 @@ export async function getQuestionHandler(req: Request, res: Response): Promise<v
|
|||
res.json({ question });
|
||||
}
|
||||
|
||||
export async function getQuestionAnswersHandler(req: Request, res: Response): Promise<void> {
|
||||
const questionId = getQuestionIdFromRequest(req);
|
||||
const full = req.query.full;
|
||||
|
||||
if (!questionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const answers = await getAnswersByQuestion(questionId, full === "true");
|
||||
|
||||
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 hruid = req.params.hruid;
|
||||
const version = req.params.version;
|
||||
|
@ -116,7 +78,7 @@ export async function createQuestionHandler(req: Request, res: Response): Promis
|
|||
|
||||
const author = req.body.author as string;
|
||||
const content = req.body.content as string;
|
||||
const inGroup = req.body.inGroup as string;
|
||||
const inGroup = req.body.inGroup;
|
||||
requireFields({ author, content, inGroup });
|
||||
|
||||
const questionData = req.body as QuestionData;
|
||||
|
|
|
@ -1,83 +1,83 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createSubmission, deleteSubmission, getSubmission, getSubmissionsForLearningObjectAndAssignment } from '../services/submissions.js';
|
||||
import {
|
||||
createSubmission,
|
||||
deleteSubmission,
|
||||
getAllSubmissions,
|
||||
getSubmission,
|
||||
getSubmissionsForLearningObjectAndAssignment,
|
||||
} from '../services/submissions.js';
|
||||
import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission';
|
||||
import { Language, languageMap } from '@dwengo-1/common/util/language';
|
||||
import { Submission } from '../entities/assignments/submission.entity';
|
||||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
|
||||
interface SubmissionParams {
|
||||
hruid: string;
|
||||
id: number;
|
||||
}
|
||||
|
||||
interface SubmissionQuery {
|
||||
language: string;
|
||||
version: number;
|
||||
}
|
||||
|
||||
interface SubmissionsQuery extends SubmissionQuery {
|
||||
classId: string;
|
||||
assignmentId: number;
|
||||
studentUsername?: string;
|
||||
}
|
||||
|
||||
export async function getSubmissionsHandler(req: Request<SubmissionParams, Submission[], null, SubmissionsQuery>, res: Response): Promise<void> {
|
||||
export async function getSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const loHruid = req.params.hruid;
|
||||
const lang = languageMap[req.query.language] || Language.Dutch;
|
||||
const version = req.query.version || 1;
|
||||
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, req.query.assignmentId);
|
||||
const submissions = await getSubmissionsForLearningObjectAndAssignment(
|
||||
loHruid,
|
||||
lang,
|
||||
version,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string)
|
||||
);
|
||||
|
||||
res.json(submissions);
|
||||
}
|
||||
|
||||
export async function getSubmissionHandler(req: Request<SubmissionParams>, res: Response): Promise<void> {
|
||||
export async function getSubmissionHandler(req: Request, res: Response): Promise<void> {
|
||||
const lohruid = req.params.hruid;
|
||||
const submissionNumber = Number(req.params.id);
|
||||
|
||||
if (isNaN(submissionNumber)) {
|
||||
res.status(400).json({ error: 'Submission number is not a number' });
|
||||
return;
|
||||
}
|
||||
|
||||
const lang = languageMap[req.query.language as string] || Language.Dutch;
|
||||
const version = (req.query.version || 1) as number;
|
||||
const submissionNumber = Number(req.params.id);
|
||||
requireFields({ lohruid, submissionNumber });
|
||||
|
||||
const submission = await getSubmission(lohruid, lang, version, submissionNumber);
|
||||
|
||||
if (!submission) {
|
||||
res.status(404).json({ error: 'Submission not found' });
|
||||
return;
|
||||
if (isNaN(submissionNumber)) {
|
||||
throw new BadRequestException('Submission number must be a number');
|
||||
}
|
||||
|
||||
res.json(submission);
|
||||
const loId = new LearningObjectIdentifier(lohruid, lang, version);
|
||||
const submission = await getSubmission(loId, submissionNumber);
|
||||
|
||||
res.json({ submission });
|
||||
}
|
||||
|
||||
export async function getAllSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const lohruid = req.params.hruid;
|
||||
const lang = languageMap[req.query.language as string] || Language.Dutch;
|
||||
const version = (req.query.version || 1) as number;
|
||||
requireFields({ lohruid });
|
||||
|
||||
const loId = new LearningObjectIdentifier(lohruid, lang, version);
|
||||
const submissions = await getAllSubmissions(loId);
|
||||
|
||||
res.json({ submissions });
|
||||
}
|
||||
|
||||
// TODO: gerald moet nog dingen toevoegen aan de databank voor dat dit gefinaliseerd kan worden
|
||||
export async function createSubmissionHandler(req: Request, res: Response): Promise<void> {
|
||||
const submissionDTO = req.body as SubmissionDTO;
|
||||
|
||||
const submission = await createSubmission(submissionDTO);
|
||||
|
||||
if (!submission) {
|
||||
res.status(400).json({ error: 'Failed to create submission' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.json(submission);
|
||||
res.json({ submission });
|
||||
}
|
||||
|
||||
export async function deleteSubmissionHandler(req: Request, res: Response): Promise<void> {
|
||||
const hruid = req.params.hruid;
|
||||
const submissionNumber = Number(req.params.id);
|
||||
|
||||
const lang = languageMap[req.query.language as string] || Language.Dutch;
|
||||
const version = (req.query.version || 1) as number;
|
||||
const submissionNumber = Number(req.params.id);
|
||||
requireFields({ hruid, submissionNumber });
|
||||
|
||||
const submission = await deleteSubmission(hruid, lang, version, submissionNumber);
|
||||
|
||||
if (!submission) {
|
||||
res.status(404).json({ error: 'Submission not found' });
|
||||
return;
|
||||
if (isNaN(submissionNumber)) {
|
||||
throw new BadRequestException('Submission number must be a number');
|
||||
}
|
||||
|
||||
res.json(submission);
|
||||
const loId = new LearningObjectIdentifier(hruid, lang, version);
|
||||
const submission = await deleteSubmission(loId, submissionNumber);
|
||||
|
||||
res.json({ submission });
|
||||
}
|
||||
|
|
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 });
|
||||
}
|
|
@ -81,16 +81,15 @@ export async function getTeacherQuestionHandler(req: Request, res: Response): Pr
|
|||
}
|
||||
|
||||
export async function getStudentJoinRequestHandler(req: Request, res: Response): Promise<void> {
|
||||
const username = req.query.username as string;
|
||||
const classId = req.params.classId;
|
||||
requireFields({ username, classId });
|
||||
requireFields({ classId });
|
||||
|
||||
const joinRequests = await getJoinRequestsByClass(classId);
|
||||
res.json({ joinRequests });
|
||||
}
|
||||
|
||||
export async function updateStudentJoinRequestHandler(req: Request, res: Response): Promise<void> {
|
||||
const studentUsername = req.query.studentUsername as string;
|
||||
const studentUsername = req.params.studentUsername;
|
||||
const classId = req.params.classId;
|
||||
const accepted = req.body.accepted !== 'false'; // Default = true
|
||||
requireFields({ studentUsername, classId });
|
||||
|
|
|
@ -18,6 +18,14 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> {
|
|||
});
|
||||
}
|
||||
|
||||
public async findByLearningObject(loId: LearningObjectIdentifier): Promise<Submission[]> {
|
||||
return this.find({
|
||||
learningObjectHruid: loId.hruid,
|
||||
learningObjectLanguage: loId.language,
|
||||
learningObjectVersion: loId.version,
|
||||
});
|
||||
}
|
||||
|
||||
public async findMostRecentSubmissionForStudent(loId: LearningObjectIdentifier, submitter: Student): Promise<Submission | null> {
|
||||
return this.findOne(
|
||||
{
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ import { Question } from '../../entities/questions/question.entity.js';
|
|||
import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js';
|
||||
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 { Group } from '../../entities/assignments/group.entity.js';
|
||||
import { Assignment } from '../../entities/assignments/assignment.entity.js';
|
||||
import { Loaded } from '@mikro-orm/core';
|
||||
|
||||
export class QuestionRepository extends DwengoEntityRepository<Question> {
|
||||
|
@ -60,6 +60,16 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
|
|||
});
|
||||
}
|
||||
|
||||
public async findAllByAssignment(assignment: Assignment): Promise<Question[]> {
|
||||
return this.find({
|
||||
inGroup: {
|
||||
$contained: assignment.groups,
|
||||
},
|
||||
learningObjectHruid: assignment.learningPathHruid,
|
||||
learningObjectLanguage: assignment.learningPathLanguage,
|
||||
});
|
||||
}
|
||||
|
||||
public async findAllByAuthor(author: Student): Promise<Question[]> {
|
||||
return this.findAll({
|
||||
where: { author },
|
||||
|
@ -67,21 +77,6 @@ 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.
|
||||
|
@ -113,4 +108,19 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import { Entity, Enum, ManyToOne, PrimaryKey, Property } from '@mikro-orm/core';
|
|||
import { Student } from '../users/student.entity.js';
|
||||
import { QuestionRepository } from '../../data/questions/question-repository.js';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import { Group } from '../assignments/group.entity';
|
||||
import { Group } from '../assignments/group.entity.js';
|
||||
|
||||
@Entity({ repository: () => QuestionRepository })
|
||||
export class Question {
|
||||
|
|
|
@ -8,19 +8,18 @@ import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
|||
export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
|
||||
return {
|
||||
id: assignment.id!,
|
||||
class: assignment.within.classId!,
|
||||
within: assignment.within.classId!,
|
||||
title: assignment.title,
|
||||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
language: assignment.learningPathLanguage,
|
||||
// Groups: assignment.groups.map(group => group.groupNumber),
|
||||
};
|
||||
}
|
||||
|
||||
export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO {
|
||||
return {
|
||||
id: assignment.id!,
|
||||
class: assignment.within.classId!,
|
||||
within: assignment.within.classId!,
|
||||
title: assignment.title,
|
||||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
|
|
|
@ -10,7 +10,6 @@ export function mapToClassDTO(cls: Class): ClassDTO {
|
|||
displayName: cls.displayName,
|
||||
teachers: cls.teachers.map((teacher) => teacher.username),
|
||||
students: cls.students.map((student) => student.username),
|
||||
joinRequests: [], // TODO
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from './assignment.js';
|
||||
import { mapToStudent, mapToStudentDTO } from './student.js';
|
||||
import { mapToAssignment } from './assignment.js';
|
||||
import { mapToStudent } from './student.js';
|
||||
import { mapToAssignmentDTO } from './assignment.js';
|
||||
import { mapToStudentDTO } from './student.js';
|
||||
import { GroupDTO } from '@dwengo-1/common/interfaces/group';
|
||||
import { getGroupRepository } from '../data/repositories';
|
||||
import { getGroupRepository } from '../data/repositories.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { Class } from '../entities/classes/class.entity';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import { mapToClassDTO } from './class';
|
||||
|
||||
export function mapToGroup(groupDto: GroupDTO, clazz: Class): Group {
|
||||
const assignmentDto = groupDto.assignment as AssignmentDTO;
|
||||
|
@ -19,7 +22,8 @@ export function mapToGroup(groupDto: GroupDTO, clazz: Class): Group {
|
|||
|
||||
export function mapToGroupDTO(group: Group): GroupDTO {
|
||||
return {
|
||||
assignment: mapToAssignmentDTO(group.assignment), // ERROR: , group.assignment.within),
|
||||
class: mapToClassDTO(group.assignment.within),
|
||||
assignment: mapToAssignmentDTO(group.assignment),
|
||||
groupNumber: group.groupNumber!,
|
||||
members: group.members.map(mapToStudentDTO),
|
||||
};
|
||||
|
@ -27,7 +31,8 @@ export function mapToGroupDTO(group: Group): GroupDTO {
|
|||
|
||||
export function mapToGroupDTOId(group: Group): GroupDTO {
|
||||
return {
|
||||
assignment: mapToAssignmentDTOId(group.assignment),
|
||||
class: group.assignment.within.classId!,
|
||||
assignment: group.assignment.id!,
|
||||
groupNumber: group.groupNumber!,
|
||||
};
|
||||
}
|
||||
|
@ -37,6 +42,7 @@ export function mapToGroupDTOId(group: Group): GroupDTO {
|
|||
*/
|
||||
export function mapToShallowGroupDTO(group: Group): GroupDTO {
|
||||
return {
|
||||
class: group.assignment.within.classId!,
|
||||
assignment: group.assignment.id!,
|
||||
groupNumber: group.groupNumber!,
|
||||
members: group.members.map((member) => member.username),
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { Question } from '../entities/questions/question.entity.js';
|
||||
import { mapToStudentDTO } from './student.js';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { mapToGroupDTOId } from './group';
|
||||
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
|
||||
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier";
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { mapToGroupDTOId } from './group.js';
|
||||
|
||||
function getLearningObjectIdentifier(question: Question): LearningObjectIdentifierDTO {
|
||||
return {
|
||||
|
|
|
@ -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,7 +1,10 @@
|
|||
import { Submission } from '../entities/assignments/submission.entity.js';
|
||||
import { mapToGroupDTO } from './group.js';
|
||||
import { mapToStudent, mapToStudentDTO } from './student.js';
|
||||
import { mapToStudentDTO } from './student.js';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { getSubmissionRepository } from '../data/repositories';
|
||||
import { Student } from '../entities/users/student.entity';
|
||||
import { Group } from '../entities/assignments/group.entity';
|
||||
|
||||
export function mapToSubmissionDTO(submission: Submission): SubmissionDTO {
|
||||
return {
|
||||
|
@ -29,16 +32,14 @@ export function mapToSubmissionDTOId(submission: Submission): SubmissionDTOId {
|
|||
};
|
||||
}
|
||||
|
||||
export function mapToSubmission(submissionDTO: SubmissionDTO): Submission {
|
||||
const submission = new Submission();
|
||||
submission.learningObjectHruid = submissionDTO.learningObjectIdentifier.hruid;
|
||||
submission.learningObjectLanguage = submissionDTO.learningObjectIdentifier.language;
|
||||
submission.learningObjectVersion = submissionDTO.learningObjectIdentifier.version!;
|
||||
// Submission.submissionNumber = submissionDTO.submissionNumber;
|
||||
submission.submitter = mapToStudent(submissionDTO.submitter);
|
||||
// Submission.submissionTime = submissionDTO.time;
|
||||
// Submission.onBehalfOf = submissionDTO.group!;
|
||||
submission.content = submissionDTO.content;
|
||||
|
||||
return submission;
|
||||
export function mapToSubmission(submissionDTO: SubmissionDTO, submitter: Student, onBehalfOf: Group): Submission {
|
||||
return getSubmissionRepository().create({
|
||||
learningObjectHruid: submissionDTO.learningObjectIdentifier.hruid,
|
||||
learningObjectLanguage: submissionDTO.learningObjectIdentifier.language,
|
||||
learningObjectVersion: submissionDTO.learningObjectIdentifier.version || 1,
|
||||
submitter: submitter,
|
||||
submissionTime: new Date(),
|
||||
content: submissionDTO.content,
|
||||
onBehalfOf: onBehalfOf,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
import express from 'express';
|
||||
import {
|
||||
createAssignmentHandler,
|
||||
deleteAssignmentHandler,
|
||||
getAllAssignmentsHandler,
|
||||
getAssignmentHandler,
|
||||
getAssignmentsSubmissionsHandler,
|
||||
putAssignmentHandler,
|
||||
} from '../controllers/assignments.js';
|
||||
import groupRouter from './groups.js';
|
||||
import {adminOnly, teachersOnly} from "../middleware/auth/checks/auth-checks";
|
||||
|
@ -12,14 +14,20 @@ import {onlyAllowIfHasAccessToAssignment} from "../middleware/auth/checks/assign
|
|||
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
router.get('/', getAllAssignmentsHandler);
|
||||
// Root endpoint used to search objects
|
||||
router.get('/', adminOnly, getAllAssignmentsHandler);
|
||||
|
||||
router.post('/', teachersOnly, onlyAllowOwnClassInBody, createAssignmentHandler);
|
||||
|
||||
router.get('/:id', getAssignmentHandler);
|
||||
// Information about an assignment with id 'id'
|
||||
router.get('/:id', onlyAllowIfHasAccessToAssignment, getAssignmentHandler);
|
||||
|
||||
router.put('/:id', putAssignmentHandler);
|
||||
|
||||
router.delete('/:id', deleteAssignmentHandler);
|
||||
|
||||
router.get('/:id/submissions', onlyAllowIfHasAccessToAssignment, getAssignmentsSubmissionsHandler);
|
||||
|
||||
router.get('/:id/questions', onlyAllowIfHasAccessToAssignment, (_req, res) => {
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
import express from 'express';
|
||||
import {
|
||||
addClassStudentHandler,
|
||||
addClassTeacherHandler,
|
||||
createClassHandler,
|
||||
deleteClassHandler,
|
||||
deleteClassStudentHandler,
|
||||
deleteClassTeacherHandler,
|
||||
getAllClassesHandler,
|
||||
getClassHandler,
|
||||
getClassStudentsHandler,
|
||||
getClassTeachersHandler,
|
||||
getTeacherInvitationsHandler,
|
||||
putClassHandler,
|
||||
} from '../controllers/classes.js';
|
||||
import assignmentRouter from './assignments.js';
|
||||
import {adminOnly, teachersOnly} from "../middleware/auth/checks/auth-checks";
|
||||
|
@ -20,10 +27,24 @@ router.post('/', teachersOnly, createClassHandler);
|
|||
// Information about an class with id 'id'
|
||||
router.get('/:id', onlyAllowIfInClass, getClassHandler);
|
||||
|
||||
router.put('/:id', putClassHandler);
|
||||
|
||||
router.delete('/:id', deleteClassHandler);
|
||||
|
||||
router.get('/:id/teacher-invitations', teachersOnly, onlyAllowIfInClass, getTeacherInvitationsHandler);
|
||||
|
||||
router.get('/:id/students', onlyAllowIfInClass, getClassStudentsHandler);
|
||||
|
||||
router.post('/:id/students', addClassStudentHandler);
|
||||
|
||||
router.delete('/:id/students/:username', deleteClassStudentHandler);
|
||||
|
||||
router.get('/:id/teachers', getClassTeachersHandler);
|
||||
|
||||
router.post('/:id/teachers', addClassTeacherHandler);
|
||||
|
||||
router.delete('/:id/teachers/:username', deleteClassTeacherHandler);
|
||||
|
||||
router.use('/:classid/assignments', assignmentRouter);
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
import express from 'express';
|
||||
import { createGroupHandler, getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../controllers/groups.js';
|
||||
import {
|
||||
createGroupHandler,
|
||||
deleteGroupHandler,
|
||||
getAllGroupsHandler,
|
||||
getGroupHandler,
|
||||
getGroupSubmissionsHandler,
|
||||
putGroupHandler,
|
||||
} from '../controllers/groups.js';
|
||||
import {onlyAllowIfHasAccessToGroup} from "../middleware/auth/checks/group-auth-checker";
|
||||
import {teachersOnly} from "../middleware/auth/checks/auth-checks";
|
||||
import {onlyAllowIfHasAccessToAssignment} from "../middleware/auth/checks/assignment-auth-checks";
|
||||
|
@ -14,6 +21,10 @@ router.post('/', teachersOnly, onlyAllowIfHasAccessToAssignment, createGroupHand
|
|||
// Information about a group (members, ... [TODO DOC])
|
||||
router.get('/:groupid', onlyAllowIfHasAccessToGroup, getGroupHandler);
|
||||
|
||||
router.put('/:groupid', putGroupHandler);
|
||||
|
||||
router.delete('/:groupid', deleteGroupHandler);
|
||||
|
||||
router.get('/:groupid/submissions', onlyAllowIfHasAccessToGroup, getGroupSubmissionsHandler);
|
||||
|
||||
// The list of questions a group has made
|
||||
|
|
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';
|
||||
|
||||
import {adminOnly} from "../middleware/auth/checks/auth-checks";
|
||||
import {onlyAllowUserHimself} from "../middleware/auth/checks/user-auth-checks";
|
||||
import {onlyAllowTeacherOfClass} from "../middleware/auth/checks/class-auth-checks";
|
||||
|
@ -35,10 +37,6 @@ router.get('/:username/joinRequests/:classId', onlyAllowTeacherOfClass, getStude
|
|||
router.put('/:username/joinRequests/:classId/:studentUsername', onlyAllowTeacherOfClass, 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;
|
||||
|
|
|
@ -1,18 +1,43 @@
|
|||
import { getAssignmentRepository, getClassRepository, getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
|
||||
import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js';
|
||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import {
|
||||
getAssignmentRepository,
|
||||
getClassRepository,
|
||||
getGroupRepository,
|
||||
getQuestionRepository,
|
||||
getSubmissionRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { Assignment } from '../entities/assignments/assignment.entity.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js';
|
||||
import { mapToQuestionDTO } from '../interfaces/question.js';
|
||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||
import { fetchClass } from './classes.js';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { putObject } from './service-helper.js';
|
||||
|
||||
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
|
||||
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
throw new NotFoundException("Could not find assignment's class");
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
throw new NotFoundException('Could not find assignment');
|
||||
}
|
||||
|
||||
return assignment;
|
||||
}
|
||||
|
||||
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
|
||||
const cls = await fetchClass(classid);
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
|
||||
|
||||
|
@ -23,42 +48,37 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
|
|||
return assignments.map(mapToAssignmentDTOId);
|
||||
}
|
||||
|
||||
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO> {
|
||||
const cls = await fetchClass(classid);
|
||||
|
||||
const assignment = mapToAssignment(assignmentData, cls);
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const newAssignment = assignmentRepository.create(assignment);
|
||||
await assignmentRepository.save(newAssignment, { preventOverwrite: true });
|
||||
|
||||
try {
|
||||
const newAssignment = assignmentRepository.create(assignment);
|
||||
await assignmentRepository.save(newAssignment);
|
||||
|
||||
return mapToAssignmentDTO(newAssignment);
|
||||
} catch (e) {
|
||||
getLogger().error(e);
|
||||
return null;
|
||||
}
|
||||
return mapToAssignmentDTO(newAssignment);
|
||||
}
|
||||
|
||||
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> {
|
||||
const assignment = await fetchAssignment(classid, id);
|
||||
return mapToAssignmentDTO(assignment);
|
||||
}
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
export async function putAssignment(classid: string, id: number, assignmentData: Partial<EntityDTO<Assignment>>): Promise<AssignmentDTO> {
|
||||
const assignment = await fetchAssignment(classid, id);
|
||||
|
||||
await putObject<Assignment>(assignment, assignmentData, getAssignmentRepository());
|
||||
|
||||
return mapToAssignmentDTO(assignment);
|
||||
}
|
||||
|
||||
export async function deleteAssignment(classid: string, id: number): Promise<AssignmentDTO> {
|
||||
const assignment = await fetchAssignment(classid, id);
|
||||
const cls = await fetchClass(classid);
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, id);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
await assignmentRepository.deleteByClassAndId(cls, id);
|
||||
|
||||
return mapToAssignmentDTO(assignment);
|
||||
}
|
||||
|
@ -68,19 +88,7 @@ export async function getAssignmentsSubmissions(
|
|||
assignmentNumber: number,
|
||||
full: boolean
|
||||
): Promise<SubmissionDTO[] | SubmissionDTOId[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return [];
|
||||
}
|
||||
const assignment = await fetchAssignment(classid, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const groups = await groupRepository.findAllGroupsForAssignment(assignment);
|
||||
|
@ -94,3 +102,16 @@ export async function getAssignmentsSubmissions(
|
|||
|
||||
return submissions.map(mapToSubmissionDTOId);
|
||||
}
|
||||
|
||||
export async function getAssignmentsQuestions(classid: string, assignmentNumber: number, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
|
||||
const assignment = await fetchAssignment(classid, assignmentNumber);
|
||||
|
||||
const questionRepository = getQuestionRepository();
|
||||
const questions = await questionRepository.findAllByAssignment(assignment);
|
||||
|
||||
if (full) {
|
||||
return questions.map(mapToQuestionDTO);
|
||||
}
|
||||
|
||||
return questions.map(mapToQuestionDTO);
|
||||
}
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js';
|
||||
import { getClassRepository, getTeacherInvitationRepository } from '../data/repositories.js';
|
||||
import { mapToClassDTO } from '../interfaces/class.js';
|
||||
import { mapToStudentDTO } from '../interfaces/student.js';
|
||||
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds } from '../interfaces/teacher-invitation.js';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
||||
import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import { fetchTeacher } from './teachers.js';
|
||||
import { fetchStudent } from './students.js';
|
||||
import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher';
|
||||
import { mapToTeacherDTO } from '../interfaces/teacher.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { putObject } from './service-helper.js';
|
||||
|
||||
const logger = getLogger();
|
||||
|
||||
export async function fetchClass(classId: string): Promise<Class> {
|
||||
export async function fetchClass(classid: string): Promise<Class> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
throw new NotFoundException('Class with id not found');
|
||||
throw new NotFoundException('Class not found');
|
||||
}
|
||||
|
||||
return cls;
|
||||
|
@ -24,11 +27,7 @@ export async function fetchClass(classId: string): Promise<Class> {
|
|||
|
||||
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.find({}, { populate: ['students', 'teachers'] });
|
||||
|
||||
if (!classes) {
|
||||
return [];
|
||||
}
|
||||
const classes = await classRepository.findAll({ populate: ['students', 'teachers'] });
|
||||
|
||||
if (full) {
|
||||
return classes.map(mapToClassDTO);
|
||||
|
@ -36,74 +35,71 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
|
|||
return classes.map((cls) => cls.classId!);
|
||||
}
|
||||
|
||||
export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> {
|
||||
const teacherRepository = getTeacherRepository();
|
||||
const teacherUsernames = classData.teachers || [];
|
||||
const teachers = (await Promise.all(teacherUsernames.map(async (id) => teacherRepository.findByUsername(id)))).filter(
|
||||
(teacher) => teacher !== null
|
||||
);
|
||||
|
||||
const studentRepository = getStudentRepository();
|
||||
const studentUsernames = classData.students || [];
|
||||
const students = (await Promise.all(studentUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter(
|
||||
(student) => student !== null
|
||||
);
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
|
||||
try {
|
||||
const newClass = classRepository.create({
|
||||
displayName: classData.displayName,
|
||||
teachers: teachers,
|
||||
students: students,
|
||||
});
|
||||
await classRepository.save(newClass);
|
||||
|
||||
return mapToClassDTO(newClass);
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
return null;
|
||||
}
|
||||
export async function getClass(classId: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function getClass(classId: string): Promise<ClassDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
|
||||
const teacherUsernames = classData.teachers || [];
|
||||
const teachers = await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id)));
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
const studentUsernames = classData.students || [];
|
||||
const students = await Promise.all(studentUsernames.map(async (id) => fetchStudent(id)));
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const newClass = classRepository.create({
|
||||
displayName: classData.displayName,
|
||||
teachers: teachers,
|
||||
students: students,
|
||||
});
|
||||
await classRepository.save(newClass, { preventOverwrite: true });
|
||||
|
||||
return mapToClassDTO(newClass);
|
||||
}
|
||||
|
||||
export async function putClass(classId: string, classData: Partial<EntityDTO<Class>>): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
await putObject<Class>(cls, classData, getClassRepository());
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
async function fetchClassStudents(classId: string): Promise<StudentDTO[]> {
|
||||
export async function deleteClass(classId: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
await classRepository.deleteById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
if (full) {
|
||||
return cls.students.map(mapToStudentDTO);
|
||||
}
|
||||
return cls.students.map((student) => student.username);
|
||||
}
|
||||
|
||||
export async function getClassStudentsDTO(classId: string): Promise<StudentDTO[]> {
|
||||
const cls = await fetchClass(classId);
|
||||
return cls.students.map(mapToStudentDTO);
|
||||
}
|
||||
|
||||
export async function getClassStudents(classId: string): Promise<StudentDTO[]> {
|
||||
return await fetchClassStudents(classId);
|
||||
}
|
||||
export async function getClassTeachers(classId: string, full: boolean): Promise<TeacherDTO[] | string[]> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
export async function getClassStudentsIds(classId: string): Promise<string[]> {
|
||||
const students: StudentDTO[] = await fetchClassStudents(classId);
|
||||
return students.map((student) => student.username);
|
||||
if (full) {
|
||||
return cls.teachers.map(mapToTeacherDTO);
|
||||
}
|
||||
return cls.teachers.map((student) => student.username);
|
||||
}
|
||||
|
||||
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls);
|
||||
|
@ -114,3 +110,41 @@ export async function getClassTeacherInvitations(classId: string, full: boolean)
|
|||
|
||||
return invitations.map(mapToTeacherInvitationDTOIds);
|
||||
}
|
||||
|
||||
export async function deleteClassStudent(classId: string, username: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const newStudents = { students: cls.students.filter((student) => student.username !== username) };
|
||||
await putObject<Class>(cls, newStudents, getClassRepository());
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function deleteClassTeacher(classId: string, username: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const newTeachers = { teachers: cls.teachers.filter((teacher) => teacher.username !== username) };
|
||||
await putObject<Class>(cls, newTeachers, getClassRepository());
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function addClassStudent(classId: string, username: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
const newStudent = await fetchStudent(username);
|
||||
|
||||
const newStudents = { students: [...cls.students, newStudent] };
|
||||
await putObject<Class>(cls, newStudents, getClassRepository());
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function addClassTeacher(classId: string, username: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
const newTeacher = await fetchTeacher(username);
|
||||
|
||||
const newTeachers = { teachers: [...cls.teachers, newTeacher] };
|
||||
await putObject<Class>(cls, newTeachers, getClassRepository());
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
|
|
@ -1,105 +1,90 @@
|
|||
import {
|
||||
getAssignmentRepository,
|
||||
getClassRepository,
|
||||
getGroupRepository,
|
||||
getStudentRepository,
|
||||
getSubmissionRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js';
|
||||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
import { mapToGroupDTO, mapToShallowGroupDTO } from '../interfaces/group.js';
|
||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||
import { GroupDTO } from '@dwengo-1/common/interfaces/group';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
import { fetchAssignment } from './assignments.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { putObject } from './service-helper.js';
|
||||
|
||||
export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number, full: boolean): Promise<GroupDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> {
|
||||
const assignment = await fetchAssignment(classId, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
|
||||
if (!group) {
|
||||
return null;
|
||||
throw new NotFoundException('Could not find group');
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
return mapToShallowGroupDTO(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<Group | null> {
|
||||
export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<GroupDTO> {
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
export async function putGroup(
|
||||
classId: string,
|
||||
assignmentNumber: number,
|
||||
groupNumber: number,
|
||||
groupData: Partial<EntityDTO<Group>>
|
||||
): Promise<GroupDTO> {
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
|
||||
await putObject<Group>(group, groupData, getGroupRepository());
|
||||
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
export async function deleteGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<GroupDTO> {
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
const assignment = await fetchAssignment(classId, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
await groupRepository.deleteByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
export async function getExistingGroupFromGroupDTO(groupData: GroupDTO): Promise<Group> {
|
||||
const classId = typeof groupData.class === 'string' ? groupData.class : groupData.class.id;
|
||||
const assignmentNumber = typeof groupData.assignment === 'number' ? groupData.assignment : groupData.assignment.id;
|
||||
const groupNumber = groupData.groupNumber;
|
||||
|
||||
return await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
}
|
||||
|
||||
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<GroupDTO> {
|
||||
const studentRepository = getStudentRepository();
|
||||
|
||||
const memberUsernames = (groupData.members as string[]) || []; // TODO check if groupdata.members is a list
|
||||
const memberUsernames = (groupData.members as string[]) || [];
|
||||
const members = (await Promise.all([...memberUsernames].map(async (id) => studentRepository.findByUsername(id)))).filter(
|
||||
(student) => student !== null
|
||||
);
|
||||
|
||||
getLogger().debug(members);
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
const assignment = await fetchAssignment(classid, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
try {
|
||||
const newGroup = groupRepository.create({
|
||||
assignment: assignment,
|
||||
members: members,
|
||||
});
|
||||
await groupRepository.save(newGroup);
|
||||
const newGroup = groupRepository.create({
|
||||
assignment: assignment,
|
||||
members: members,
|
||||
});
|
||||
await groupRepository.save(newGroup);
|
||||
|
||||
return newGroup;
|
||||
} catch (e) {
|
||||
getLogger().error(e);
|
||||
return null;
|
||||
}
|
||||
return mapToGroupDTO(newGroup);
|
||||
}
|
||||
|
||||
export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return [];
|
||||
}
|
||||
const assignment = await fetchAssignment(classId, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const groups = await groupRepository.findAllGroupsForAssignment(assignment);
|
||||
|
||||
if (full) {
|
||||
getLogger().debug({ full: full, groups: groups });
|
||||
return groups.map(mapToGroupDTO);
|
||||
}
|
||||
|
||||
|
@ -112,26 +97,7 @@ export async function getGroupSubmissions(
|
|||
groupNumber: number,
|
||||
full: boolean
|
||||
): Promise<SubmissionDTO[] | SubmissionDTOId[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
|
||||
if (!group) {
|
||||
return [];
|
||||
}
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submissions = await submissionRepository.findAllSubmissionsForGroup(group);
|
||||
|
|
|
@ -1,23 +1,17 @@
|
|||
import {
|
||||
getAnswerRepository, getAssignmentRepository,
|
||||
getClassRepository,
|
||||
getGroupRepository,
|
||||
getQuestionRepository
|
||||
} from '../data/repositories.js';
|
||||
import {mapToLearningObjectID, 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';
|
||||
import { QuestionRepository } from '../data/questions/question-repository.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { mapToStudent } from '../interfaces/student.js';
|
||||
import {QuestionData, QuestionDTO, QuestionId} from '@dwengo-1/common/interfaces/question';
|
||||
import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
|
||||
import {fetchStudent} from "./students";
|
||||
import {mapToAssignment} from "../interfaces/assignment";
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import {AssignmentDTO} from "@dwengo-1/common/interfaces/assignment";
|
||||
import {FALLBACK_VERSION_NUM} from "../config";
|
||||
import { mapToAssignment } from '../interfaces/assignment.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { fetchStudent } from './students.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception';
|
||||
import { FALLBACK_VERSION_NUM } from '../config.js';
|
||||
|
||||
export async function getQuestionsAboutLearningObjectInAssignment(
|
||||
loId: LearningObjectIdentifier,
|
||||
|
@ -92,14 +86,14 @@ export async function createQuestion(loId: LearningObjectIdentifier, questionDat
|
|||
const author = await fetchStudent(questionData.author!);
|
||||
const content = questionData.content;
|
||||
|
||||
const clazz = await getClassRepository().findById((questionData.inGroup.assignment as AssignmentDTO).class);
|
||||
const clazz = await getClassRepository().findById((questionData.inGroup.assignment as AssignmentDTO).within);
|
||||
const assignment = mapToAssignment(questionData.inGroup.assignment as AssignmentDTO, clazz!);
|
||||
const inGroup = (await getGroupRepository().findByAssignmentAndGroupNumber(assignment, questionData.inGroup.groupNumber))!;
|
||||
const inGroup = await getGroupRepository().findByAssignmentAndGroupNumber(assignment, questionData.inGroup.groupNumber);
|
||||
|
||||
const question = await questionRepository.createQuestion({
|
||||
loId,
|
||||
inGroup,
|
||||
author,
|
||||
inGroup: inGroup!,
|
||||
content,
|
||||
});
|
||||
|
||||
|
|
20
backend/src/services/service-helper.ts
Normal file
20
backend/src/services/service-helper.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { EntityDTO, FromEntityType } from '@mikro-orm/core';
|
||||
import { DwengoEntityRepository } from '../data/dwengo-entity-repository';
|
||||
|
||||
/**
|
||||
* Utility function to perform an PUT on an object.
|
||||
*
|
||||
* @param object The object that needs to be changed
|
||||
* @param data The datafields and their values that will be updated
|
||||
* @param repo The repository on which this action needs to be performed
|
||||
*
|
||||
* @returns Nothing.
|
||||
*/
|
||||
export async function putObject<T extends object>(
|
||||
object: T,
|
||||
data: Partial<EntityDTO<FromEntityType<T>>>,
|
||||
repo: DwengoEntityRepository<T>
|
||||
): Promise<void> {
|
||||
repo.assign(object, data);
|
||||
await repo.getEntityManager().flush();
|
||||
}
|
|
@ -23,6 +23,7 @@ import { GroupDTO } from '@dwengo-1/common/interfaces/group';
|
|||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request';
|
||||
import { ConflictException } from '../exceptions/conflict-exception.js';
|
||||
import { Submission } from '../entities/assignments/submission.entity';
|
||||
|
||||
export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> {
|
||||
|
@ -137,6 +138,10 @@ export async function createClassJoinRequest(username: string, classId: string):
|
|||
const student = await fetchStudent(username); // Throws error if student not found
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
if (cls.students.contains(student)) {
|
||||
throw new ConflictException('Student already in this class');
|
||||
}
|
||||
|
||||
const request = mapToStudentRequest(student, cls);
|
||||
await requestRepo.save(request, { preventOverwrite: true });
|
||||
return mapToStudentRequestDTO(request);
|
||||
|
|
|
@ -1,61 +1,56 @@
|
|||
import { getAssignmentRepository, 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';
|
||||
import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission';
|
||||
import { fetchStudent } from './students.js';
|
||||
import { getExistingGroupFromGroupDTO } from './groups.js';
|
||||
import { Submission } from '../entities/assignments/submission.entity.js';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
|
||||
export async function getSubmission(
|
||||
learningObjectHruid: string,
|
||||
language: Language,
|
||||
version: number,
|
||||
submissionNumber: number
|
||||
): Promise<SubmissionDTO | null> {
|
||||
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
|
||||
|
||||
export async function fetchSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<Submission> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
|
||||
|
||||
if (!submission) {
|
||||
return null;
|
||||
throw new NotFoundException('Could not find submission');
|
||||
}
|
||||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO | null> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = mapToSubmission(submissionDTO);
|
||||
|
||||
try {
|
||||
const newSubmission = submissionRepository.create(submission);
|
||||
await submissionRepository.save(newSubmission);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
export async function deleteSubmission(
|
||||
learningObjectHruid: string,
|
||||
language: Language,
|
||||
version: number,
|
||||
submissionNumber: number
|
||||
): Promise<SubmissionDTO | null> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
|
||||
const submission = getSubmission(learningObjectHruid, language, version, submissionNumber);
|
||||
|
||||
if (!submission) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
|
||||
await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
|
||||
|
||||
return submission;
|
||||
}
|
||||
|
||||
export async function getSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<SubmissionDTO> {
|
||||
const submission = await fetchSubmission(loId, submissionNumber);
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
export async function getAllSubmissions(loId: LearningObjectIdentifier): Promise<SubmissionDTO[]> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submissions = await submissionRepository.findByLearningObject(loId);
|
||||
|
||||
return submissions.map(mapToSubmissionDTO);
|
||||
}
|
||||
|
||||
export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO> {
|
||||
const submitter = await fetchStudent(submissionDTO.submitter.username);
|
||||
const group = await getExistingGroupFromGroupDTO(submissionDTO.group);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = mapToSubmission(submissionDTO, submitter, group);
|
||||
await submissionRepository.save(submission);
|
||||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
export async function deleteSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<SubmissionDTO> {
|
||||
const submission = await fetchSubmission(loId, submissionNumber);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
|
||||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the submissions made by on behalf of any group the given student is in.
|
||||
*/
|
||||
|
|
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);
|
||||
}
|
|
@ -22,13 +22,14 @@ import { Question } from '../entities/questions/question.entity.js';
|
|||
import { ClassJoinRequestRepository } from '../data/classes/class-join-request-repository.js';
|
||||
import { Student } from '../entities/users/student.entity.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { getClassStudents } from './classes.js';
|
||||
import { addClassStudent, fetchClass, getClassStudentsDTO } from './classes.js';
|
||||
import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher';
|
||||
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[]> {
|
||||
const teacherRepository: TeacherRepository = getTeacherRepository();
|
||||
|
@ -99,10 +100,12 @@ export async function getStudentsByTeacher(username: string, full: boolean): Pro
|
|||
|
||||
const classIds: string[] = classes.map((cls) => cls.id);
|
||||
|
||||
const students: StudentDTO[] = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat();
|
||||
const students: StudentDTO[] = (await Promise.all(classIds.map(async (username) => await getClassStudentsDTO(username)))).flat();
|
||||
|
||||
if (full) {
|
||||
return students;
|
||||
}
|
||||
|
||||
return students.map((student) => student.username);
|
||||
}
|
||||
|
||||
|
@ -143,13 +146,12 @@ export async function getJoinRequestsByClass(classId: string): Promise<ClassJoin
|
|||
|
||||
export async function updateClassJoinRequestStatus(studentUsername: string, classId: string, accepted = true): Promise<ClassJoinRequestDTO> {
|
||||
const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository();
|
||||
const classRepo: ClassRepository = getClassRepository();
|
||||
|
||||
const student: Student = await fetchStudent(studentUsername);
|
||||
const cls: Class | null = await classRepo.findById(classId);
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
if (!cls) {
|
||||
throw new NotFoundException('Class not found');
|
||||
if (cls.students.contains(student)) {
|
||||
throw new ConflictException('Student already in this class');
|
||||
}
|
||||
|
||||
const request: ClassJoinRequest | null = await requestRepo.findByStudentAndClass(student, cls);
|
||||
|
@ -158,8 +160,14 @@ export async function updateClassJoinRequestStatus(studentUsername: string, clas
|
|||
throw new NotFoundException('Join request not found');
|
||||
}
|
||||
|
||||
request.status = accepted ? ClassJoinRequestStatus.Accepted : ClassJoinRequestStatus.Declined;
|
||||
request.status = ClassStatus.Declined;
|
||||
|
||||
if (accepted) {
|
||||
request.status = ClassStatus.Accepted;
|
||||
await addClassStudent(classId, studentUsername);
|
||||
}
|
||||
|
||||
await requestRepo.save(request);
|
||||
|
||||
return mapToStudentRequestDTO(request);
|
||||
}
|
||||
|
|
|
@ -198,15 +198,34 @@ describe('Student controllers', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('Create join request', async () => {
|
||||
it('Create and delete join request', async () => {
|
||||
req = {
|
||||
params: { username: 'Noordkaap' },
|
||||
params: { username: 'TheDoors' },
|
||||
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
await createStudentRequestHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
|
||||
|
||||
req = {
|
||||
params: { username: 'TheDoors', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
await deleteClassJoinRequestHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
|
||||
|
||||
await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('Create join request student already in class error', async () => {
|
||||
req = {
|
||||
params: { username: 'Noordkaap' },
|
||||
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);
|
||||
});
|
||||
|
||||
it('Create join request duplicate', async () => {
|
||||
|
@ -217,16 +236,4 @@ describe('Student controllers', () => {
|
|||
|
||||
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);
|
||||
});
|
||||
|
||||
it('Delete join request', async () => {
|
||||
req = {
|
||||
params: { username: 'Noordkaap', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
await deleteClassJoinRequestHandler(req as Request, res as Response);
|
||||
|
||||
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
|
||||
|
||||
await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
|
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');
|
||||
});
|
||||
});
|
|
@ -16,6 +16,7 @@ import { BadRequestException } from '../../src/exceptions/bad-request-exception.
|
|||
import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js';
|
||||
import { getStudentRequestsHandler } from '../../src/controllers/students.js';
|
||||
import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher';
|
||||
import { getClassHandler } from '../../src/controllers/classes';
|
||||
|
||||
describe('Teacher controllers', () => {
|
||||
let req: Partial<Request>;
|
||||
|
@ -168,7 +169,6 @@ describe('Teacher controllers', () => {
|
|||
|
||||
it('Get join requests by class', async () => {
|
||||
req = {
|
||||
query: { username: 'LimpBizkit' },
|
||||
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
|
@ -183,8 +183,7 @@ describe('Teacher controllers', () => {
|
|||
|
||||
it('Update join request status', async () => {
|
||||
req = {
|
||||
query: { username: 'LimpBizkit', studentUsername: 'PinkFloyd' },
|
||||
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', studentUsername: 'PinkFloyd' },
|
||||
body: { accepted: 'true' },
|
||||
};
|
||||
|
||||
|
@ -200,5 +199,13 @@ describe('Teacher controllers', () => {
|
|||
|
||||
const status: boolean = jsonMock.mock.lastCall?.[0].requests[0].status;
|
||||
expect(status).toBeTruthy();
|
||||
|
||||
req = {
|
||||
params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
|
||||
};
|
||||
|
||||
await getClassHandler(req as Request, res as Response);
|
||||
const students: string[] = jsonMock.mock.lastCall?.[0].class.students;
|
||||
expect(students).contains('PinkFloyd');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -32,7 +32,7 @@ describe('AssignmentRepository', () => {
|
|||
});
|
||||
|
||||
it('should find all by username of the responsible teacher', async () => {
|
||||
const result = await assignmentRepository.findAllByResponsibleTeacher('FooFighters');
|
||||
const result = await assignmentRepository.findAllByResponsibleTeacher('testleerkracht1');
|
||||
const resultIds = result.map((it) => it.id).sort((a, b) => (a ?? 0) - (b ?? 0));
|
||||
|
||||
expect(resultIds).toEqual([1, 3, 4]);
|
||||
|
|
|
@ -66,7 +66,7 @@ describe('SubmissionRepository', () => {
|
|||
let assignment: Assignment | null;
|
||||
let loId: LearningObjectIdentifier;
|
||||
it('should find all submissions for a certain learning object and assignment', async () => {
|
||||
clazz = await classRepository.findById('id01');
|
||||
clazz = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
|
||||
assignment = await assignmentRepository.findByClassAndId(clazz!, 1);
|
||||
loId = {
|
||||
hruid: 'id02',
|
||||
|
|
|
@ -37,7 +37,7 @@ describe('QuestionRepository', () => {
|
|||
const id = new LearningObjectIdentifier('id03', Language.English, 1);
|
||||
const student = await studentRepository.findByUsername('Noordkaap');
|
||||
|
||||
const clazz = await getClassRepository().findById('id01');
|
||||
const clazz = await getClassRepository().findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
|
||||
const assignment = await getAssignmentRepository().findByClassAndId(clazz!, 1);
|
||||
const group = await getGroupRepository().findByAssignmentAndGroupNumber(assignment!, 1);
|
||||
await questionRepository.createQuestion({
|
||||
|
@ -56,7 +56,7 @@ describe('QuestionRepository', () => {
|
|||
let assignment: Assignment | null;
|
||||
let loId: LearningObjectIdentifier;
|
||||
it('should find all questions for a certain learning object and assignment', async () => {
|
||||
clazz = await getClassRepository().findById('id01');
|
||||
clazz = await getClassRepository().findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
|
||||
assignment = await getAssignmentRepository().findByClassAndId(clazz!, 1);
|
||||
loId = {
|
||||
hruid: 'id05',
|
||||
|
|
|
@ -13,6 +13,7 @@ import { makeTestAttachments } from './test_assets/content/attachments.testdata.
|
|||
import { makeTestQuestions } from './test_assets/questions/questions.testdata.js';
|
||||
import { makeTestAnswers } from './test_assets/questions/answers.testdata.js';
|
||||
import { makeTestSubmissions } from './test_assets/assignments/submission.testdata.js';
|
||||
import { Collection } from '@mikro-orm/core';
|
||||
|
||||
export async function setupTestApp(): Promise<void> {
|
||||
dotenv.config({ path: '.env.test' });
|
||||
|
@ -28,8 +29,8 @@ export async function setupTestApp(): Promise<void> {
|
|||
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);
|
||||
assignments[0].groups = new Collection(groups.slice(0, 3));
|
||||
assignments[1].groups = new Collection(groups.slice(3, 4));
|
||||
|
||||
const teacherInvitations = makeTestTeacherInvitations(em, teachers, classes);
|
||||
const classJoinRequests = makeTestClassJoinRequests(em, students, classes);
|
||||
|
|
|
@ -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,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];
|
||||
|
|
|
@ -14,6 +14,8 @@ import { makeTestQuestions } from '../tests/test_assets/questions/questions.test
|
|||
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';
|
||||
import { Collection } from '@mikro-orm/core';
|
||||
import { Group } from '../dist/entities/assignments/group.entity.js';
|
||||
|
||||
const logger: Logger = getLogger();
|
||||
|
||||
|
@ -34,8 +36,8 @@ export async function seedDatabase(): Promise<void> {
|
|||
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);
|
||||
assignments[0].groups = new Collection<Group>(groups.slice(0, 3));
|
||||
assignments[1].groups = new Collection<Group>(groups.slice(3, 4));
|
||||
|
||||
const teacherInvitations = makeTestTeacherInvitations(em, teachers, classes);
|
||||
const classJoinRequests = makeTestClassJoinRequests(em, students, classes);
|
||||
|
@ -43,7 +45,7 @@ export async function seedDatabase(): Promise<void> {
|
|||
|
||||
learningObjects[1].attachments = attachments;
|
||||
|
||||
const questions = makeTestQuestions(em, students);
|
||||
const questions = makeTestQuestions(em, students, groups);
|
||||
const answers = makeTestAnswers(em, teachers, questions);
|
||||
const submissions = makeTestSubmissions(em, students, groups);
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import { GroupDTO } from './group';
|
|||
|
||||
export interface AssignmentDTO {
|
||||
id: number;
|
||||
class: string; // Id of class 'within'
|
||||
within: string;
|
||||
title: string;
|
||||
description: string;
|
||||
learningPath: string;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { StudentDTO } from './student';
|
||||
import { ClassJoinRequestStatus } from '../util/class-join-request';
|
||||
import { ClassStatus } from '../util/class-join-request';
|
||||
|
||||
export interface ClassJoinRequestDTO {
|
||||
requester: StudentDTO;
|
||||
class: string;
|
||||
status: ClassJoinRequestStatus;
|
||||
status: ClassStatus;
|
||||
}
|
||||
|
|
|
@ -3,5 +3,4 @@ export interface ClassDTO {
|
|||
displayName: string;
|
||||
teachers: string[];
|
||||
students: string[];
|
||||
joinRequests: string[];
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { AssignmentDTO } from './assignment';
|
||||
import { ClassDTO } from './class';
|
||||
import { StudentDTO } from './student';
|
||||
|
||||
export interface GroupDTO {
|
||||
class: string | ClassDTO;
|
||||
assignment: number | AssignmentDTO;
|
||||
groupNumber: number;
|
||||
members?: string[] | StudentDTO[];
|
||||
|
|
|
@ -1,8 +1,16 @@
|
|||
import { UserDTO } from './user';
|
||||
import { ClassDTO } from './class';
|
||||
import { ClassStatus } from '../util/class-join-request';
|
||||
|
||||
export interface TeacherInvitationDTO {
|
||||
sender: string | UserDTO;
|
||||
receiver: string | UserDTO;
|
||||
class: string | ClassDTO;
|
||||
classId: string;
|
||||
status: ClassStatus;
|
||||
}
|
||||
|
||||
export interface TeacherInvitationData {
|
||||
sender: string;
|
||||
receiver: string;
|
||||
class: string;
|
||||
accepted?: boolean; // Use for put requests, else skip
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
export enum ClassJoinRequestStatus {
|
||||
export enum ClassStatus {
|
||||
Open = 'open',
|
||||
Accepted = 'accepted',
|
||||
Declined = 'declined',
|
||||
|
|
1
docs/.gitignore
vendored
Normal file
1
docs/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
api/swagger.json
|
|
@ -15,6 +15,10 @@ const doc = {
|
|||
url: 'http://localhost:3000/',
|
||||
description: 'Development server',
|
||||
},
|
||||
{
|
||||
url: 'http://localhost/',
|
||||
description: 'Staging server',
|
||||
},
|
||||
{
|
||||
url: 'https://sel2-1.ugent.be/',
|
||||
description: 'Production server',
|
||||
|
@ -55,4 +59,4 @@ const doc = {
|
|||
const outputFile = './swagger.json';
|
||||
const routes = ['../../backend/src/app.ts'];
|
||||
|
||||
await swaggerAutogen({ openapi: '3.1.0' })(outputFile, routes, doc);
|
||||
void swaggerAutogen({ openapi: '3.1.0' })(outputFile, routes, doc);
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -44,16 +44,16 @@ export default [
|
|||
// All @typescript-eslint configuration options are listed.
|
||||
// If the rules are commented, they are configured by the inherited configurations.
|
||||
|
||||
'@typescript-eslint/adjacent-overload-signatures': 'warn',
|
||||
'@typescript-eslint/array-type': 'warn',
|
||||
'@typescript-eslint/adjacent-overload-signatures': 'error',
|
||||
'@typescript-eslint/array-type': 'error',
|
||||
'@typescript-eslint/await-thenable': 'error',
|
||||
'@typescript-eslint/ban-ts-comment': ['error', { minimumDescriptionLength: 10 }],
|
||||
'@typescript-eslint/ban-tslint-comment': 'error',
|
||||
camelcase: 'off',
|
||||
'@typescript-eslint/class-literal-property-style': 'warn',
|
||||
'@typescript-eslint/class-literal-property-style': 'error',
|
||||
'class-methods-use-this': 'off',
|
||||
'@typescript-eslint/class-methods-use-this': ['error', { ignoreOverrideMethods: true }],
|
||||
'@typescript-eslint/consistent-generic-constructors': 'warn',
|
||||
'@typescript-eslint/consistent-generic-constructors': 'error',
|
||||
'@typescript-eslint/consistent-indexed-object-style': 'error',
|
||||
'consistent-return': 'off',
|
||||
'@typescript-eslint/consistent-return': 'off',
|
||||
|
@ -64,18 +64,18 @@ export default [
|
|||
'default-param-last': 'off',
|
||||
'@typescript-eslint/default-param-last': 'error',
|
||||
'dot-notation': 'off',
|
||||
'@typescript-eslint/dot-notation': 'warn',
|
||||
'@typescript-eslint/explicit-function-return-type': 'warn',
|
||||
'@typescript-eslint/dot-notation': 'error',
|
||||
'@typescript-eslint/explicit-function-return-type': 'error',
|
||||
'@typescript-eslint/explicit-member-accessibility': 'off',
|
||||
'@typescript-eslint/explicit-module-boundary-types': 'warn',
|
||||
'@typescript-eslint/explicit-module-boundary-types': 'error',
|
||||
'init-declarations': 'off',
|
||||
'@typescript-eslint/init-declarations': 'off',
|
||||
'max-params': 'off',
|
||||
'@typescript-eslint/max-params': ['error', { max: 6 }],
|
||||
'@typescript-eslint/member-ordering': 'warn',
|
||||
'@typescript-eslint/member-ordering': 'error',
|
||||
'@typescript-eslint/method-signature-style': 'off', // Don't care about TypeScript strict mode.
|
||||
'@typescript-eslint/naming-convention': [
|
||||
'warn',
|
||||
'error',
|
||||
{
|
||||
// Enforce that all variables, functions and properties are camelCase
|
||||
selector: 'variableLike',
|
||||
|
@ -113,7 +113,7 @@ export default [
|
|||
'@typescript-eslint/no-empty-function': 'error',
|
||||
'@typescript-eslint/no-empty-interface': 'off',
|
||||
'@typescript-eslint/no-empty-object-type': 'error',
|
||||
'@typescript-eslint/no-explicit-any': 'warn', // Once in production, this should be an error.
|
||||
'@typescript-eslint/no-explicit-any': 'error', // Once in production, this should be an error.
|
||||
'@typescript-eslint/no-extra-non-null-assertion': 'error',
|
||||
'@typescript-eslint/no-extraneous-class': 'error',
|
||||
'@typescript-eslint/no-floating-promises': 'error',
|
||||
|
@ -121,7 +121,7 @@ export default [
|
|||
'no-implied-eval': 'off',
|
||||
'@typescript-eslint/no-implied-eval': 'error',
|
||||
'@typescript-eslint/no-import-type-side-effects': 'error',
|
||||
'@typescript-eslint/no-inferrable-types': 'warn',
|
||||
'@typescript-eslint/no-inferrable-types': 'error',
|
||||
'no-invalid-this': 'off',
|
||||
'@typescript-eslint/no-invalid-this': 'off',
|
||||
'@typescript-eslint/no-invalid-void-type': 'error',
|
||||
|
@ -146,10 +146,10 @@ export default [
|
|||
'@typescript-eslint/no-unsafe-function-type': 'error',
|
||||
|
||||
'no-unused-expressions': 'off',
|
||||
'@typescript-eslint/no-unused-expressions': 'warn',
|
||||
'@typescript-eslint/no-unused-expressions': 'error',
|
||||
'no-unused-vars': 'off',
|
||||
'@typescript-eslint/no-unused-vars': [
|
||||
'warn',
|
||||
'error',
|
||||
{
|
||||
args: 'all',
|
||||
argsIgnorePattern: '^_',
|
||||
|
@ -164,53 +164,53 @@ export default [
|
|||
|
||||
'@typescript-eslint/parameter-properties': 'off',
|
||||
|
||||
'@typescript-eslint/prefer-find': 'warn',
|
||||
'@typescript-eslint/prefer-find': 'error',
|
||||
|
||||
'@typescript-eslint/prefer-function-type': 'error',
|
||||
|
||||
'@typescript-eslint/prefer-readonly-parameter-types': 'off',
|
||||
'@typescript-eslint/prefer-reduce-type-parameter': 'error',
|
||||
|
||||
'@typescript-eslint/promise-function-async': 'warn',
|
||||
'@typescript-eslint/promise-function-async': 'error',
|
||||
|
||||
'@typescript-eslint/require-array-sort-compare': 'warn',
|
||||
'@typescript-eslint/require-array-sort-compare': 'error',
|
||||
|
||||
'no-await-in-loop': 'warn',
|
||||
'no-await-in-loop': 'error',
|
||||
'no-constructor-return': 'error',
|
||||
'no-inner-declarations': 'error',
|
||||
'no-self-compare': 'error',
|
||||
'no-template-curly-in-string': 'error',
|
||||
'no-unmodified-loop-condition': 'warn',
|
||||
'no-unreachable-loop': 'warn',
|
||||
'no-unmodified-loop-condition': 'error',
|
||||
'no-unreachable-loop': 'error',
|
||||
'no-useless-assignment': 'error',
|
||||
|
||||
'arrow-body-style': ['warn', 'as-needed'],
|
||||
'block-scoped-var': 'warn',
|
||||
'capitalized-comments': 'warn',
|
||||
'arrow-body-style': ['error', 'as-needed'],
|
||||
'block-scoped-var': 'error',
|
||||
'capitalized-comments': 'error',
|
||||
'consistent-this': 'error',
|
||||
curly: 'error',
|
||||
'default-case': 'error',
|
||||
'default-case-last': 'error',
|
||||
eqeqeq: 'error',
|
||||
'func-names': 'warn',
|
||||
'func-style': ['warn', 'declaration'],
|
||||
'grouped-accessor-pairs': ['warn', 'getBeforeSet'],
|
||||
'guard-for-in': 'warn',
|
||||
'logical-assignment-operators': 'warn',
|
||||
'max-classes-per-file': 'warn',
|
||||
'func-names': 'error',
|
||||
'func-style': ['error', 'declaration'],
|
||||
'grouped-accessor-pairs': ['error', 'getBeforeSet'],
|
||||
'guard-for-in': 'error',
|
||||
'logical-assignment-operators': 'error',
|
||||
'max-classes-per-file': 'error',
|
||||
'no-alert': 'error',
|
||||
'no-bitwise': 'warn',
|
||||
'no-console': 'warn',
|
||||
'no-continue': 'warn',
|
||||
'no-else-return': 'warn',
|
||||
'no-bitwise': 'error',
|
||||
'no-console': 'error',
|
||||
'no-continue': 'error',
|
||||
'no-else-return': 'error',
|
||||
'no-eq-null': 'error',
|
||||
'no-eval': 'error',
|
||||
'no-extend-native': 'error',
|
||||
'no-extra-label': 'error',
|
||||
'no-implicit-coercion': 'warn',
|
||||
'no-implicit-coercion': 'error',
|
||||
'no-iterator': 'error',
|
||||
'no-label-var': 'warn',
|
||||
'no-labels': 'warn',
|
||||
'no-label-var': 'error',
|
||||
'no-labels': 'error',
|
||||
'no-multi-assign': 'error',
|
||||
'no-nested-ternary': 'error',
|
||||
'no-object-constructor': 'error',
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
"@tanstack/vue-query": "^5.69.0",
|
||||
"axios": "^1.8.2",
|
||||
"oidc-client-ts": "^3.1.0",
|
||||
"uuid": "^11.1.0",
|
||||
"vue": "^3.5.13",
|
||||
"vue-i18n": "^11.1.2",
|
||||
"vue-router": "^4.5.0",
|
||||
|
|
|
@ -33,6 +33,10 @@ export class AssignmentController extends BaseController {
|
|||
return this.delete<AssignmentResponse>(`/${num}`);
|
||||
}
|
||||
|
||||
async updateAssignment(num: number, data: Partial<AssignmentDTO>): Promise<AssignmentResponse> {
|
||||
return this.put<AssignmentResponse>(`/${num}`, data);
|
||||
}
|
||||
|
||||
async getSubmissions(assignmentNumber: number, full = true): Promise<SubmissionsResponse> {
|
||||
return this.get<SubmissionsResponse>(`/${assignmentNumber}/submissions`, { full });
|
||||
}
|
||||
|
|
|
@ -2,7 +2,8 @@ import { BaseController } from "./base-controller";
|
|||
import type { ClassDTO } from "@dwengo-1/common/interfaces/class";
|
||||
import type { StudentsResponse } from "./students";
|
||||
import type { AssignmentsResponse } from "./assignments";
|
||||
import type { TeacherInvitationDTO } from "@dwengo-1/common/interfaces/teacher-invitation";
|
||||
import type { TeachersResponse } from "@/controllers/teachers.ts";
|
||||
import type { TeacherInvitationsResponse } from "@/controllers/teacher-invitations.ts";
|
||||
|
||||
export interface ClassesResponse {
|
||||
classes: ClassDTO[] | string[];
|
||||
|
@ -12,14 +13,6 @@ export interface ClassResponse {
|
|||
class: ClassDTO;
|
||||
}
|
||||
|
||||
export interface TeacherInvitationsResponse {
|
||||
invites: TeacherInvitationDTO[];
|
||||
}
|
||||
|
||||
export interface TeacherInvitationResponse {
|
||||
invite: TeacherInvitationDTO;
|
||||
}
|
||||
|
||||
export class ClassController extends BaseController {
|
||||
constructor() {
|
||||
super("class");
|
||||
|
@ -41,10 +34,34 @@ export class ClassController extends BaseController {
|
|||
return this.delete<ClassResponse>(`/${id}`);
|
||||
}
|
||||
|
||||
async updateClass(id: string, data: Partial<ClassDTO>): Promise<ClassResponse> {
|
||||
return this.put<ClassResponse>(`/${id}`, data);
|
||||
}
|
||||
|
||||
async getStudents(id: string, full = true): Promise<StudentsResponse> {
|
||||
return this.get<StudentsResponse>(`/${id}/students`, { full });
|
||||
}
|
||||
|
||||
async addStudent(id: string, username: string): Promise<ClassResponse> {
|
||||
return this.post<ClassResponse>(`/${id}/students`, { username });
|
||||
}
|
||||
|
||||
async deleteStudent(id: string, username: string): Promise<ClassResponse> {
|
||||
return this.delete<ClassResponse>(`/${id}/students/${username}`);
|
||||
}
|
||||
|
||||
async getTeachers(id: string, full = true): Promise<TeachersResponse> {
|
||||
return this.get<TeachersResponse>(`/${id}/teachers`, { full });
|
||||
}
|
||||
|
||||
async addTeacher(id: string, username: string): Promise<ClassResponse> {
|
||||
return this.post<ClassResponse>(`/${id}/teachers`, { username });
|
||||
}
|
||||
|
||||
async deleteTeacher(id: string, username: string): Promise<ClassResponse> {
|
||||
return this.delete<ClassResponse>(`/${id}/teachers/${username}`);
|
||||
}
|
||||
|
||||
async getTeacherInvitations(id: string, full = true): Promise<TeacherInvitationsResponse> {
|
||||
return this.get<TeacherInvitationsResponse>(`/${id}/teacher-invitations`, { full });
|
||||
}
|
||||
|
|
|
@ -32,11 +32,15 @@ export class GroupController extends BaseController {
|
|||
return this.delete<GroupResponse>(`/${num}`);
|
||||
}
|
||||
|
||||
async getSubmissions(groupNumber: number, full = true): Promise<SubmissionsResponse> {
|
||||
return this.get<SubmissionsResponse>(`/${groupNumber}/submissions`, { full });
|
||||
async updateGroup(num: number, data: Partial<GroupDTO>): Promise<GroupResponse> {
|
||||
return this.put<GroupResponse>(`/${num}`, data);
|
||||
}
|
||||
|
||||
async getQuestions(groupNumber: number, full = true): Promise<QuestionsResponse> {
|
||||
return this.get<QuestionsResponse>(`/${groupNumber}/questions`, { full });
|
||||
async getSubmissions(num: number, full = true): Promise<SubmissionsResponse> {
|
||||
return this.get<SubmissionsResponse>(`/${num}/submissions`, { full });
|
||||
}
|
||||
|
||||
async getQuestions(num: number, full = true): Promise<QuestionsResponse> {
|
||||
return this.get<QuestionsResponse>(`/${num}/questions`, { full });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ export interface SubmissionResponse {
|
|||
|
||||
export class SubmissionController extends BaseController {
|
||||
constructor(classid: string, assignmentNumber: number, groupNumber: number) {
|
||||
super(`class/${classid}/assignments/${assignmentNumber}/groups/${groupNumber}`);
|
||||
super(`class/${classid}/assignments/${assignmentNumber}/groups/${groupNumber}/submissions`);
|
||||
}
|
||||
|
||||
async getAll(full = true): Promise<SubmissionsResponse> {
|
||||
|
@ -22,7 +22,7 @@ export class SubmissionController extends BaseController {
|
|||
return this.get<SubmissionResponse>(`/${submissionNumber}`);
|
||||
}
|
||||
|
||||
async createSubmission(data: unknown): Promise<SubmissionResponse> {
|
||||
async createSubmission(data: SubmissionDTO): Promise<SubmissionResponse> {
|
||||
return this.post<SubmissionResponse>(`/`, data);
|
||||
}
|
||||
|
||||
|
|
36
frontend/src/controllers/teacher-invitations.ts
Normal file
36
frontend/src/controllers/teacher-invitations.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { BaseController } from "@/controllers/base-controller.ts";
|
||||
import type { TeacherInvitationData, TeacherInvitationDTO } from "@dwengo-1/common/interfaces/teacher-invitation";
|
||||
|
||||
export interface TeacherInvitationsResponse {
|
||||
invitations: TeacherInvitationDTO[];
|
||||
}
|
||||
|
||||
export interface TeacherInvitationResponse {
|
||||
invitation: TeacherInvitationDTO;
|
||||
}
|
||||
|
||||
export class TeacherInvitationController extends BaseController {
|
||||
constructor() {
|
||||
super("teachers/invitations");
|
||||
}
|
||||
|
||||
async getAll(username: string, sent: boolean): Promise<TeacherInvitationsResponse> {
|
||||
return this.get<TeacherInvitationsResponse>(`/${username}`, { sent });
|
||||
}
|
||||
|
||||
async getBy(data: TeacherInvitationData): Promise<TeacherInvitationResponse> {
|
||||
return this.get<TeacherInvitationResponse>(`/${data.sender}/${data.receiver}/${data.class}`);
|
||||
}
|
||||
|
||||
async create(data: TeacherInvitationData): Promise<TeacherInvitationResponse> {
|
||||
return this.post<TeacherInvitationResponse>("/", data);
|
||||
}
|
||||
|
||||
async remove(data: TeacherInvitationData): Promise<TeacherInvitationResponse> {
|
||||
return this.delete<TeacherInvitationResponse>(`/${data.sender}/${data.receiver}/${data.class}`);
|
||||
}
|
||||
|
||||
async respond(data: TeacherInvitationData): Promise<TeacherInvitationResponse> {
|
||||
return this.put<TeacherInvitationResponse>("/", data);
|
||||
}
|
||||
}
|
188
frontend/src/queries/assignments.ts
Normal file
188
frontend/src/queries/assignments.ts
Normal file
|
@ -0,0 +1,188 @@
|
|||
import { AssignmentController, type AssignmentResponse, type AssignmentsResponse } from "@/controllers/assignments";
|
||||
import type { QuestionsResponse } from "@/controllers/questions";
|
||||
import type { SubmissionsResponse } from "@/controllers/submissions";
|
||||
import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
type UseMutationReturnType,
|
||||
type UseQueryReturnType,
|
||||
} from "@tanstack/vue-query";
|
||||
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
||||
import { groupsQueryKey, invalidateAllGroupKeys } from "./groups";
|
||||
import type { GroupsResponse } from "@/controllers/groups";
|
||||
import type { AssignmentDTO } from "@dwengo-1/common/interfaces/assignment";
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
import { invalidateAllSubmissionKeys } from "./submissions";
|
||||
|
||||
function assignmentsQueryKey(classid: string, full: boolean) {
|
||||
return ["assignments", classid, full];
|
||||
}
|
||||
function assignmentQueryKey(classid: string, assignmentNumber: number) {
|
||||
return ["assignment", classid, assignmentNumber];
|
||||
}
|
||||
function assignmentSubmissionsQueryKey(classid: string, assignmentNumber: number, full: boolean) {
|
||||
return ["assignment-submissions", classid, assignmentNumber, full];
|
||||
}
|
||||
function assignmentQuestionsQueryKey(classid: string, assignmentNumber: number, full: boolean) {
|
||||
return ["assignment-questions", classid, assignmentNumber, full];
|
||||
}
|
||||
|
||||
export async function invalidateAllAssignmentKeys(
|
||||
queryClient: QueryClient,
|
||||
classid?: string,
|
||||
assignmentNumber?: number,
|
||||
) {
|
||||
const keys = ["assignment", "assignment-submissions", "assignment-questions"];
|
||||
|
||||
for (const key of keys) {
|
||||
const queryKey = [key, classid, assignmentNumber].filter((arg) => arg !== undefined);
|
||||
await queryClient.invalidateQueries({ queryKey: queryKey });
|
||||
}
|
||||
|
||||
await queryClient.invalidateQueries({ queryKey: ["assignments", classid].filter((arg) => arg !== undefined) });
|
||||
}
|
||||
|
||||
function checkEnabled(
|
||||
classid: string | undefined,
|
||||
assignmentNumber: number | undefined,
|
||||
groupNumber: number | undefined,
|
||||
): boolean {
|
||||
return Boolean(classid) && !isNaN(Number(groupNumber)) && !isNaN(Number(assignmentNumber));
|
||||
}
|
||||
function toValues(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean>,
|
||||
) {
|
||||
return { cid: toValue(classid), an: toValue(assignmentNumber), gn: toValue(groupNumber), f: toValue(full) };
|
||||
}
|
||||
|
||||
export function useAssignmentsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<AssignmentsResponse, Error> {
|
||||
const { cid, f } = toValues(classid, 1, 1, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => assignmentsQueryKey(cid!, f)),
|
||||
queryFn: async () => new AssignmentController(cid!).getAll(f),
|
||||
enabled: () => checkEnabled(cid, 1, 1),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignmentQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
): UseQueryReturnType<AssignmentsResponse, Error> {
|
||||
const { cid, an } = toValues(classid, assignmentNumber, 1, true);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => assignmentQueryKey(cid!, an!)),
|
||||
queryFn: async () => new AssignmentController(cid!).getByNumber(an!),
|
||||
enabled: () => checkEnabled(cid, an, 1),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateAssignmentMutation(): UseMutationReturnType<
|
||||
AssignmentResponse,
|
||||
Error,
|
||||
{ cid: string; data: AssignmentDTO },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, data }) => new AssignmentController(cid).createAssignment(data),
|
||||
onSuccess: async (_) => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["assignments"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteAssignmentMutation(): UseMutationReturnType<
|
||||
AssignmentResponse,
|
||||
Error,
|
||||
{ cid: string; an: number },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an }) => new AssignmentController(cid).deleteAssignment(an),
|
||||
onSuccess: async (response) => {
|
||||
const cid = response.assignment.within;
|
||||
const an = response.assignment.id;
|
||||
|
||||
await invalidateAllAssignmentKeys(queryClient, cid, an);
|
||||
await invalidateAllGroupKeys(queryClient, cid, an);
|
||||
await invalidateAllSubmissionKeys(queryClient, cid, an);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateAssignmentMutation(): UseMutationReturnType<
|
||||
AssignmentResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; data: Partial<AssignmentDTO> },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, data }) => new AssignmentController(cid).updateAssignment(an, data),
|
||||
onSuccess: async (response) => {
|
||||
const cid = response.assignment.within;
|
||||
const an = response.assignment.id;
|
||||
|
||||
await invalidateAllGroupKeys(queryClient, cid, an);
|
||||
await queryClient.invalidateQueries({ queryKey: ["assignments"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignmentSubmissionsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<SubmissionsResponse, Error> {
|
||||
const { cid, an, gn, f } = toValues(classid, assignmentNumber, groupNumber, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => assignmentSubmissionsQueryKey(cid!, an!, f)),
|
||||
queryFn: async () => new AssignmentController(cid!).getSubmissions(gn!, f),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignmentQuestionsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<QuestionsResponse, Error> {
|
||||
const { cid, an, gn, f } = toValues(classid, assignmentNumber, groupNumber, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => assignmentQuestionsQueryKey(cid!, an!, f)),
|
||||
queryFn: async () => new AssignmentController(cid!).getQuestions(gn!, f),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignmentGroupsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<GroupsResponse, Error> {
|
||||
const { cid, an, gn, f } = toValues(classid, assignmentNumber, groupNumber, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => groupsQueryKey(cid!, an!, f)),
|
||||
queryFn: async () => new AssignmentController(cid!).getQuestions(gn!, f),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
224
frontend/src/queries/classes.ts
Normal file
224
frontend/src/queries/classes.ts
Normal file
|
@ -0,0 +1,224 @@
|
|||
import { ClassController, type ClassesResponse, type ClassResponse } from "@/controllers/classes";
|
||||
import type { StudentsResponse } from "@/controllers/students";
|
||||
import type { ClassDTO } from "@dwengo-1/common/interfaces/class";
|
||||
import {
|
||||
QueryClient,
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
type UseMutationReturnType,
|
||||
type UseQueryReturnType,
|
||||
} from "@tanstack/vue-query";
|
||||
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
||||
import { invalidateAllAssignmentKeys } from "./assignments";
|
||||
import { invalidateAllGroupKeys } from "./groups";
|
||||
import { invalidateAllSubmissionKeys } from "./submissions";
|
||||
|
||||
const classController = new ClassController();
|
||||
|
||||
/* Query cache keys */
|
||||
function classesQueryKey(full: boolean) {
|
||||
return ["classes", full];
|
||||
}
|
||||
function classQueryKey(classid: string) {
|
||||
return ["class", classid];
|
||||
}
|
||||
function classStudentsKey(classid: string, full: boolean) {
|
||||
return ["class-students", classid, full];
|
||||
}
|
||||
function classTeachersKey(classid: string, full: boolean) {
|
||||
return ["class-teachers", classid, full];
|
||||
}
|
||||
function classTeacherInvitationsKey(classid: string, full: boolean) {
|
||||
return ["class-teacher-invitations", classid, full];
|
||||
}
|
||||
function classAssignmentsKey(classid: string, full: boolean) {
|
||||
return ["class-assignments", classid, full];
|
||||
}
|
||||
|
||||
export async function invalidateAllClassKeys(queryClient: QueryClient, classid?: string) {
|
||||
const keys = ["class", "class-students", "class-teachers", "class-teacher-invitations", "class-assignments"];
|
||||
|
||||
for (const key of keys) {
|
||||
const queryKey = [key, classid].filter((arg) => arg !== undefined);
|
||||
await queryClient.invalidateQueries({ queryKey: queryKey });
|
||||
}
|
||||
|
||||
await queryClient.invalidateQueries({ queryKey: ["classes"] });
|
||||
}
|
||||
|
||||
/* Queries */
|
||||
export function useClassesQuery(full: MaybeRefOrGetter<boolean> = true): UseQueryReturnType<ClassesResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classesQueryKey(toValue(full))),
|
||||
queryFn: async () => classController.getAll(toValue(full)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassQuery(id: MaybeRefOrGetter<string | undefined>): UseQueryReturnType<ClassResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classQueryKey(toValue(id)!)),
|
||||
queryFn: async () => classController.getById(toValue(id)!),
|
||||
enabled: () => Boolean(toValue(id)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateClassMutation(): UseMutationReturnType<ClassResponse, Error, ClassDTO, unknown> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (data) => classController.createClass(data),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["classes"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteClassMutation(): UseMutationReturnType<ClassResponse, Error, string, unknown> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id) => classController.deleteClass(id),
|
||||
onSuccess: async (data) => {
|
||||
await invalidateAllClassKeys(queryClient, data.class.id);
|
||||
await invalidateAllAssignmentKeys(queryClient, data.class.id);
|
||||
await invalidateAllGroupKeys(queryClient, data.class.id);
|
||||
await invalidateAllSubmissionKeys(queryClient, data.class.id);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateClassMutation(): UseMutationReturnType<
|
||||
ClassResponse,
|
||||
Error,
|
||||
{ cid: string; data: Partial<ClassDTO> },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, data }) => classController.updateClass(cid, data),
|
||||
onSuccess: async (data) => {
|
||||
await invalidateAllClassKeys(queryClient, data.class.id);
|
||||
await invalidateAllAssignmentKeys(queryClient, data.class.id);
|
||||
await invalidateAllGroupKeys(queryClient, data.class.id);
|
||||
await invalidateAllSubmissionKeys(queryClient, data.class.id);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassStudentsQuery(
|
||||
id: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classStudentsKey(toValue(id)!, toValue(full))),
|
||||
queryFn: async () => classController.getStudents(toValue(id)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(id)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassAddStudentMutation(): UseMutationReturnType<
|
||||
ClassResponse,
|
||||
Error,
|
||||
{ id: string; username: string },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, username }) => classController.addStudent(id, username),
|
||||
onSuccess: async (data) => {
|
||||
await queryClient.invalidateQueries({ queryKey: classQueryKey(data.class.id) });
|
||||
await queryClient.invalidateQueries({ queryKey: classStudentsKey(data.class.id, true) });
|
||||
await queryClient.invalidateQueries({ queryKey: classStudentsKey(data.class.id, false) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassDeleteStudentMutation(): UseMutationReturnType<
|
||||
ClassResponse,
|
||||
Error,
|
||||
{ id: string; username: string },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, username }) => classController.deleteStudent(id, username),
|
||||
onSuccess: async (data) => {
|
||||
await queryClient.invalidateQueries({ queryKey: classQueryKey(data.class.id) });
|
||||
await queryClient.invalidateQueries({ queryKey: classStudentsKey(data.class.id, true) });
|
||||
await queryClient.invalidateQueries({ queryKey: classStudentsKey(data.class.id, false) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassTeachersQuery(
|
||||
id: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classTeachersKey(toValue(id)!, toValue(full))),
|
||||
queryFn: async () => classController.getTeachers(toValue(id)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(id)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassAddTeacherMutation(): UseMutationReturnType<
|
||||
ClassResponse,
|
||||
Error,
|
||||
{ id: string; username: string },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, username }) => classController.addTeacher(id, username),
|
||||
onSuccess: async (data) => {
|
||||
await queryClient.invalidateQueries({ queryKey: classQueryKey(data.class.id) });
|
||||
await queryClient.invalidateQueries({ queryKey: classTeachersKey(data.class.id, true) });
|
||||
await queryClient.invalidateQueries({ queryKey: classTeachersKey(data.class.id, false) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassDeleteTeacherMutation(): UseMutationReturnType<
|
||||
ClassResponse,
|
||||
Error,
|
||||
{ id: string; username: string },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, username }) => classController.deleteTeacher(id, username),
|
||||
onSuccess: async (data) => {
|
||||
await queryClient.invalidateQueries({ queryKey: classQueryKey(data.class.id) });
|
||||
await queryClient.invalidateQueries({ queryKey: classTeachersKey(data.class.id, true) });
|
||||
await queryClient.invalidateQueries({ queryKey: classTeachersKey(data.class.id, false) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassTeacherInvitationsQuery(
|
||||
id: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classTeacherInvitationsKey(toValue(id)!, toValue(full))),
|
||||
queryFn: async () => classController.getTeacherInvitations(toValue(id)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(id)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useClassAssignmentsQuery(
|
||||
id: MaybeRefOrGetter<string | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<StudentsResponse, Error> {
|
||||
return useQuery({
|
||||
queryKey: computed(() => classAssignmentsKey(toValue(id)!, toValue(full))),
|
||||
queryFn: async () => classController.getAssignments(toValue(id)!, toValue(full)),
|
||||
enabled: () => Boolean(toValue(id)),
|
||||
});
|
||||
}
|
191
frontend/src/queries/groups.ts
Normal file
191
frontend/src/queries/groups.ts
Normal file
|
@ -0,0 +1,191 @@
|
|||
import type { ClassesResponse } from "@/controllers/classes";
|
||||
import { GroupController, type GroupResponse, type GroupsResponse } from "@/controllers/groups";
|
||||
import type { QuestionsResponse } from "@/controllers/questions";
|
||||
import type { SubmissionsResponse } from "@/controllers/submissions";
|
||||
import type { GroupDTO } from "@dwengo-1/common/interfaces/group";
|
||||
import {
|
||||
QueryClient,
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
type UseMutationReturnType,
|
||||
type UseQueryReturnType,
|
||||
} from "@tanstack/vue-query";
|
||||
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
||||
import { invalidateAllAssignmentKeys } from "./assignments";
|
||||
import { invalidateAllSubmissionKeys } from "./submissions";
|
||||
|
||||
export function groupsQueryKey(classid: string, assignmentNumber: number, full: boolean) {
|
||||
return ["groups", classid, assignmentNumber, full];
|
||||
}
|
||||
function groupQueryKey(classid: string, assignmentNumber: number, groupNumber: number) {
|
||||
return ["group", classid, assignmentNumber, groupNumber];
|
||||
}
|
||||
function groupSubmissionsQueryKey(classid: string, assignmentNumber: number, groupNumber: number, full: boolean) {
|
||||
return ["group-submissions", classid, assignmentNumber, groupNumber, full];
|
||||
}
|
||||
function groupQuestionsQueryKey(classid: string, assignmentNumber: number, groupNumber: number, full: boolean) {
|
||||
return ["group-questions", classid, assignmentNumber, groupNumber, full];
|
||||
}
|
||||
|
||||
export async function invalidateAllGroupKeys(
|
||||
queryClient: QueryClient,
|
||||
classid?: string,
|
||||
assignmentNumber?: number,
|
||||
groupNumber?: number,
|
||||
) {
|
||||
const keys = ["group", "group-submissions", "group-questions"];
|
||||
|
||||
for (const key of keys) {
|
||||
const queryKey = [key, classid, assignmentNumber, groupNumber].filter((arg) => arg !== undefined);
|
||||
await queryClient.invalidateQueries({ queryKey: queryKey });
|
||||
}
|
||||
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ["groups", classid, assignmentNumber].filter((arg) => arg !== undefined),
|
||||
});
|
||||
}
|
||||
|
||||
function checkEnabled(
|
||||
classid: string | undefined,
|
||||
assignmentNumber: number | undefined,
|
||||
groupNumber: number | undefined,
|
||||
): boolean {
|
||||
return Boolean(classid) && !isNaN(Number(groupNumber)) && !isNaN(Number(assignmentNumber));
|
||||
}
|
||||
function toValues(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean>,
|
||||
) {
|
||||
return { cid: toValue(classid), an: toValue(assignmentNumber), gn: toValue(groupNumber), f: toValue(full) };
|
||||
}
|
||||
|
||||
export function useGroupsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<GroupsResponse, Error> {
|
||||
const { cid, an, f } = toValues(classid, assignmentNumber, 1, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => groupsQueryKey(cid!, an!, f)),
|
||||
queryFn: async () => new GroupController(cid!, an!).getAll(f),
|
||||
enabled: () => checkEnabled(cid, an, 1),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
): UseQueryReturnType<GroupResponse, Error> {
|
||||
const { cid, an, gn } = toValues(classid, assignmentNumber, groupNumber, true);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => groupQueryKey(cid!, an!, gn!)),
|
||||
queryFn: async () => new GroupController(cid!, an!).getByNumber(gn!),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateGroupMutation(): UseMutationReturnType<
|
||||
GroupResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; data: GroupDTO },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, data }) => new GroupController(cid, an).createGroup(data),
|
||||
onSuccess: async (response) => {
|
||||
const cid = typeof response.group.class === "string" ? response.group.class : response.group.class.id;
|
||||
const an =
|
||||
typeof response.group.assignment === "number"
|
||||
? response.group.assignment
|
||||
: response.group.assignment.id;
|
||||
|
||||
await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid, an, true) });
|
||||
await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid, an, false) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteGroupMutation(): UseMutationReturnType<
|
||||
GroupResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; gn: number },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, gn }) => new GroupController(cid, an).deleteGroup(gn),
|
||||
onSuccess: async (response) => {
|
||||
const cid = typeof response.group.class === "string" ? response.group.class : response.group.class.id;
|
||||
const an =
|
||||
typeof response.group.assignment === "number"
|
||||
? response.group.assignment
|
||||
: response.group.assignment.id;
|
||||
const gn = response.group.groupNumber;
|
||||
|
||||
await invalidateAllGroupKeys(queryClient, cid, an, gn);
|
||||
await invalidateAllSubmissionKeys(queryClient, cid, an, gn);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateGroupMutation(): UseMutationReturnType<
|
||||
GroupResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; gn: number; data: Partial<GroupDTO> },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, gn, data }) => new GroupController(cid, an).updateGroup(gn, data),
|
||||
onSuccess: async (response) => {
|
||||
const cid = typeof response.group.class === "string" ? response.group.class : response.group.class.id;
|
||||
const an =
|
||||
typeof response.group.assignment === "number"
|
||||
? response.group.assignment
|
||||
: response.group.assignment.id;
|
||||
const gn = response.group.groupNumber;
|
||||
|
||||
await invalidateAllGroupKeys(queryClient, cid, an, gn);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupSubmissionsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<SubmissionsResponse, Error> {
|
||||
const { cid, an, gn, f } = toValues(classid, assignmentNumber, groupNumber, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => groupSubmissionsQueryKey(cid!, an!, gn!, f)),
|
||||
queryFn: async () => new GroupController(cid!, an!).getSubmissions(gn!, f),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupQuestionsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<QuestionsResponse, Error> {
|
||||
const { cid, an, gn, f } = toValues(classid, assignmentNumber, groupNumber, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => groupQuestionsQueryKey(cid!, an!, gn!, f)),
|
||||
queryFn: async () => new GroupController(cid!, an!).getSubmissions(gn!, f),
|
||||
enabled: () => checkEnabled(cid, an, gn),
|
||||
});
|
||||
}
|
157
frontend/src/queries/submissions.ts
Normal file
157
frontend/src/queries/submissions.ts
Normal file
|
@ -0,0 +1,157 @@
|
|||
import { SubmissionController, type SubmissionResponse, type SubmissionsResponse } from "@/controllers/submissions";
|
||||
import type { SubmissionDTO } from "@dwengo-1/common/interfaces/submission";
|
||||
import {
|
||||
QueryClient,
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
type UseMutationReturnType,
|
||||
type UseQueryReturnType,
|
||||
} from "@tanstack/vue-query";
|
||||
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
||||
|
||||
function submissionsQueryKey(classid: string, assignmentNumber: number, groupNumber: number, full: boolean) {
|
||||
return ["submissions", classid, assignmentNumber, groupNumber, full];
|
||||
}
|
||||
function submissionQueryKey(classid: string, assignmentNumber: number, groupNumber: number, submissionNumber: number) {
|
||||
return ["submission", classid, assignmentNumber, groupNumber, submissionNumber];
|
||||
}
|
||||
|
||||
export async function invalidateAllSubmissionKeys(
|
||||
queryClient: QueryClient,
|
||||
classid?: string,
|
||||
assignmentNumber?: number,
|
||||
groupNumber?: number,
|
||||
submissionNumber?: number,
|
||||
) {
|
||||
const keys = ["submission"];
|
||||
|
||||
for (const key of keys) {
|
||||
const queryKey = [key, classid, assignmentNumber, groupNumber, submissionNumber].filter(
|
||||
(arg) => arg !== undefined,
|
||||
);
|
||||
await queryClient.invalidateQueries({ queryKey: queryKey });
|
||||
}
|
||||
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ["submissions", classid, assignmentNumber, groupNumber].filter((arg) => arg !== undefined),
|
||||
});
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ["group-submissions", classid, assignmentNumber, groupNumber].filter((arg) => arg !== undefined),
|
||||
});
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ["assignment-submissions", classid, assignmentNumber].filter((arg) => arg !== undefined),
|
||||
});
|
||||
}
|
||||
|
||||
function checkEnabled(
|
||||
classid: string | undefined,
|
||||
assignmentNumber: number | undefined,
|
||||
groupNumber: number | undefined,
|
||||
submissionNumber: number | undefined,
|
||||
): boolean {
|
||||
return (
|
||||
Boolean(classid) &&
|
||||
!isNaN(Number(groupNumber)) &&
|
||||
!isNaN(Number(assignmentNumber)) &&
|
||||
!isNaN(Number(submissionNumber))
|
||||
);
|
||||
}
|
||||
function toValues(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
submissionNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean>,
|
||||
) {
|
||||
return {
|
||||
cid: toValue(classid),
|
||||
an: toValue(assignmentNumber),
|
||||
gn: toValue(groupNumber),
|
||||
sn: toValue(submissionNumber),
|
||||
f: toValue(full),
|
||||
};
|
||||
}
|
||||
|
||||
export function useSubmissionsQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
full: MaybeRefOrGetter<boolean> = true,
|
||||
): UseQueryReturnType<SubmissionsResponse, Error> {
|
||||
const { cid, an, gn, sn, f } = toValues(classid, assignmentNumber, groupNumber, 1, full);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => submissionsQueryKey(cid!, an!, gn!, f)),
|
||||
queryFn: async () => new SubmissionController(cid!, an!, gn!).getAll(f),
|
||||
enabled: () => checkEnabled(cid, an, gn, sn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useSubmissionQuery(
|
||||
classid: MaybeRefOrGetter<string | undefined>,
|
||||
assignmentNumber: MaybeRefOrGetter<number | undefined>,
|
||||
groupNumber: MaybeRefOrGetter<number | undefined>,
|
||||
): UseQueryReturnType<SubmissionResponse, Error> {
|
||||
const { cid, an, gn, sn, f } = toValues(classid, assignmentNumber, groupNumber, 1, true);
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => submissionQueryKey(cid!, an!, gn!, sn!)),
|
||||
queryFn: async () => new SubmissionController(cid!, an!, gn!).getByNumber(sn!),
|
||||
enabled: () => checkEnabled(cid, an, gn, sn),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateSubmissionMutation(): UseMutationReturnType<
|
||||
SubmissionResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; gn: number; data: SubmissionDTO },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, gn, data }) => new SubmissionController(cid, an, gn).createSubmission(data),
|
||||
onSuccess: async (response) => {
|
||||
if (!response.submission.group) {
|
||||
await invalidateAllSubmissionKeys(queryClient);
|
||||
} else {
|
||||
const cls = response.submission.group.class;
|
||||
const assignment = response.submission.group.assignment;
|
||||
|
||||
const cid = typeof cls === "string" ? cls : cls.id;
|
||||
const an = typeof assignment === "number" ? assignment : assignment.id;
|
||||
const gn = response.submission.group.groupNumber;
|
||||
|
||||
await invalidateAllSubmissionKeys(queryClient, cid, an, gn);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteSubmissionMutation(): UseMutationReturnType<
|
||||
SubmissionResponse,
|
||||
Error,
|
||||
{ cid: string; an: number; gn: number; sn: number },
|
||||
unknown
|
||||
> {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ cid, an, gn, sn }) => new SubmissionController(cid, an, gn).deleteSubmission(sn),
|
||||
onSuccess: async (response) => {
|
||||
if (!response.submission.group) {
|
||||
await invalidateAllSubmissionKeys(queryClient);
|
||||
} else {
|
||||
const cls = response.submission.group.class;
|
||||
const assignment = response.submission.group.assignment;
|
||||
|
||||
const cid = typeof cls === "string" ? cls : cls.id;
|
||||
const an = typeof assignment === "number" ? assignment : assignment.id;
|
||||
const gn = response.submission.group.groupNumber;
|
||||
|
||||
await invalidateAllSubmissionKeys(queryClient, cid, an, gn);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
78
frontend/src/queries/teacher-invitations.ts
Normal file
78
frontend/src/queries/teacher-invitations.ts
Normal file
|
@ -0,0 +1,78 @@
|
|||
import { useMutation, useQuery, type UseMutationReturnType, type UseQueryReturnType } from "@tanstack/vue-query";
|
||||
import { computed, toValue } from "vue";
|
||||
import type { MaybeRefOrGetter } from "vue";
|
||||
import {
|
||||
TeacherInvitationController,
|
||||
type TeacherInvitationResponse,
|
||||
type TeacherInvitationsResponse,
|
||||
} from "@/controllers/teacher-invitations.ts";
|
||||
import type { TeacherInvitationData } from "@dwengo-1/common/interfaces/teacher-invitation";
|
||||
import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher";
|
||||
|
||||
const controller = new TeacherInvitationController();
|
||||
|
||||
/**
|
||||
All the invitations the teacher sent
|
||||
**/
|
||||
export function useTeacherInvitationsSentQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
): UseQueryReturnType<TeacherInvitationsResponse, Error> {
|
||||
return useQuery({
|
||||
queryFn: computed(async () => controller.getAll(toValue(username), true)),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
All the pending invitations sent to this teacher
|
||||
*/
|
||||
export function useTeacherInvitationsReceivedQuery(
|
||||
username: MaybeRefOrGetter<string | undefined>,
|
||||
): UseQueryReturnType<TeacherInvitationsResponse, Error> {
|
||||
return useQuery({
|
||||
queryFn: computed(async () => controller.getAll(toValue(username), false)),
|
||||
enabled: () => Boolean(toValue(username)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useTeacherInvitationQuery(
|
||||
data: MaybeRefOrGetter<TeacherInvitationData | undefined>,
|
||||
): UseQueryReturnType<TeacherInvitationResponse, Error> {
|
||||
return useQuery({
|
||||
queryFn: computed(async () => controller.getBy(toValue(data))),
|
||||
enabled: () => Boolean(toValue(data)),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateTeacherInvitationMutation(): UseMutationReturnType<
|
||||
TeacherInvitationResponse,
|
||||
Error,
|
||||
TeacherDTO,
|
||||
unknown
|
||||
> {
|
||||
return useMutation({
|
||||
mutationFn: async (data: TeacherInvitationData) => controller.create(data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRespondTeacherInvitationMutation(): UseMutationReturnType<
|
||||
TeacherInvitationResponse,
|
||||
Error,
|
||||
TeacherDTO,
|
||||
unknown
|
||||
> {
|
||||
return useMutation({
|
||||
mutationFn: async (data: TeacherInvitationData) => controller.respond(data),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteTeacherInvitationMutation(): UseMutationReturnType<
|
||||
TeacherInvitationResponse,
|
||||
Error,
|
||||
TeacherDTO,
|
||||
unknown
|
||||
> {
|
||||
return useMutation({
|
||||
mutationFn: async (data: TeacherInvitationData) => controller.remove(data),
|
||||
});
|
||||
}
|
|
@ -276,10 +276,11 @@
|
|||
<tbody>
|
||||
<tr
|
||||
v-for="i in invitations"
|
||||
:key="(i.class as ClassDTO).id"
|
||||
:key="i.classId"
|
||||
>
|
||||
<td>
|
||||
{{ (i.class as ClassDTO).displayName }}
|
||||
{{ i.classId }}
|
||||
<!-- TODO fetch display name via classId because db only returns classId field -->
|
||||
</td>
|
||||
<td>{{ (i.sender as TeacherDTO).firstName + " " + (i.sender as TeacherDTO).lastName }}</td>
|
||||
<td class="text-right">
|
||||
|
|
1
package-lock.json
generated
1
package-lock.json
generated
|
@ -273,6 +273,7 @@
|
|||
"@tanstack/vue-query": "^5.69.0",
|
||||
"axios": "^1.8.2",
|
||||
"oidc-client-ts": "^3.1.0",
|
||||
"uuid": "^11.1.0",
|
||||
"vue": "^3.5.13",
|
||||
"vue-i18n": "^11.1.2",
|
||||
"vue-router": "^4.5.0",
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"prebuild": "npm run clean",
|
||||
"prebuild": "npm run clean && npm run swagger --workspace=docs",
|
||||
"build": "tsc --build tsconfig.build.json",
|
||||
"clean": "tsc --build tsconfig.build.json --clean",
|
||||
"watch": "tsc --build tsconfig.build.json --watch",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue