Files
workparking/app/api/leads/route.ts
2026-04-17 16:10:29 +03:00

58 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
type LeadPayload = {
company?: string;
phone?: string;
message?: string;
};
export async function POST(request: Request) {
try {
const body = (await request.json()) as LeadPayload;
const company = body.company?.trim();
const phone = body.phone?.trim();
const message = body.message?.trim() || "";
if (!company || !phone) {
return NextResponse.json(
{ error: "Компания и телефон обязательны" },
{ status: 400 }
);
}
const lead = await prisma.lead.create({
data: {
company,
phone,
message,
source: "website",
},
});
return NextResponse.json({ success: true, leadId: lead.id }, { status: 201 });
} catch (error) {
console.error("POST /api/leads error:", error);
return NextResponse.json(
{ error: "Не удалось сохранить заявку" },
{ status: 500 }
);
}
}
export async function GET() {
try {
const leads = await prisma.lead.findMany({
orderBy: { createdAt: "desc" },
});
return NextResponse.json(leads);
} catch (error) {
console.error("GET /api/leads error:", error);
return NextResponse.json(
{ error: "Не удалось получить заявки" },
{ status: 500 }
);
}
}