diff --git a/ExpressAPI/src/helpers/DojoModelsHelper.ts b/ExpressAPI/src/helpers/DojoModelsHelper.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fa9e5f2d28180bdf5711fb65f572218e253160b --- /dev/null +++ b/ExpressAPI/src/helpers/DojoModelsHelper.ts @@ -0,0 +1,33 @@ +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 diff --git a/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts b/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts index 56819c91d853807036d4848fd6b9fbefcc128792..a5f813993c56c3d5c4824854231a2a6486cddd3b 100644 --- a/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts +++ b/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts @@ -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); }); } diff --git a/ExpressAPI/src/routes/AssignmentRoutes.ts b/ExpressAPI/src/routes/AssignmentRoutes.ts index c32ee2dfa6a061a997ca10a0067c592e9756814f..fc39e223883f9155e72ca5259d7365dbe9764f78 100644 --- a/ExpressAPI/src/routes/AssignmentRoutes.ts +++ b/ExpressAPI/src/routes/AssignmentRoutes.ts @@ -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) { diff --git a/ExpressAPI/src/types/DatabaseTypes.ts b/ExpressAPI/src/types/DatabaseTypes.ts index ae95e3bfb03a2b7c52dab17b486f36439ae139f6..14e9b00a7f35a210302854e69c94dcc2f4dc63b4 100644 --- a/ExpressAPI/src/types/DatabaseTypes.ts +++ b/ExpressAPI/src/types/DatabaseTypes.ts @@ -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