Skip to content
Snippets Groups Projects
Commit acd19aaf authored by michael.minelli's avatar michael.minelli
Browse files

Merge branch 'add-corrige-list-to-assignments' into v3.5.0

parents c711b82a 93ce4f1c
No related branches found
No related tags found
No related merge requests found
Pipeline #29612 passed
import LazyVal from '../shared/helpers/LazyVal';
class DojoModelsHelper {
/**
* Get a full serializable object from a given object that contains LazyVal instances
*
* @param obj
* @param depth The depth of the search for LazyVal instances
*/
async getFullSerializableObject<T extends NonNullable<unknown>>(obj: T, depth: number = 0): Promise<unknown> {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const result: any = {};
for ( const key in obj ) {
let value: unknown = obj[key];
if ( value instanceof LazyVal ) {
value = await (obj[key] as LazyVal<unknown>).value;
}
if ( typeof value === 'object' && obj[key] !== null && depth > 0 ) {
result[key] = await this.getFullSerializableObject(value as NonNullable<unknown>, depth - 1);
} else {
result[key] = value;
}
}
return result;
}
}
export default new DojoModelsHelper();
\ No newline at end of file
......@@ -4,16 +4,21 @@ import db from '../../DatabaseHelper';
import LazyVal from '../../../shared/helpers/LazyVal';
async function getCorrections(assignment: { name: string }): Promise<Array<Exercise> | undefined> {
async function getCorrections(assignment: { name: string }): Promise<Array<Partial<Exercise>> | undefined> {
try {
return await db.exercise.findMany({
where: {
where : {
assignmentName : assignment.name,
correctionCommit: {
not: Prisma.JsonNull
}
},
include: {
assignment: false,
members : true,
results : false
}
}) as Array<Exercise> ?? undefined;
}) as Array<Partial<Exercise>> ?? undefined;
} catch ( e ) {
return undefined;
}
......@@ -25,7 +30,7 @@ export default Prisma.defineExtension(client => {
assignment: {
corrections: {
compute(assignment) {
return new LazyVal<Array<Exercise> | undefined>(() => {
return new LazyVal<Array<Partial<Exercise>> | undefined>(() => {
return getCorrections(assignment);
});
}
......
......@@ -25,6 +25,7 @@ import path from 'path';
import SharedAssignmentHelper from '../shared/helpers/Dojo/SharedAssignmentHelper';
import GlobalHelper from '../helpers/GlobalHelper';
import DojoStatusCode from '../shared/types/Dojo/DojoStatusCode';
import DojoModelsHelper from '../helpers/DojoModelsHelper';
class AssignmentRoutes implements RoutesManager {
......@@ -66,33 +67,19 @@ class AssignmentRoutes implements RoutesManager {
// Get an assignment by its name or gitlab url
private async getAssignment(req: express.Request, res: express.Response) {
const assignment: Assignment | undefined = req.boundParams.assignment;
const assignment: Partial<Assignment> | undefined = req.boundParams.assignment;
if ( assignment && !assignment.published && !await AssignmentManager.isUserAllowedToAccessAssignment(assignment, req.session.profile) ) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if ( assignment && !assignment.published && !await AssignmentManager.isUserAllowedToAccessAssignment(assignment as Assignment, req.session.profile) ) {
delete assignment.gitlabId;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.gitlabLink;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.gitlabCreationInfo;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.gitlabLastInfo;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.gitlabLastInfoDate;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.staff;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete assignment.exercises;
}
return assignment ? req.session.sendResponse(res, StatusCodes.OK, assignment) : res.status(StatusCodes.NOT_FOUND).send();
return assignment ? req.session.sendResponse(res, StatusCodes.OK, DojoModelsHelper.getFullSerializableObject(assignment)) : res.status(StatusCodes.NOT_FOUND).send();
}
private async createAssignment(req: express.Request, res: express.Response) {
......
......@@ -35,6 +35,6 @@ export type Exercise = Prisma.ExerciseGetPayload<typeof exerciseBase> & {
isCorrection: boolean
}
export type Assignment = Prisma.AssignmentGetPayload<typeof assignmentBase> & {
corrections: LazyVal<Exercise>
corrections: LazyVal<Array<Exercise>>
}
export type Result = Prisma.ResultGetPayload<typeof resultBase>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment