feat: Puente Mayúscula-Minúscula + TTS SpeechSynthesis + E2E fixes
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { prisma } from "@/lib/db"
|
||||
import { randomUUID } from "crypto"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { deviceFingerprint, pairingCode, childId, action } = await request.json()
|
||||
|
||||
if (action === "generate") {
|
||||
if (!childId) {
|
||||
return NextResponse.json({ error: "childId required" }, { status: 400 })
|
||||
}
|
||||
const code = randomUUID().slice(0, 8).toUpperCase()
|
||||
await prisma.pendingPairing.create({
|
||||
data: {
|
||||
code,
|
||||
childId,
|
||||
expiresAt: new Date(Date.now() + 10 * 60 * 1000),
|
||||
},
|
||||
})
|
||||
return NextResponse.json({ pairingCode: code })
|
||||
}
|
||||
|
||||
if (pairingCode) {
|
||||
const pending = await prisma.pendingPairing.findUnique({
|
||||
where: { code: pairingCode },
|
||||
})
|
||||
if (!pending || pending.expiresAt < new Date()) {
|
||||
return NextResponse.json({ error: "Código inválido o expirado" }, { status: 404 })
|
||||
}
|
||||
|
||||
const targetChildId = childId || pending.childId
|
||||
if (!targetChildId) {
|
||||
return NextResponse.json({ error: "Sin niño asociado" }, { status: 400 })
|
||||
}
|
||||
|
||||
if (childId && !pending.childId) {
|
||||
await prisma.pendingPairing.update({
|
||||
where: { id: pending.id },
|
||||
data: { childId },
|
||||
})
|
||||
}
|
||||
|
||||
const fp = deviceFingerprint || pending.deviceFingerprint
|
||||
if (!fp) {
|
||||
return NextResponse.json({ error: "Se requiere deviceFingerprint" }, { status: 400 })
|
||||
}
|
||||
|
||||
const device = await prisma.device.create({
|
||||
data: {
|
||||
childId: targetChildId,
|
||||
deviceFingerprint: fp,
|
||||
role: "child",
|
||||
},
|
||||
include: { child: true },
|
||||
})
|
||||
|
||||
await prisma.pendingPairing.delete({ where: { id: pending.id } })
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
childId: device.childId,
|
||||
childName: device.child.name,
|
||||
})
|
||||
}
|
||||
|
||||
if (deviceFingerprint) {
|
||||
const existingDevice = await prisma.device.findFirst({
|
||||
where: { deviceFingerprint },
|
||||
})
|
||||
if (existingDevice) {
|
||||
return NextResponse.json({
|
||||
paired: true,
|
||||
childId: existingDevice.childId,
|
||||
role: existingDevice.role,
|
||||
})
|
||||
}
|
||||
|
||||
const code = randomUUID().slice(0, 8).toUpperCase()
|
||||
await prisma.pendingPairing.create({
|
||||
data: {
|
||||
code,
|
||||
deviceFingerprint,
|
||||
expiresAt: new Date(Date.now() + 10 * 60 * 1000),
|
||||
},
|
||||
})
|
||||
return NextResponse.json({
|
||||
paired: false,
|
||||
pairingCode: code,
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: "Invalid request" }, { status: 400 })
|
||||
} catch (error) {
|
||||
console.error("Pairing error:", error)
|
||||
return NextResponse.json({ error: "Internal error" }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user