import { Request, Response, NextFunction } from 'express'; import { ClassEnrollmentService } from '../services/classEnrollment.service'; import { ApiError } from '../middleware/errorHandler'; export class ClassEnrollmentController { // Inscribirse en una clase static async enrollInClass(req: Request, res: Response, next: NextFunction) { try { const userId = req.user!.userId; const result = await ClassEnrollmentService.enrollInClass(userId, req.body); res.status(201).json({ success: true, message: 'Inscripción creada exitosamente', data: result, }); } catch (error) { next(error); } } // Webhook de MercadoPago static async webhook(req: Request, res: Response, next: NextFunction) { try { // Responder inmediatamente a MP res.status(200).send('OK'); // Procesar el webhook de forma asíncrona await ClassEnrollmentService.processPaymentWebhook(req.body); } catch (error) { // Loggear error pero no enviar respuesta (ya se envió 200) console.error('Error procesando webhook:', error); } } // Cancelar inscripción static async cancelEnrollment(req: Request, res: Response, next: NextFunction) { try { const userId = req.user!.userId; const { id } = req.params; const result = await ClassEnrollmentService.cancelEnrollment(userId, id); res.status(200).json({ success: true, message: result.message, }); } catch (error) { next(error); } } // Obtener mis inscripciones static async getMyEnrollments(req: Request, res: Response, next: NextFunction) { try { const userId = req.user!.userId; const status = req.query.status as string | undefined; const enrollments = await ClassEnrollmentService.getMyEnrollments(userId, status); res.status(200).json({ success: true, count: enrollments.length, data: enrollments, }); } catch (error) { next(error); } } // Obtener inscripción por ID static async getEnrollmentById(req: Request, res: Response, next: NextFunction) { try { const userId = req.user!.userId; const { id } = req.params; const enrollment = await ClassEnrollmentService.getEnrollmentById(id, userId); res.status(200).json({ success: true, data: enrollment, }); } catch (error) { next(error); } } // Marcar asistencia (solo coach) static async markAttendance(req: Request, res: Response, next: NextFunction) { try { const userId = req.user!.userId; const { id } = req.params; const enrollment = await ClassEnrollmentService.markAttendance(id, userId); res.status(200).json({ success: true, message: 'Asistencia marcada exitosamente', data: enrollment, }); } catch (error) { next(error); } } } export default ClassEnrollmentController;