route.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { NextRequest, NextResponse } from "next/server"
  2. import { getServerSession } from "next-auth"
  3. import { authOptions } from "@/lib/auth"
  4. import { prisma } from "@/lib/prisma"
  5. import type { DailyLog } from "@prisma/client"
  6. // GET - Obtener estadísticas de los logs
  7. export async function GET(req: NextRequest) {
  8. try {
  9. const session = await getServerSession(authOptions)
  10. if (!session?.user) {
  11. return NextResponse.json({ error: "No autorizado" }, { status: 401 })
  12. }
  13. if (session.user.role !== "PATIENT") {
  14. return NextResponse.json(
  15. { error: "Solo pacientes pueden acceder a estadísticas" },
  16. { status: 403 }
  17. )
  18. }
  19. // Obtener parámetros
  20. const searchParams = req.nextUrl.searchParams
  21. const startDate = searchParams.get("startDate")
  22. const endDate = searchParams.get("endDate")
  23. // Si no hay fechas, usar últimos 30 días
  24. const end = endDate ? new Date(endDate) : new Date()
  25. const start = startDate
  26. ? new Date(startDate)
  27. : new Date(end.getTime() - 30 * 24 * 60 * 60 * 1000)
  28. // Obtener logs en el rango
  29. const logs = await prisma.dailyLog.findMany({
  30. where: {
  31. userId: session.user.id,
  32. date: {
  33. gte: start,
  34. lte: end,
  35. },
  36. },
  37. orderBy: {
  38. date: "asc",
  39. },
  40. })
  41. // Calcular estadísticas
  42. const totalEntries = logs.length
  43. if (totalEntries === 0) {
  44. return NextResponse.json({
  45. totalEntries: 0,
  46. avgMood: null,
  47. avgEnergy: null,
  48. avgSleep: null,
  49. avgSleepQuality: null,
  50. streak: 0,
  51. moodDistribution: {},
  52. energyDistribution: {},
  53. })
  54. }
  55. // Promedios
  56. const moodLogs = logs.filter((log): log is DailyLog & { mood: number } => log.mood !== null)
  57. const energyLogs = logs.filter((log): log is DailyLog & { energy: number } => log.energy !== null)
  58. const sleepLogs = logs.filter((log): log is DailyLog & { sleepHours: number } => log.sleepHours !== null)
  59. const sleepQualityLogs = logs.filter((log): log is DailyLog & { sleepQuality: number } => log.sleepQuality !== null)
  60. const avgMood = moodLogs.length
  61. ? moodLogs.reduce((sum: number, log) => sum + log.mood, 0) / moodLogs.length
  62. : null
  63. const avgEnergy = energyLogs.length
  64. ? energyLogs.reduce((sum: number, log) => sum + log.energy, 0) /
  65. energyLogs.length
  66. : null
  67. const avgSleep = sleepLogs.length
  68. ? sleepLogs.reduce((sum: number, log) => sum + log.sleepHours, 0) /
  69. sleepLogs.length
  70. : null
  71. const avgSleepQuality = sleepQualityLogs.length
  72. ? sleepQualityLogs.reduce((sum: number, log) => sum + log.sleepQuality, 0) /
  73. sleepQualityLogs.length
  74. : null
  75. // Calcular racha (días consecutivos con registro)
  76. let streak = 0
  77. const today = new Date()
  78. today.setHours(0, 0, 0, 0)
  79. // Ordenar logs por fecha descendente
  80. const sortedLogs = [...logs].sort(
  81. (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
  82. )
  83. // Verificar si hay registro hoy o ayer
  84. const latestLog = sortedLogs[0]
  85. if (latestLog) {
  86. const latestDate = new Date(latestLog.date)
  87. latestDate.setHours(0, 0, 0, 0)
  88. const daysDiff = Math.floor(
  89. (today.getTime() - latestDate.getTime()) / (1000 * 60 * 60 * 24)
  90. )
  91. if (daysDiff <= 1) {
  92. // Hay registro hoy o ayer, calcular racha
  93. streak = 1
  94. const checkDate = new Date(latestDate)
  95. for (let i = 1; i < sortedLogs.length; i++) {
  96. checkDate.setDate(checkDate.getDate() - 1)
  97. const currentLogDate = new Date(sortedLogs[i].date)
  98. currentLogDate.setHours(0, 0, 0, 0)
  99. if (currentLogDate.getTime() === checkDate.getTime()) {
  100. streak++
  101. } else {
  102. break
  103. }
  104. }
  105. }
  106. }
  107. // Distribución de mood y energy
  108. const moodDistribution: Record<number, number> = {}
  109. const energyDistribution: Record<number, number> = {}
  110. logs.forEach((log) => {
  111. if (log.mood) {
  112. moodDistribution[log.mood] = (moodDistribution[log.mood] || 0) + 1
  113. }
  114. if (log.energy) {
  115. energyDistribution[log.energy] = (energyDistribution[log.energy] || 0) + 1
  116. }
  117. })
  118. return NextResponse.json({
  119. totalEntries,
  120. avgMood: avgMood ? Number(avgMood.toFixed(1)) : null,
  121. avgEnergy: avgEnergy ? Number(avgEnergy.toFixed(1)) : null,
  122. avgSleep: avgSleep ? Number(avgSleep.toFixed(1)) : null,
  123. avgSleepQuality: avgSleepQuality ? Number(avgSleepQuality.toFixed(1)) : null,
  124. streak,
  125. moodDistribution,
  126. energyDistribution,
  127. })
  128. } catch (error) {
  129. console.error("Error al calcular estadísticas:", error)
  130. return NextResponse.json(
  131. { error: "Error al calcular estadísticas" },
  132. { status: 500 }
  133. )
  134. }
  135. }