Add src/app/api/orders/route.ts
This commit is contained in:
136
src/app/api/orders/route.ts
Normal file
136
src/app/api/orders/route.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
interface OrderData {
|
||||
fullName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
address: string;
|
||||
city: string;
|
||||
postalCode: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
// Mock in-memory database for demonstration
|
||||
const orders: OrderData[] = [];
|
||||
|
||||
// Telegram Bot configuration
|
||||
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN || "";
|
||||
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID || "";
|
||||
|
||||
async function sendTelegramNotification(order: OrderData) {
|
||||
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
|
||||
console.warn(
|
||||
"Telegram credentials not configured. Skipping notification."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const message = `
|
||||
🌸 *YANGI BUYURTMA* 🌸
|
||||
|
||||
👤 *Ism:* ${order.fullName}
|
||||
📧 *Email:* ${order.email}
|
||||
📞 *Telefon:* ${order.phone}
|
||||
|
||||
📍 *Manzil:* ${order.address}, ${order.city}
|
||||
📮 *Postal kod:* ${order.postalCode || "Berilmagan"}
|
||||
|
||||
💬 *Xabar:* ${order.message || "Xabar berilmagan"}
|
||||
|
||||
⏰ *Vaqti:* ${new Date().toLocaleString("uz-UZ")}
|
||||
`;
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`,
|
||||
{
|
||||
method: "POST", headers: {
|
||||
"Content-Type": "application/json"},
|
||||
body: JSON.stringify({
|
||||
chat_id: TELEGRAM_CHAT_ID,
|
||||
text: message,
|
||||
parse_mode: "Markdown"}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Telegram API error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
console.log("Telegram notification sent successfully");
|
||||
} catch (error) {
|
||||
console.error("Failed to send Telegram notification:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body: OrderData = await request.json();
|
||||
|
||||
// Validate required fields
|
||||
if (
|
||||
!body.fullName ||
|
||||
!body.email ||
|
||||
!body.phone ||
|
||||
!body.address ||
|
||||
!body.city
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing required fields" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Add timestamp and ID
|
||||
const orderWithMetadata = {
|
||||
...body,
|
||||
id: Date.now().toString(),
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Save to mock database
|
||||
orders.push(body);
|
||||
console.log("Order saved to database:", orderWithMetadata);
|
||||
|
||||
// Send Telegram notification
|
||||
await sendTelegramNotification(body);
|
||||
|
||||
// Return success response
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Buyurtma muvaffaqiyatli qabul qilindi", orderId: orderWithMetadata.id,
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error processing order:", error);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: "Buyurtmani qayta ishlashda xatolik yuz berdi"},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
return NextResponse.json(
|
||||
{
|
||||
orders,
|
||||
total: orders.length,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error fetching orders:", error);
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: "Buyurtmalarni olishda xatolik yuz berdi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user