53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { CATEGORIES } from "@/lib/categories";
|
|
|
|
type PromptPayload = {
|
|
title?: string;
|
|
answers?: Record<string, string>;
|
|
};
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = (await request.json()) as PromptPayload;
|
|
const answers = body.answers ?? {};
|
|
|
|
if (!answers || typeof answers !== "object") {
|
|
return NextResponse.json(
|
|
{ error: "Campo 'answers' é obrigatório." },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const title =
|
|
body.title?.trim() ||
|
|
answers.perfil?.trim() ||
|
|
"Prompt de Hotelaria";
|
|
|
|
const categoriesData = CATEGORIES.map((category) => ({
|
|
categoryKey: category.id,
|
|
content: answers[category.id]?.trim() || ""
|
|
}));
|
|
|
|
const prompt = await prisma.prompt.create({
|
|
data: {
|
|
title,
|
|
categories: {
|
|
create: categoriesData
|
|
}
|
|
}
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{ id: prompt.id, title: prompt.title, createdAt: prompt.createdAt },
|
|
{ status: 201 }
|
|
);
|
|
} catch (error) {
|
|
console.error("POST /api/prompts error", error);
|
|
return NextResponse.json(
|
|
{ error: "Não foi possível salvar o prompt." },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|