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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"express-rate-limit": "^8.0.1",
"helmet": "^8.1.0",
"jsonwebtoken": "^9.0.2",
"mercadopago": "^2.9.0",
"mercadopago": "^2.12.0",
"multer": "^2.0.2",
"multer-storage-cloudinary": "^4.0.0",
"nodemailer": "^7.0.9",
Expand Down
19 changes: 13 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/models/assessment/assessment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ async function getAttemptsByStudent(req: Request, res: Response) {
return HttpResponse.Ok(res, attempts);
}

/**
* Handles checking for an active attempt on an assessment.
* @param {Request} req - The Express request object, containing the assessment ID in params.
* @param {Response} res - The Express response object.
* @returns {Promise<Response>} The active attempt or null.
*/
async function getActiveAttempt(req: Request, res: Response) {
try {
const assessmentService = new AssessmentService(orm.em.fork(), req.log);
const { assessmentId } = req.params;
const requestingUserId = req.user!.id;

const studentId = await getStudentIdFromUserId(orm.em.fork(), requestingUserId);

const activeAttempt = await assessmentService.getActiveAttempt(
assessmentId,
studentId
);

return HttpResponse.Ok(res, activeAttempt);
} catch (error: any) {
req.log.error({ error }, 'Error fetching active attempt');
if (error.message === 'User is not a student') {
return HttpResponse.Unauthorized(res, 'No se encontró un perfil de estudiante para este usuario.');
}
if (error.name === 'NotFoundError') {
return HttpResponse.NotFound(res, 'Assessment not found.');
}
throw error;
}
}

/**
* Handles retrieving a single attempt with all its answers.
* @param {Request} req - The Express request object, containing the attempt ID in params.
Expand Down Expand Up @@ -410,6 +442,7 @@ export {
submitAttempt,
getAttemptsByAssessment,
getAttemptsByStudent,
getActiveAttempt,
getAttemptWithAnswers,
getPendingAssessments,
getAssessmentsByCourse,
Expand Down
4 changes: 3 additions & 1 deletion src/models/assessment/assessment.dtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export interface AssessmentPublicSchema {
isActive: boolean;
availableFrom?: string;
availableUntil?: string;
questionsCount: number;
attemptsCount: number;
attemptsRemaining: number | null; // null if it's unlimited
attemptsRemaining: number | null;
bestScore?: number;
lastAttemptDate?: string;
status: string;
hasActiveAttempt: boolean;
}
2 changes: 2 additions & 0 deletions src/models/assessment/assessment.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export const AssessmentMapper = {
? new Date(assessment.availableUntil).toISOString()
: undefined,

questionsCount: assessment.questionsCount || 0,
attemptsCount: assessment.attemptsCount || 0,
attemptsRemaining: assessment.attemptsRemaining,
bestScore: assessment.bestScore,
lastAttemptDate: assessment.lastAttemptDate
? new Date(assessment.lastAttemptDate).toISOString()
: undefined,
status: assessment.status || 'available',
hasActiveAttempt: assessment.hasActiveAttempt || false,
};
},

Expand Down
7 changes: 7 additions & 0 deletions src/models/assessment/assessment.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ assessmentRouter.patch(
assessmentRouter.delete('/:id', professorOrAdmin, assessmentController.remove);

// Attempt routes (Students & Professors)
// GET /api/assessments/:assessmentId/active-attempt - Check for active attempt
assessmentRouter.get(
'/:assessmentId/active-attempt',
studentOrProfessor,
assessmentController.getActiveAttempt
);

// POST /api/assessments/:assessmentId/attempts - Start new attempt
assessmentRouter.post(
'/:assessmentId/attempts',
Expand Down
Loading