53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { db } from "@/lib/db"
|
|
import { NextResponse } from "next/server";
|
|
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
|
import voices from "@/data/tts";
|
|
|
|
export async function GET(req: Request) {
|
|
try {
|
|
if (!voices) {
|
|
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
|
|
}
|
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
|
}
|
|
|
|
return NextResponse.json(user.ttsDefaultVoice);
|
|
} catch (error) {
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
if (!voices) {
|
|
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
|
|
}
|
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
|
}
|
|
|
|
const { voice } = await req.json();
|
|
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase()))
|
|
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
|
|
|
|
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
|
|
|
|
await db.user.update({
|
|
where: {
|
|
id: user.id
|
|
},
|
|
data: {
|
|
ttsDefaultVoice: v
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
|
|
} catch (error) {
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
|
}
|
|
} |