77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
export interface Notification {
|
|
id: string;
|
|
title: string;
|
|
message: string;
|
|
type: "whale" | "weather" | "excursion" | "info";
|
|
timestamp: string;
|
|
read: boolean;
|
|
}
|
|
|
|
// Notifications stockées (en production, utiliser une base de données)
|
|
let notifications: Notification[] = [
|
|
{
|
|
id: "1",
|
|
title: "Observation de baleines",
|
|
message: "Les baleines ont été vues au nord de l'île ce matin !",
|
|
type: "whale",
|
|
timestamp: new Date().toISOString(),
|
|
read: false,
|
|
},
|
|
];
|
|
|
|
export async function GET() {
|
|
return NextResponse.json(notifications);
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json();
|
|
const { title, message, type } = body;
|
|
|
|
const notification: Notification = {
|
|
id: `notif-${Date.now()}`,
|
|
title,
|
|
message,
|
|
type: type || "info",
|
|
timestamp: new Date().toISOString(),
|
|
read: false,
|
|
};
|
|
|
|
notifications.unshift(notification);
|
|
|
|
// Limiter à 50 notifications
|
|
if (notifications.length > 50) {
|
|
notifications = notifications.slice(0, 50);
|
|
}
|
|
|
|
return NextResponse.json({ success: true, notification });
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ success: false, message: "Erreur lors de la création de la notification" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request: Request) {
|
|
try {
|
|
const body = await request.json();
|
|
const { id, read } = body;
|
|
|
|
const notification = notifications.find((n) => n.id === id);
|
|
if (notification) {
|
|
notification.read = read;
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ success: false, message: "Erreur lors de la mise à jour" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|
|
|