| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- import { NextRequest, NextResponse } from 'next/server';
- import { getServerSession } from 'next-auth';
- import { authOptions } from '@/lib/auth';
- import { prisma } from '@/lib/prisma';
- import { AttendanceStatus } from '@prisma/client';
- export async function GET(request: NextRequest) {
- try {
- // Verificar sesión
- const session = await getServerSession(authOptions);
- if (!session?.user?.id) {
- return NextResponse.json(
- { error: 'No autorizado' },
- { status: 401 }
- );
- }
- // Verificar que el usuario sea estudiante
- const user = await prisma.user.findUnique({
- where: { id: session.user.id },
- include: { student: true }
- });
- if (!user || user.role !== 'STUDENT' || !user.student) {
- return NextResponse.json(
- { error: 'Acceso denegado. Solo estudiantes pueden acceder.' },
- { status: 403 }
- );
- }
- // Obtener parámetros de consulta
- const { searchParams } = new URL(request.url);
- const sectionId = searchParams.get('sectionId');
- const status = searchParams.get('status') as AttendanceStatus | 'all' | null;
- const startDate = searchParams.get('startDate');
- const endDate = searchParams.get('endDate');
- const periodId = searchParams.get('periodId');
- /* eslint-disable @typescript-eslint/no-explicit-any */
- // Construir filtros
- const whereClause: any = {
- studentId: user.student.id,
- };
- if (sectionId && sectionId !== 'all') {
- whereClause.sectionId = sectionId;
- }
- if (status && status !== 'all') {
- whereClause.status = status;
- }
- if (startDate) {
- whereClause.date = {
- ...whereClause.date,
- gte: new Date(startDate)
- };
- }
- if (endDate) {
- whereClause.date = {
- ...whereClause.date,
- lte: new Date(endDate)
- };
- }
- // Si se especifica un período, filtrar por secciones de ese período
- if (periodId && periodId !== 'all') {
- whereClause.section = {
- class: {
- periodId: periodId
- }
- };
- }
- // Obtener registros de asistencia
- const attendanceRecords = await prisma.attendance.findMany({
- where: whereClause,
- include: {
- section: {
- include: {
- class: {
- include: {
- period: true
- }
- },
- teacherAssignments: {
- where: { isActive: true },
- include: {
- teacher: true
- }
- }
- }
- }
- },
- orderBy: {
- date: 'desc'
- }
- });
- // Obtener estadísticas generales
- const totalRecords = attendanceRecords.length;
- const presentCount = attendanceRecords.filter(r => r.status === 'PRESENT').length;
- const absentCount = attendanceRecords.filter(r => r.status === 'ABSENT').length;
- const justifiedCount = attendanceRecords.filter(r => r.status === 'JUSTIFIED').length;
- const attendanceRate = totalRecords > 0 ? Math.round((presentCount / totalRecords) * 100) : 0;
- /* eslint-disable @typescript-eslint/no-explicit-any */
- // Estadísticas por sección
- const sectionStats = attendanceRecords.reduce((acc, record) => {
- const sectionId = record.sectionId;
- if (!acc[sectionId]) {
- acc[sectionId] = {
- sectionId,
- sectionName: record.section.name,
- className: record.section.class.name,
- classCode: record.section.class.code,
- periodName: record.section.class.period.name,
- total: 0,
- present: 0,
- absent: 0,
- justified: 0,
- attendanceRate: 0
- };
- }
-
- acc[sectionId].total++;
- if (record.status === 'PRESENT') acc[sectionId].present++;
- if (record.status === 'ABSENT') acc[sectionId].absent++;
- if (record.status === 'JUSTIFIED') acc[sectionId].justified++;
-
- acc[sectionId].attendanceRate = Math.round(
- (acc[sectionId].present / acc[sectionId].total) * 100
- );
-
- return acc;
- }, {} as Record<string, any>);
- // Estadísticas por período
- const periodStats = attendanceRecords.reduce((acc, record) => {
- const periodId = record.section.class.period.id;
- if (!acc[periodId]) {
- acc[periodId] = {
- periodId,
- periodName: record.section.class.period.name,
- isActive: record.section.class.period.isActive,
- total: 0,
- present: 0,
- absent: 0,
- justified: 0,
- attendanceRate: 0
- };
- }
-
- acc[periodId].total++;
- if (record.status === 'PRESENT') acc[periodId].present++;
- if (record.status === 'ABSENT') acc[periodId].absent++;
- if (record.status === 'JUSTIFIED') acc[periodId].justified++;
-
- acc[periodId].attendanceRate = Math.round(
- (acc[periodId].present / acc[periodId].total) * 100
- );
-
- return acc;
- }, {} as Record<string, any>);
- // Obtener todas las secciones matriculadas para filtros
- const enrolledSections = await prisma.studentEnrollment.findMany({
- where: {
- studentId: user.student.id,
- isActive: true
- },
- include: {
- section: {
- include: {
- class: {
- include: {
- period: true
- }
- }
- }
- }
- }
- });
- // Obtener períodos únicos
- const periods = Array.from(
- new Set(
- enrolledSections.map(e => e.section.class.period)
- )
- ).filter((period, index, self) =>
- self.findIndex(p => p.id === period.id) === index
- );
- return NextResponse.json({
- student: {
- id: user.student.id,
- firstName: user.student.firstName,
- lastName: user.student.lastName,
- admissionNumber: user.student.admissionNumber
- },
- attendanceRecords: attendanceRecords.map(record => ({
- id: record.id,
- date: record.date,
- status: record.status,
- reason: record.reason,
- section: {
- id: record.section.id,
- name: record.section.name,
- class: {
- id: record.section.class.id,
- name: record.section.class.name,
- code: record.section.class.code,
- period: {
- id: record.section.class.period.id,
- name: record.section.class.period.name,
- isActive: record.section.class.period.isActive
- }
- },
- teachers: record.section.teacherAssignments.map(ta => ({
- id: ta.teacher.id,
- firstName: ta.teacher.firstName,
- lastName: ta.teacher.lastName,
- email: ta.teacher.email
- }))
- }
- })),
- statistics: {
- overall: {
- totalRecords,
- present: presentCount,
- absent: absentCount,
- justified: justifiedCount,
- attendanceRate
- },
- bySections: Object.values(sectionStats),
- byPeriods: Object.values(periodStats)
- },
- filters: {
- sections: enrolledSections.map(e => ({
- id: e.section.id,
- name: e.section.name,
- className: e.section.class.name,
- classCode: e.section.class.code,
- periodName: e.section.class.period.name
- })),
- periods: periods.map(p => ({
- id: p.id,
- name: p.name,
- isActive: p.isActive
- }))
- }
- });
- } catch (error) {
- console.error('Error al obtener historial de asistencia:', error);
- return NextResponse.json(
- { error: 'Error interno del servidor' },
- { status: 500 }
- );
- }
- }
|