2024-01-04 02:14:11 -05:00
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
2024-01-04 16:57:32 -05:00
|
|
|
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
2024-01-04 02:14:11 -05:00
|
|
|
import voices from "@/data/tts";
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
2024-06-24 18:16:55 -04:00
|
|
|
if (!voices) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
|
2024-06-24 18:16:55 -04:00
|
|
|
}
|
2024-08-25 17:35:46 -04:00
|
|
|
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
2024-01-04 02:14:11 -05:00
|
|
|
if (!user) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2024-01-04 02:14:11 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
return NextResponse.json(user.ttsDefaultVoice);
|
2024-01-04 02:14:11 -05:00
|
|
|
} catch (error) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2024-01-04 02:14:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function POST(req: Request) {
|
|
|
|
try {
|
2024-06-24 18:16:55 -04:00
|
|
|
if (!voices) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
|
2024-06-24 18:16:55 -04:00
|
|
|
}
|
|
|
|
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
2024-01-04 02:14:11 -05:00
|
|
|
if (!user) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2024-01-04 02:14:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const { voice } = await req.json();
|
2024-08-25 17:35:46 -04:00
|
|
|
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase()))
|
|
|
|
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
|
2024-01-04 02:14:11 -05:00
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
|
2024-01-04 02:14:11 -05:00
|
|
|
|
|
|
|
await db.user.update({
|
2024-08-25 17:35:46 -04:00
|
|
|
where: {
|
|
|
|
id: user.id
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
ttsDefaultVoice: v
|
|
|
|
}
|
2024-01-04 02:14:11 -05:00
|
|
|
});
|
|
|
|
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
|
2024-01-04 02:14:11 -05:00
|
|
|
} catch (error) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2024-01-04 02:14:11 -05:00
|
|
|
}
|
|
|
|
}
|