2025-06-09 11:29:54 +03:30
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { writeFile, readFile } from 'fs/promises';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
|
|
const filePath = path.resolve(process.cwd(), 'contactMessages.json');
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
const { first_name, last_name, phone, email, message } = body;
|
|
|
|
|
|
|
|
|
|
if (!first_name || !last_name || !phone || !email || !message) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'مقدار تمامی فیلدها را پر کنید' },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let messages = [];
|
|
|
|
|
try {
|
|
|
|
|
const fileData = await readFile(filePath, 'utf-8');
|
|
|
|
|
messages = JSON.parse(fileData);
|
|
|
|
|
} catch {
|
|
|
|
|
messages = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newMessage = {
|
2025-06-09 17:14:44 +03:30
|
|
|
first_name,
|
|
|
|
|
last_name,
|
|
|
|
|
phone,
|
2025-06-09 11:29:54 +03:30
|
|
|
email,
|
|
|
|
|
message,
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
messages.push(newMessage);
|
|
|
|
|
|
|
|
|
|
// Write updated messages back to file
|
|
|
|
|
await writeFile(filePath, JSON.stringify(messages, null, 2), 'utf-8');
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: 'Your message has been received.',
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error, message: 'مشکلی پیش آمده.' },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|