64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { getPrisma } from "../../../lib/db";
|
|
import { ok, fail } from "../../../lib/http";
|
|
|
|
const ContactSchema = z.object({
|
|
firstName: z.string().min(1, "First name is required"),
|
|
lastName: z.string().min(1, "Last name is required"),
|
|
email: z.string().email("Invalid email address"),
|
|
subject: z.string().min(1, "Subject is required"),
|
|
message: z.string().min(10, "Message must be at least 10 characters"),
|
|
});
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const parsed = ContactSchema.safeParse(await req.json().catch(() => ({})));
|
|
if (!parsed.success) {
|
|
console.error("[CONTACT] Validation error:", parsed.error);
|
|
const firstError = parsed.error.errors[0];
|
|
return NextResponse.json(
|
|
{ ok: false, message: firstError.message },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { firstName, lastName, email, subject, message } = parsed.data;
|
|
const name = `${firstName} ${lastName}`;
|
|
console.log("[CONTACT] New message from:", email);
|
|
|
|
try {
|
|
const prisma = await getPrisma();
|
|
if (!prisma) {
|
|
console.error("[CONTACT] Database not configured");
|
|
return NextResponse.json(
|
|
{ ok: false, message: "Service temporarily unavailable. Please try again later." },
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
|
|
// Store contact message in database
|
|
await prisma.contactMessage.create({
|
|
data: {
|
|
name,
|
|
email,
|
|
subject,
|
|
message,
|
|
status: "NEW",
|
|
},
|
|
});
|
|
|
|
console.log("[CONTACT] Message saved from:", email);
|
|
|
|
// TODO: Send email notification to admin
|
|
// TODO: Send confirmation email to user
|
|
|
|
return ok({ message: "Message received. We'll get back to you soon." });
|
|
} catch (error) {
|
|
console.error("[CONTACT] Error saving message:", error);
|
|
return NextResponse.json(
|
|
{ ok: false, message: "Failed to send message. Please try again." },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|