Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend/src/core/repositories/submissionRepositoryInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,26 @@ export abstract class ISubmissionRepository extends AbstractRepository {
* @returns A promise that resolves when the submission is deleted.
*/
public abstract delete(id: string): Promise<void>;

/**
* Get all submissions for a specific student in a specific step of an assignment.
* @param studentId The id of the student.
* @param assignmentId The id of the assignment.
* @param learning_object_id The id of the learning object.
* @throws EntityNotFoundError when the assignment or student is not found.
* @returns A promise that resolves to the retrieved submissions.
*/
public abstract getAllForStudentInAssignmentStep(
studentId: string,
assignmentId: string,
learning_object_id: string,
): Promise<Submission[]>;

/**
* Get all submissions for a specific student, for any assignment and step in that assignment.
* @param studentId The id of the student.
* @throws EntityNotFoundError when the student is not found.
* @returns A promise that resolves to the retrieved submissions.
*/
public abstract getByStudentId(studentId: string): Promise<Submission[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export abstract class IDatasourceSubmission {
public abstract getById(id: string): Promise<Submission | null>;
public abstract update(submission: Submission): Promise<Submission>;
public abstract delete(submissionId: string): Promise<void>;
public abstract getAllForStudentInAssignmentStep(
studentId: string,
assignmentId: string,
learningObjectId: string,
): Promise<Submission[]>;
public abstract getByStudentId(studentId: string): Promise<Submission[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,55 @@ export class DatasourceSubmissionTypeORM extends IDatasourceSubmission {
public async delete(submission: string): Promise<void> {
await this.datasource.getRepository(SubmissionTypeORM).delete(submission);
}

public async getAllForStudentInAssignmentStep(
studentId: string,
assignmentId: string,
learningObjectId: string,
): Promise<Submission[]> {
const studentRepository = this.datasource.getRepository(StudentTypeORM);
const assignmentRepository = this.datasource.getRepository(AssignmentTypeORM);
const submissionRepository = this.datasource.getRepository(SubmissionTypeORM);

// First get the student
const studentModel: StudentTypeORM | null = await studentRepository.findOne({
where: { id: studentId },
});
if (!studentModel) {
throw new EntityNotFoundError(`Student with id ${studentId} not found`);
}
// Then get the assignment
const assignmentModel: AssignmentTypeORM | null = await assignmentRepository.findOne({
where: { id: assignmentId },
});
if (!assignmentModel) {
throw new EntityNotFoundError(`Assignment with id ${assignmentId} not found`);
}
// Now get all the student's submissions for the step of the assignment
const submissionModels: SubmissionTypeORM[] = await submissionRepository.find({
where: { assignment: assignmentModel, student: studentModel, learning_object_id: learningObjectId },
relations: ["student", "assignment"],
});
// Return the submissions as entities
return submissionModels.map(model => model.toEntity());
}

public async getByStudentId(studentId: string): Promise<Submission[]> {
const studentRepository = this.datasource.getRepository(StudentTypeORM);
const submissionRepository = this.datasource.getRepository(SubmissionTypeORM);
// First get the student
const studentModel: StudentTypeORM | null = await studentRepository.findOne({
where: { id: studentId },
});
if (!studentModel) {
throw new EntityNotFoundError(`Student with id ${studentId} not found`);
}
// Now get all the student's submissions for any assignment and step
const submissionModels: SubmissionTypeORM[] = await submissionRepository.find({
where: { student: studentModel },
relations: ["student"],
});
// Return the submissions as entities
return submissionModels.map(model => model.toEntity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ export class SubmissionRepositoryTypeORM extends ISubmissionRepository {
public async delete(submissionId: string): Promise<void> {
return await (await this.datasourceSubmission).delete(submissionId);
}

public async getAllForStudentInAssignmentStep(
studentId: string,
assignmentId: string,
learningObjectId: string,
): Promise<Submission[]> {
return await (
await this.datasourceSubmission
).getAllForStudentInAssignmentStep(studentId, assignmentId, learningObjectId);
}

public async getByStudentId(studentId: string): Promise<Submission[]> {
return await (await this.datasourceSubmission).getByStudentId(studentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ describe("SubmissionRepositoryTypeORM", () => {
update: jest.fn(() => Promise.resolve(submission)),
getSubmissionsByClassId: jest.fn(() => Promise.resolve(submission)),
getSubmissionsByLearningPathId: jest.fn(() => Promise.resolve([submission, submission])),
delete: jest.fn()
delete: jest.fn(),
getAllForStudentInAssignmentStep: jest.fn(() => Promise.resolve(submission)),
getByStudentId: jest.fn(() => Promise.resolve(submission)),
} as any;

// Mock submission
Expand Down Expand Up @@ -85,4 +87,32 @@ describe("SubmissionRepositoryTypeORM", () => {
expect(datasourceSubmission.delete).toHaveBeenCalledWith(createdSubmissionId);
});

test("getAllForStudentInAssignmentStep", async () => {

const id: string = await datasourceSubmission.create(submission);

const result: Submission[] = await datasourceSubmission.getAllForStudentInAssignmentStep(
submission.studentId,
submission.assignmentId,
submission.learningObjectId
);

expect(datasourceSubmission.getAllForStudentInAssignmentStep).toHaveBeenCalledTimes(1);
expect(datasourceSubmission.getAllForStudentInAssignmentStep).toHaveBeenCalledWith(
submission.studentId,
submission.assignmentId,
submission.learningObjectId
)
});

test("getByStudentId", async () => {

await datasourceSubmission.create(submission);

const result: Submission[] = await datasourceSubmission.getByStudentId(submission.studentId);

expect(datasourceSubmission.getByStudentId).toHaveBeenCalledTimes(1);
expect(datasourceSubmission.getByStudentId).toHaveBeenCalledWith(submission.studentId)
});

});