import { endOfMonth, startOfMonth } from "date-fns"; import { between, desc, getTableColumns, sql, sum } from "drizzle-orm"; import type { Metadata } from "next"; import { redirect } from "next/navigation"; import { adminCheck } from "@/lib/auth"; import { db } from "@/lib/db"; import { donations } from "@/lib/db/schema"; import { DonateAdminPage } from "./client"; export default async function () { if (!(await adminCheck())) redirect(`/login?next=${encodeURIComponent("/donate/admin")}`); const now = new Date(); const [data, [stats]] = await Promise.all([ db .select({ ...getTableColumns(donations), image: sql`${donations.image} IS NOT NULL`, }) .from(donations) .limit(100) .orderBy(desc(donations.id)), db .select({ total: sum(donations.amount).mapWith(Number), today: sql` COALESCE( SUM(${donations.amount}) FILTER ( WHERE ${donations.created} >= date_trunc('day', NOW()) ), 0 ) `, }) .from(donations) .where(between(donations.created, startOfMonth(now), endOfMonth(now))) .limit(1), ]); return ; } export const metadata: Metadata = { title: "โดเนททั้งหมด", description: "ดูโดเนททั้งหมด", }; export const dynamic = "force-dynamic";