All files / web/src/app/api/settings/practice-config route.ts

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

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                                                                                                                         
import { eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { db } from '@/db'
import { appSettings } from '@/db/schema'
import {
  DEFAULT_TERM_COUNT_SCALING,
  parseTermCountScaling,
  validateTermCountScaling,
  type TermCountScalingConfig,
} from '@/lib/curriculum/config/term-count-scaling'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * Ensure the default settings row exists.
 */
async function ensureDefaultSettings() {
  const existing = await db.select().from(appSettings).where(eq(appSettings.id, 'default')).limit(1)
  if (existing.length === 0) {
    await db.insert(appSettings).values({ id: 'default' })
  }
}

/**
 * GET /api/settings/practice-config
 *
 * Returns the current term count scaling configuration.
 * If no custom config is saved, returns the hardcoded defaults.
 */
export const GET = withAuth(async () => {
  try {
    await ensureDefaultSettings()

    const [settings] = await db
      .select()
      .from(appSettings)
      .where(eq(appSettings.id, 'default'))
      .limit(1)

    const config = parseTermCountScaling(settings?.termCountScaling ?? null)
    const isCustom = settings?.termCountScaling !== null && settings?.termCountScaling !== undefined

    return NextResponse.json({ config, isCustom })
  } catch (error) {
    console.error('Error fetching practice config:', error)
    return NextResponse.json({ error: 'Failed to fetch practice config' }, { status: 500 })
  }
})

/**
 * PATCH /api/settings/practice-config
 *
 * Updates the term count scaling configuration.
 *
 * Body:
 * - config: TermCountScalingConfig | null (null = reset to defaults)
 */
export const PATCH = withAuth(async (request) => {
  try {
    const body = await request.json()
    const { config } = body as { config: TermCountScalingConfig | null }

    // null means reset to defaults
    if (config === null) {
      await ensureDefaultSettings()
      await db
        .update(appSettings)
        .set({ termCountScaling: null })
        .where(eq(appSettings.id, 'default'))

      return NextResponse.json({ config: DEFAULT_TERM_COUNT_SCALING, isCustom: false })
    }

    // Validate the config
    const error = validateTermCountScaling(config)
    if (error) {
      return NextResponse.json({ error }, { status: 400 })
    }

    await ensureDefaultSettings()

    const jsonStr = JSON.stringify(config)
    await db
      .update(appSettings)
      .set({ termCountScaling: jsonStr })
      .where(eq(appSettings.id, 'default'))

    return NextResponse.json({ config, isCustom: true })
  } catch (error) {
    console.error('Error updating practice config:', error)
    return NextResponse.json({ error: 'Failed to update practice config' }, { status: 500 })
  }
})