merge: merge fix/183-post-assignment into dev
This commit is contained in:
commit
5a593cb88f
67 changed files with 2205 additions and 2129 deletions
|
@ -1,8 +1,15 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createQuestion, deleteQuestion, getAllQuestions, getQuestion, updateQuestion } from '../services/questions.js';
|
||||
import {
|
||||
createQuestion,
|
||||
deleteQuestion,
|
||||
getAllQuestions,
|
||||
getQuestion,
|
||||
getQuestionsAboutLearningObjectInAssignment,
|
||||
updateQuestion,
|
||||
} from '../services/questions.js';
|
||||
import { FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM } from '../config.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { QuestionData, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import { requireFields } from './error-helper.js';
|
||||
|
||||
|
@ -30,7 +37,18 @@ export async function getAllQuestionsHandler(req: Request, res: Response): Promi
|
|||
|
||||
const learningObjectId = getLearningObjectId(hruid, version, language);
|
||||
|
||||
const questions = await getAllQuestions(learningObjectId, full);
|
||||
let questions: QuestionDTO[] | QuestionId[];
|
||||
if (req.query.classId && req.query.assignmentId) {
|
||||
questions = await getQuestionsAboutLearningObjectInAssignment(
|
||||
learningObjectId,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string),
|
||||
full ?? false,
|
||||
req.query.forStudent as string | undefined
|
||||
);
|
||||
} else {
|
||||
questions = await getAllQuestions(learningObjectId, full ?? false);
|
||||
}
|
||||
|
||||
res.json({ questions });
|
||||
}
|
||||
|
@ -60,7 +78,8 @@ export async function createQuestionHandler(req: Request, res: Response): Promis
|
|||
|
||||
const author = req.body.author as string;
|
||||
const content = req.body.content as string;
|
||||
requireFields({ author, content });
|
||||
const inGroup = req.body.inGroup;
|
||||
requireFields({ author, content, inGroup });
|
||||
|
||||
const questionData = req.body as QuestionData;
|
||||
|
||||
|
|
|
@ -1,10 +1,32 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { createSubmission, deleteSubmission, getAllSubmissions, getSubmission } from '../services/submissions.js';
|
||||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { Language, languageMap } from '@dwengo-1/common/util/language';
|
||||
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 { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
|
||||
export async function getSubmissionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const loHruid = req.params.hruid;
|
||||
const lang = languageMap[req.query.language as string] || Language.Dutch;
|
||||
const version = parseInt(req.query.version as string) ?? 1;
|
||||
|
||||
const submissions = await getSubmissionsForLearningObjectAndAssignment(
|
||||
loHruid,
|
||||
lang,
|
||||
version,
|
||||
req.query.classId as string,
|
||||
parseInt(req.query.assignmentId as string)
|
||||
);
|
||||
|
||||
res.json(submissions);
|
||||
}
|
||||
|
||||
export async function getSubmissionHandler(req: Request, res: Response): Promise<void> {
|
||||
const lohruid = req.params.hruid;
|
||||
|
|
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.js';
|
||||
import { createInvitation, deleteInvitation, getAllInvitations, getInvitation, updateInvitation } from '../services/teacher-invitations.js';
|
||||
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 });
|
||||
}
|
|
@ -6,6 +6,22 @@ export class AssignmentRepository extends DwengoEntityRepository<Assignment> {
|
|||
public async findByClassAndId(within: Class, id: number): Promise<Assignment | null> {
|
||||
return this.findOne({ within: within, id: id }, { populate: [ "groups", "groups.members" ]});
|
||||
}
|
||||
public async findByClassIdAndAssignmentId(withinClass: string, id: number): Promise<Assignment | null> {
|
||||
return this.findOne({ within: { classId: withinClass }, id: id });
|
||||
}
|
||||
public async findAllByResponsibleTeacher(teacherUsername: string): Promise<Assignment[]> {
|
||||
return this.findAll({
|
||||
where: {
|
||||
within: {
|
||||
teachers: {
|
||||
$some: {
|
||||
username: teacherUsername,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
public async findAllAssignmentsInClass(within: Class): Promise<Assignment[]> {
|
||||
return this.findAll({ where: { within: within }, populate: [ "groups", "groups.members" ] });
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import { Group } from '../../entities/assignments/group.entity.js';
|
|||
import { Submission } from '../../entities/assignments/submission.entity.js';
|
||||
import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js';
|
||||
import { Student } from '../../entities/users/student.entity.js';
|
||||
import { Assignment } from '../../entities/assignments/assignment.entity';
|
||||
|
||||
export class SubmissionRepository extends DwengoEntityRepository<Submission> {
|
||||
public async findSubmissionByLearningObjectAndSubmissionNumber(
|
||||
|
@ -50,11 +51,58 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> {
|
|||
}
|
||||
|
||||
public async findAllSubmissionsForGroup(group: Group): Promise<Submission[]> {
|
||||
return this.find({ onBehalfOf: group });
|
||||
return this.find(
|
||||
{ onBehalfOf: group },
|
||||
{
|
||||
populate: ['onBehalfOf.members'],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up all submissions for the given learning object which were submitted as part of the given assignment.
|
||||
* When forStudentUsername is set, only the submissions of the given user's group are shown.
|
||||
*/
|
||||
public async findAllSubmissionsForLearningObjectAndAssignment(
|
||||
loId: LearningObjectIdentifier,
|
||||
assignment: Assignment,
|
||||
forStudentUsername?: string
|
||||
): Promise<Submission[]> {
|
||||
const onBehalfOf = forStudentUsername
|
||||
? {
|
||||
assignment,
|
||||
members: {
|
||||
$some: {
|
||||
username: forStudentUsername,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
assignment,
|
||||
};
|
||||
|
||||
return this.findAll({
|
||||
where: {
|
||||
learningObjectHruid: loId.hruid,
|
||||
learningObjectLanguage: loId.language,
|
||||
learningObjectVersion: loId.version,
|
||||
onBehalfOf,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public async findAllSubmissionsForStudent(student: Student): Promise<Submission[]> {
|
||||
return this.find({ submitter: student });
|
||||
const result = await this.find(
|
||||
{ submitter: student },
|
||||
{
|
||||
populate: ['onBehalfOf.members'],
|
||||
}
|
||||
);
|
||||
|
||||
// Workaround: For some reason, without this MikroORM generates an UPDATE query with a syntax error in some tests
|
||||
this.em.clear();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async deleteSubmissionByLearningObjectAndSubmissionNumber(loId: LearningObjectIdentifier, submissionNumber: number): Promise<void> {
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,15 +5,16 @@ import { Student } from '../../entities/users/student.entity.js';
|
|||
import { LearningObject } from '../../entities/content/learning-object.entity.js';
|
||||
import { Assignment } from '../../entities/assignments/assignment.entity.js';
|
||||
import { Loaded } from '@mikro-orm/core';
|
||||
import {Group} from "../../entities/assignments/group.entity";
|
||||
import { Group } from '../../entities/assignments/group.entity';
|
||||
|
||||
export class QuestionRepository extends DwengoEntityRepository<Question> {
|
||||
public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; content: string }): Promise<Question> {
|
||||
public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; inGroup: Group; content: string }): Promise<Question> {
|
||||
const questionEntity = this.create({
|
||||
learningObjectHruid: question.loId.hruid,
|
||||
learningObjectLanguage: question.loId.language,
|
||||
learningObjectVersion: question.loId.version,
|
||||
author: question.author,
|
||||
inGroup: question.inGroup,
|
||||
content: question.content,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
|
@ -21,6 +22,7 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
|
|||
questionEntity.learningObjectLanguage = question.loId.language;
|
||||
questionEntity.learningObjectVersion = question.loId.version;
|
||||
questionEntity.author = question.author;
|
||||
questionEntity.inGroup = question.inGroup;
|
||||
questionEntity.content = question.content;
|
||||
return this.insert(questionEntity);
|
||||
}
|
||||
|
@ -60,7 +62,9 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
|
|||
|
||||
public async findAllByAssignment(assignment: Assignment): Promise<Question[]> {
|
||||
return this.find({
|
||||
author: assignment.groups.toArray<Group>().flatMap((group) => group.members),
|
||||
inGroup: {
|
||||
$contained: assignment.groups,
|
||||
},
|
||||
learningObjectHruid: assignment.learningPathHruid,
|
||||
learningObjectLanguage: assignment.learningPathLanguage,
|
||||
});
|
||||
|
@ -73,6 +77,38 @@ export class QuestionRepository extends DwengoEntityRepository<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.
|
||||
*/
|
||||
public async findAllQuestionsAboutLearningObjectInAssignment(
|
||||
loId: LearningObjectIdentifier,
|
||||
assignment: Assignment,
|
||||
forStudentUsername?: string
|
||||
): Promise<Question[]> {
|
||||
const inGroup = forStudentUsername
|
||||
? {
|
||||
assignment,
|
||||
members: {
|
||||
$some: {
|
||||
username: forStudentUsername,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
assignment,
|
||||
};
|
||||
|
||||
return this.findAll({
|
||||
where: {
|
||||
learningObjectHruid: loId.hruid,
|
||||
learningObjectLanguage: loId.language,
|
||||
learningObjectVersion: loId.version,
|
||||
inGroup,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public async findByLearningObjectAndSequenceNumber(loId: LearningObjectIdentifier, sequenceNumber: number): Promise<Loaded<Question> | null> {
|
||||
return this.findOne({
|
||||
learningObjectHruid: loId.hruid,
|
||||
|
|
|
@ -21,6 +21,11 @@ export class Submission {
|
|||
@PrimaryKey({ type: 'integer', autoincrement: true })
|
||||
submissionNumber?: number;
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Group,
|
||||
})
|
||||
onBehalfOf!: Group;
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Student,
|
||||
})
|
||||
|
@ -29,12 +34,6 @@ export class Submission {
|
|||
@Property({ type: 'datetime' })
|
||||
submissionTime!: Date;
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Group,
|
||||
nullable: true,
|
||||
})
|
||||
onBehalfOf?: Group;
|
||||
|
||||
@Property({ type: 'json' })
|
||||
content!: string;
|
||||
}
|
||||
|
|
|
@ -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,6 +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.js';
|
||||
|
||||
@Entity({ repository: () => QuestionRepository })
|
||||
export class Question {
|
||||
|
@ -20,6 +21,9 @@ export class Question {
|
|||
@PrimaryKey({ type: 'integer', autoincrement: true })
|
||||
sequenceNumber?: number;
|
||||
|
||||
@ManyToOne({ entity: () => Group })
|
||||
inGroup!: Group;
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Student,
|
||||
})
|
||||
|
|
|
@ -1,17 +1,31 @@
|
|||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { mapToAssignment } from './assignment.js';
|
||||
import { mapToStudent } from './student.js';
|
||||
import { mapToAssignmentDTO } from './assignment.js';
|
||||
import { mapToStudentDTO } from './student.js';
|
||||
import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group';
|
||||
import { getGroupRepository } from '../data/repositories.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import { mapToClassDTO } from './class.js';
|
||||
|
||||
export function mapToGroupDTO(group: Group, cls: Class, options?: { expandStudents: boolean }): GroupDTO {
|
||||
export function mapToGroup(groupDto: GroupDTO, clazz: Class): Group {
|
||||
const assignmentDto = groupDto.assignment as AssignmentDTO;
|
||||
|
||||
return getGroupRepository().create({
|
||||
groupNumber: groupDto.groupNumber,
|
||||
assignment: mapToAssignment(assignmentDto, clazz),
|
||||
members: groupDto.members!.map((studentDto) => mapToStudent(studentDto as StudentDTO)),
|
||||
});
|
||||
}
|
||||
|
||||
export function mapToGroupDTO(group: Group, cls: Class): GroupDTO {
|
||||
return {
|
||||
class: cls.classId!,
|
||||
assignment: group.assignment.id!,
|
||||
groupNumber: group.groupNumber!,
|
||||
members:
|
||||
options?.expandStudents
|
||||
? group.members.map(mapToStudentDTO)
|
||||
: group.members.map((student) => student.username)
|
||||
members: group.members.map(mapToStudentDTO)
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -23,3 +37,15 @@ export function mapToGroupDTOId(group: Group, cls: Class): GroupDTOId {
|
|||
groupNumber: group.groupNumber!,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Map to group DTO where other objects are only referenced by their id.
|
||||
*/
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import { mapToStudentDTO } from './student.js';
|
|||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { mapToGroupDTOId } from './group.js';
|
||||
|
||||
function getLearningObjectIdentifier(question: Question): LearningObjectIdentifierDTO {
|
||||
return {
|
||||
|
@ -30,6 +31,7 @@ export function mapToQuestionDTO(question: Question): QuestionDTO {
|
|||
learningObjectIdentifier,
|
||||
sequenceNumber: question.sequenceNumber!,
|
||||
author: mapToStudentDTO(question.author),
|
||||
inGroup: mapToGroupDTOId(question.inGroup),
|
||||
timestamp: question.timestamp.toISOString(),
|
||||
content: question.content,
|
||||
};
|
||||
|
|
|
@ -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,10 +1,10 @@
|
|||
import { getSubmissionRepository } from '../data/repositories.js';
|
||||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
import { Submission } from '../entities/assignments/submission.entity.js';
|
||||
import { Student } from '../entities/users/student.entity.js';
|
||||
import { mapToGroupDTO } from './group.js';
|
||||
import { mapToStudentDTO } from './student.js';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { getSubmissionRepository } from '../data/repositories.js';
|
||||
import { Student } from '../entities/users/student.entity.js';
|
||||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
|
||||
export function mapToSubmissionDTO(submission: Submission): SubmissionDTO {
|
||||
return {
|
||||
|
@ -32,7 +32,7 @@ export function mapToSubmissionDTOId(submission: Submission): SubmissionDTOId {
|
|||
};
|
||||
}
|
||||
|
||||
export function mapToSubmission(submissionDTO: SubmissionDTO, submitter: Student, onBehalfOf: Group | undefined): Submission {
|
||||
export function mapToSubmission(submissionDTO: SubmissionDTO, submitter: Student, onBehalfOf: Group): Submission {
|
||||
return getSubmissionRepository().create({
|
||||
learningObjectHruid: submissionDTO.learningObjectIdentifier.hruid,
|
||||
learningObjectLanguage: submissionDTO.learningObjectIdentifier.language,
|
||||
|
|
|
@ -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.js';
|
||||
import { Teacher } from '../entities/users/teacher.entity.js';
|
||||
import { Class } from '../entities/classes/class.entity.js';
|
||||
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,9 @@
|
|||
import express from 'express';
|
||||
import { createSubmissionHandler, deleteSubmissionHandler, getAllSubmissionsHandler, getSubmissionHandler } from '../controllers/submissions.js';
|
||||
import { createSubmissionHandler, deleteSubmissionHandler, getSubmissionHandler, getSubmissionsHandler } from '../controllers/submissions.js';
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
// Root endpoint used to search objects
|
||||
router.get('/', getAllSubmissionsHandler);
|
||||
router.get('/', getSubmissionsHandler);
|
||||
|
||||
router.post('/:id', createSubmissionHandler);
|
||||
|
||||
|
|
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.js';
|
||||
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
router.get('/:username', getAllInvitationsHandler);
|
||||
|
||||
router.get('/:sender/:receiver/:classId', getInvitationHandler);
|
||||
|
||||
router.post('/', createInvitationHandler);
|
||||
|
||||
router.put('/', updateInvitationHandler);
|
||||
|
||||
router.delete('/:sender/:receiver/:classId', deleteInvitationHandler);
|
||||
|
||||
export default router;
|
|
@ -10,6 +10,8 @@ import {
|
|||
getTeacherStudentHandler,
|
||||
updateStudentJoinRequestHandler,
|
||||
} from '../controllers/teachers.js';
|
||||
import invitationRouter from './teacher-invitations.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Root endpoint used to search objects
|
||||
|
@ -32,10 +34,6 @@ router.get('/:username/joinRequests/:classId', getStudentJoinRequestHandler);
|
|||
router.put('/:username/joinRequests/:classId/:studentUsername', updateStudentJoinRequestHandler);
|
||||
|
||||
// Invitations to other classes a teacher received
|
||||
router.get('/:id/invitations', (_req, res) => {
|
||||
res.json({
|
||||
invitations: ['0'],
|
||||
});
|
||||
});
|
||||
router.get('/invitations', invitationRouter);
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
|
||||
import { Group } from '../entities/assignments/group.entity.js';
|
||||
import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
|
||||
import { mapToGroupDTO, mapToGroupDTOId, mapToShallowGroupDTO } from '../interfaces/group.js';
|
||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||
import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
|
|
|
@ -1,12 +1,34 @@
|
|||
import { getQuestionRepository } from '../data/repositories.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 { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
|
||||
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.js';
|
||||
import { FALLBACK_VERSION_NUM } from '../config.js';
|
||||
import { fetchStudent } from './students.js';
|
||||
|
||||
export async function getQuestionsAboutLearningObjectInAssignment(
|
||||
loId: LearningObjectIdentifier,
|
||||
classId: string,
|
||||
assignmentId: number,
|
||||
full: boolean,
|
||||
studentUsername?: string
|
||||
): Promise<QuestionDTO[] | QuestionId[]> {
|
||||
const assignment = await getAssignmentRepository().findByClassIdAndAssignmentId(classId, assignmentId);
|
||||
|
||||
const questions = await getQuestionRepository().findAllQuestionsAboutLearningObjectInAssignment(loId, assignment!, studentUsername);
|
||||
|
||||
if (full) {
|
||||
return questions.map((q) => mapToQuestionDTO(q));
|
||||
}
|
||||
return questions.map((q) => mapToQuestionDTOId(q));
|
||||
}
|
||||
|
||||
export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
|
||||
const questionRepository: QuestionRepository = getQuestionRepository();
|
||||
|
@ -38,14 +60,40 @@ export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO>
|
|||
return mapToQuestionDTO(question);
|
||||
}
|
||||
|
||||
export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> {
|
||||
const answerRepository = getAnswerRepository();
|
||||
const question = await fetchQuestion(questionId);
|
||||
|
||||
if (!question) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
|
||||
|
||||
if (!answers) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return answers.map(mapToAnswerDTO);
|
||||
}
|
||||
|
||||
return answers.map(mapToAnswerDTOId);
|
||||
}
|
||||
|
||||
export async function createQuestion(loId: LearningObjectIdentifier, questionData: QuestionData): Promise<QuestionDTO> {
|
||||
const questionRepository = getQuestionRepository();
|
||||
const author = await fetchStudent(questionData.author!);
|
||||
const content = questionData.content;
|
||||
|
||||
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 question = await questionRepository.createQuestion({
|
||||
loId,
|
||||
author,
|
||||
inGroup: inGroup!,
|
||||
content,
|
||||
});
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import {
|
|||
getSubmissionRepository,
|
||||
} from '../data/repositories.js';
|
||||
import { mapToClassDTO } from '../interfaces/class.js';
|
||||
import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
|
||||
import { mapToGroupDTO, mapToGroupDTOId, mapToShallowGroupDTO } from '../interfaces/group.js';
|
||||
import { mapToStudent, mapToStudentDTO } from '../interfaces/student.js';
|
||||
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
|
||||
import { getAllAssignments } from './assignments.js';
|
||||
|
@ -24,6 +24,7 @@ import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/subm
|
|||
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[]> {
|
||||
const studentRepository = getStudentRepository();
|
||||
|
@ -113,7 +114,8 @@ export async function getStudentSubmissions(username: string, full: boolean): Pr
|
|||
const student = await fetchStudent(username);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submissions = await submissionRepository.findAllSubmissionsForStudent(student);
|
||||
|
||||
const submissions: Submission[] = await submissionRepository.findAllSubmissionsForStudent(student);
|
||||
|
||||
if (full) {
|
||||
return submissions.map(mapToSubmissionDTO);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { getSubmissionRepository } from '../data/repositories.js';
|
||||
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';
|
||||
|
@ -6,6 +6,7 @@ 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 fetchSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<Submission> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
|
@ -32,9 +33,7 @@ export async function getAllSubmissions(loId: LearningObjectIdentifier): Promise
|
|||
|
||||
export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO> {
|
||||
const submitter = await fetchStudent(submissionDTO.submitter.username);
|
||||
// TODO: fix
|
||||
// const group = submissionDTO.group ? await getExistingGroupFromGroupDTO(submissionDTO.group) : undefined;
|
||||
const group = undefined;
|
||||
const group = await getExistingGroupFromGroupDTO(submissionDTO.group!);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = mapToSubmission(submissionDTO, submitter, group);
|
||||
|
@ -51,3 +50,22 @@ export async function deleteSubmission(loId: LearningObjectIdentifier, submissio
|
|||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the submissions made by on behalf of any group the given student is in.
|
||||
*/
|
||||
export async function getSubmissionsForLearningObjectAndAssignment(
|
||||
learningObjectHruid: string,
|
||||
language: Language,
|
||||
version: number,
|
||||
classId: string,
|
||||
assignmentId: number,
|
||||
studentUsername?: string
|
||||
): Promise<SubmissionDTO[]> {
|
||||
const loId = new LearningObjectIdentifier(learningObjectHruid, language, version);
|
||||
const assignment = await getAssignmentRepository().findByClassIdAndAssignmentId(classId, assignmentId);
|
||||
|
||||
const submissions = await getSubmissionRepository().findAllSubmissionsForLearningObjectAndAssignment(loId, assignment!, studentUsername);
|
||||
|
||||
return submissions.map((s) => mapToSubmissionDTO(s));
|
||||
}
|
||||
|
|
87
backend/src/services/teacher-invitations.ts
Normal file
87
backend/src/services/teacher-invitations.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
import { fetchTeacher } from './teachers.js';
|
||||
import { getTeacherInvitationRepository } from '../data/repositories.js';
|
||||
import { mapToInvitation, mapToTeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
|
||||
import { addClassTeacher, fetchClass } from './classes.js';
|
||||
import { TeacherInvitationData, TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
|
||||
import { ConflictException } from '../exceptions/conflict-exception.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { TeacherInvitation } from '../entities/classes/teacher-invitation.entity.js';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
|
||||
export async function getAllInvitations(username: string, sent: boolean): Promise<TeacherInvitationDTO[]> {
|
||||
const teacher = await fetchTeacher(username);
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
|
||||
let invitations;
|
||||
if (sent) {
|
||||
invitations = await teacherInvitationRepository.findAllInvitationsBy(teacher);
|
||||
} else {
|
||||
invitations = await teacherInvitationRepository.findAllInvitationsFor(teacher);
|
||||
}
|
||||
return invitations.map(mapToTeacherInvitationDTO);
|
||||
}
|
||||
|
||||
export async function createInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const sender = await fetchTeacher(data.sender);
|
||||
const receiver = await fetchTeacher(data.receiver);
|
||||
|
||||
const cls = await fetchClass(data.class);
|
||||
|
||||
if (!cls.teachers.contains(sender)) {
|
||||
throw new ConflictException('The teacher sending the invite is not part of the class');
|
||||
}
|
||||
|
||||
const newInvitation = mapToInvitation(sender, receiver, cls);
|
||||
await teacherInvitationRepository.save(newInvitation, { preventOverwrite: true });
|
||||
|
||||
return mapToTeacherInvitationDTO(newInvitation);
|
||||
}
|
||||
|
||||
async function fetchInvitation(usernameSender: string, usernameReceiver: string, classId: string): Promise<TeacherInvitation> {
|
||||
const sender = await fetchTeacher(usernameSender);
|
||||
const receiver = await fetchTeacher(usernameReceiver);
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
const invite = await teacherInvitationRepository.findBy(cls, sender, receiver);
|
||||
|
||||
if (!invite) {
|
||||
throw new NotFoundException('Teacher invite not found');
|
||||
}
|
||||
|
||||
return invite;
|
||||
}
|
||||
|
||||
export async function getInvitation(sender: string, receiver: string, classId: string): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(sender, receiver, classId);
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
||||
|
||||
export async function updateInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(data.sender, data.receiver, data.class);
|
||||
invitation.status = ClassStatus.Declined;
|
||||
|
||||
if (data.accepted) {
|
||||
invitation.status = ClassStatus.Accepted;
|
||||
await addClassTeacher(data.class, data.receiver);
|
||||
}
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
await teacherInvitationRepository.save(invitation);
|
||||
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
||||
|
||||
export async function deleteInvitation(data: TeacherInvitationData): Promise<TeacherInvitationDTO> {
|
||||
const invitation = await fetchInvitation(data.sender, data.receiver, data.class);
|
||||
|
||||
const sender = await fetchTeacher(data.sender);
|
||||
const receiver = await fetchTeacher(data.receiver);
|
||||
const cls = await fetchClass(data.class);
|
||||
|
||||
const teacherInvitationRepository = getTeacherInvitationRepository();
|
||||
await teacherInvitationRepository.deleteBy(cls, sender, receiver);
|
||||
|
||||
return mapToTeacherInvitationDTO(invitation);
|
||||
}
|
|
@ -28,7 +28,7 @@ import { ClassDTO } from '@dwengo-1/common/interfaces/class';
|
|||
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request';
|
||||
import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
|
||||
import { ConflictException } from '../exceptions/conflict-exception.js';
|
||||
|
||||
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
|
||||
|
@ -160,10 +160,10 @@ export async function updateClassJoinRequestStatus(studentUsername: string, clas
|
|||
throw new NotFoundException('Join request not found');
|
||||
}
|
||||
|
||||
request.status = ClassJoinRequestStatus.Declined;
|
||||
request.status = ClassStatus.Declined;
|
||||
|
||||
if (accepted) {
|
||||
request.status = ClassJoinRequestStatus.Accepted;
|
||||
request.status = ClassStatus.Accepted;
|
||||
await addClassStudent(classId, studentUsername);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue