All files / web/src/app/api/worksheets/skills/customizations route.ts

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

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                                                                                   
import { eq, and } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { db } from '@/db'
import { skillCustomizations } from '@/db/schema'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/worksheets/skills/customizations?operator=addition
 *
 * Get all skill customizations for the current user
 */
export const GET = withAuth(async (request) => {
  try {
    const userId = await getUserId()
    const { searchParams } = new URL(request.url)
    const operator = searchParams.get('operator') as 'addition' | 'subtraction' | null

    const query = operator
      ? and(eq(skillCustomizations.userId, userId), eq(skillCustomizations.operator, operator))
      : eq(skillCustomizations.userId, userId)

    const customizations = await db.query.skillCustomizations.findMany({
      where: query,
      orderBy: (skillCustomizations, { asc }) => [asc(skillCustomizations.updatedAt)],
    })

    // Parse JSON fields
    const parsed = customizations.map((customization) => ({
      ...customization,
      digitRange: JSON.parse(customization.digitRange),
      regroupingConfig: JSON.parse(customization.regroupingConfig),
      displayRules: JSON.parse(customization.displayRules),
    }))

    return NextResponse.json({ customizations: parsed })
  } catch (error) {
    console.error('Failed to fetch skill customizations:', error)
    return NextResponse.json({ error: 'Failed to fetch skill customizations' }, { status: 500 })
  }
})