2024-08-26 13:11:47 +02:00
|
|
|
import { NextResponse } from "next/server";
|
2024-09-06 19:50:52 +02:00
|
|
|
import clientPromise from "@/lib/db";
|
2024-08-26 13:11:47 +02:00
|
|
|
|
|
|
|
interface Subscriber {
|
2024-09-07 09:12:48 +02:00
|
|
|
email: string;
|
|
|
|
subscribedAt: Date;
|
2024-08-26 13:11:47 +02:00
|
|
|
}
|
|
|
|
|
2024-09-06 19:50:52 +02:00
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
2024-08-26 13:11:47 +02:00
|
|
|
export async function GET(req: Request) {
|
2024-09-07 09:12:48 +02:00
|
|
|
try {
|
|
|
|
const url = new URL(req.url);
|
|
|
|
const page = parseInt(url.searchParams.get("page") || "1", 10);
|
|
|
|
const limit = 10;
|
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
|
|
|
|
const client = await clientPromise;
|
|
|
|
const db = client.db("newsletter");
|
|
|
|
const collection = db.collection("subscribers");
|
|
|
|
|
|
|
|
// Pagination
|
|
|
|
const documents = await collection.find().skip(skip).limit(limit).toArray();
|
|
|
|
|
|
|
|
const subscribers: Subscriber[] = documents.map((doc) => ({
|
|
|
|
email: doc.email,
|
|
|
|
subscribedAt:
|
|
|
|
doc.subscribedAt instanceof Date
|
|
|
|
? doc.subscribedAt
|
|
|
|
: new Date(doc.subscribedAt)
|
|
|
|
}));
|
|
|
|
|
|
|
|
const totalSubscribers = await collection.countDocuments();
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
subscribers,
|
|
|
|
totalSubscribers,
|
|
|
|
totalPages: Math.ceil(totalSubscribers / limit)
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching subscribers:", error);
|
|
|
|
return new NextResponse("Internal Server Error", { status: 500 });
|
|
|
|
}
|
2024-08-26 13:11:47 +02:00
|
|
|
}
|