All files / web/src/app/api/settings/audio-voice route.ts

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

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 94 95 96 97 98 99 100 101 102                                                                                                                                                                                                           
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db } from '@/db'
import { appSettings, DEFAULT_APP_SETTINGS } from '@/db/schema'
import { withAuth } from '@/lib/auth/withAuth'

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',
      bktConfidenceThreshold: DEFAULT_APP_SETTINGS.bktConfidenceThreshold,
      audioVoice: DEFAULT_APP_SETTINGS.audioVoice,
    })
  }
}

/**
 * GET /api/settings/audio-voice
 *
 * Returns the active TTS voice name.
 */
export const GET = withAuth(async () => {
  try {
    await ensureDefaultSettings()

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

    return NextResponse.json({
      audioVoice: settings?.audioVoice ?? DEFAULT_APP_SETTINGS.audioVoice,
    })
  } catch (error) {
    console.error('Error fetching audio voice setting:', error)
    return NextResponse.json({ error: 'Failed to fetch settings' }, { status: 500 })
  }
})

/**
 * PATCH /api/settings/audio-voice
 *
 * Updates the active TTS voice.
 *
 * Body: { audioVoice: string }
 */
export const PATCH = withAuth(async (request) => {
  try {
    const body = await request.json()
    const { audioVoice } = body

    if (typeof audioVoice !== 'string' || audioVoice.trim().length === 0) {
      return NextResponse.json({ error: 'audioVoice must be a non-empty string' }, { status: 400 })
    }

    await ensureDefaultSettings()

    const trimmed = audioVoice.trim()

    // Also update the voice chain: replace the first pregenerated entry's name
    const [current] = await db
      .select()
      .from(appSettings)
      .where(eq(appSettings.id, 'default'))
      .limit(1)

    type VoiceSource = { type: 'pregenerated'; name: string } | { type: 'browser-tts' }
    let chain: VoiceSource[] = [{ type: 'pregenerated', name: trimmed }, { type: 'browser-tts' }]
    if (current?.voiceChain) {
      try {
        const parsed = JSON.parse(current.voiceChain) as VoiceSource[]
        let replaced = false
        chain = parsed.map((s) => {
          if (s.type === 'pregenerated' && !replaced) {
            replaced = true
            return { type: 'pregenerated' as const, name: trimmed }
          }
          return s
        })
        if (!replaced) {
          chain.unshift({ type: 'pregenerated', name: trimmed })
        }
      } catch {
        // Keep the default chain
      }
    }

    await db
      .update(appSettings)
      .set({ audioVoice: trimmed, voiceChain: JSON.stringify(chain) })
      .where(eq(appSettings.id, 'default'))

    return NextResponse.json({ audioVoice: trimmed })
  } catch (error) {
    console.error('Error updating audio voice setting:', error)
    return NextResponse.json({ error: 'Failed to update settings' }, { status: 500 })
  }
})