All files / web/src/app/api/debug/seed-students/seeded route.ts

0% Statements 0/57
0% Branches 0/1
0% Functions 0/1
0% Lines 0/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
import { eq, desc } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db, schema } from '@/db'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/debug/seed-students/seeded
 *
 * Returns previously-seeded test students for the current user,
 * keyed by profile name. Only returns the most recently seeded
 * player for each profile.
 */
export const GET = withAuth(
  async () => {
    try {
      const userId = await getUserId()

      // Join seed_profile_players with players to get the data we need,
      // filtered to only players owned by this user
      const rows = await db
        .select({
          profileId: schema.seedProfilePlayers.profileId,
          playerId: schema.seedProfilePlayers.playerId,
          seededAt: schema.seedProfilePlayers.seededAt,
          playerName: schema.players.name,
        })
        .from(schema.seedProfilePlayers)
        .innerJoin(schema.players, eq(schema.seedProfilePlayers.playerId, schema.players.id))
        .where(eq(schema.players.userId, userId))
        .orderBy(desc(schema.seedProfilePlayers.seededAt))

      // Keep only the most recent player per profile
      const seeded: Record<string, { playerId: string; seededAt: string }> = {}
      for (const row of rows) {
        if (!seeded[row.profileId]) {
          seeded[row.profileId] = {
            playerId: row.playerId,
            seededAt:
              row.seededAt instanceof Date
                ? row.seededAt.toISOString()
                : new Date(Number(row.seededAt) * 1000).toISOString(),
          }
        }
      }

      return NextResponse.json({ seeded })
    } catch (error) {
      console.error('[seed-students/seeded] Failed:', error)
      return NextResponse.json(
        { error: error instanceof Error ? error.message : 'Failed to fetch seeded students' },
        { status: 500 }
      )
    }
  },
  { role: 'admin' }
)