Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add pagination to admin payouts page

Steven Teyd36a19045a3f
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/payouts/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/payouts/page.tsx
index c339b466df..e973d9420a 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/payouts/page.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/payouts/page.tsx
@@ -85,12 +85,16 @@ function PayoutsPageClient() {
const { interval, start, end, status, programId, tab } = searchParamsObj;
const selectedTab = isPayoutTab(tab) ? tab : "payouts";
- const { data: { invoices, timeseriesData } = {}, isLoading } = useSWR<{
- invoices: InvoiceData[];
- timeseriesData: TimeseriesData[];
- }>(`/api/admin/payouts${getQueryString()}`, fetcher, {
- keepPreviousData: true,
- });
+ const { pagination, setPagination } = usePagination();
+
+ const { data: { invoices, timeseriesData, totalInvoices } = {}, isLoading } =
+ useSWR<{
+ invoices: InvoiceData[];
+ timeseriesData: TimeseriesData[];
+ totalInvoices: number;
+ }>(`/api/admin/payouts${getQueryString()}`, fetcher, {
+ keepPreviousData: true,
+ });
const previousPeriodQueryString = useMemo(() => {
if (!timeseriesData || timeseriesData.length === 0) {
@@ -227,7 +231,7 @@ function PayoutsPageClient() {
const onRemoveAll = useCallback(
() =>
queryParams({
- del: ["status", "programId"],
+ del: ["status", "programId", "page"],
}),
[queryParams],
);
@@ -297,8 +301,6 @@ function PayoutsPageClient() {
};
}, [isPreviousPeriodLoading, previousPeriodTimeseriesData, timeseriesData]);
- const { pagination, setPagination } = usePagination();
-
const { table, ...tableProps } = useTable({
data: invoices ?? [],
columns: [
@@ -376,7 +378,7 @@ function PayoutsPageClient() {
pagination,
onPaginationChange: setPagination,
resourceName: (plural) => `invoice${plural ? "s" : ""}`,
- rowCount: invoices?.length ?? 0,
+ rowCount: totalInvoices ?? 0,
loading: isLoading,
cellRight: (cell) => {
const meta = cell.column.columnDef.meta as
diff --git a/apps/web/app/(ee)/api/admin/payouts/route.ts b/apps/web/app/(ee)/api/admin/payouts/route.ts
index df2befcdd8..25a0ac079e 100644
--- a/apps/web/app/(ee)/api/admin/payouts/route.ts
+++ b/apps/web/app/(ee)/api/admin/payouts/route.ts
@@ -2,8 +2,9 @@ import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { withAdmin } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getPaginationQuerySchema } from "@/lib/zod/schemas/misc";
import { ACME_PROGRAM_ID } from "@dub/utils";
-import { InvoiceStatus } from "@prisma/client";
+import { InvoiceStatus, Prisma } from "@prisma/client";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
import { getPayoutsTimeseries } from "./get-payouts-timeseries";
@@ -15,7 +16,8 @@ const adminPayoutsQuerySchema = z
})
.extend(
analyticsQuerySchema.pick({ interval: true, start: true, end: true }).shape,
- );
+ )
+ .extend(getPaginationQuerySchema({ pageSize: 100 }));
export const GET = withAdmin(async ({ searchParams }) => {
const {
@@ -24,6 +26,8 @@ export const GET = withAdmin(async ({ searchParams }) => {
interval = "mtd",
start,
end,
+ page,
+ pageSize,
} = adminPayoutsQuerySchema.parse(searchParams);
const timezone = "UTC";
@@ -34,45 +38,53 @@ export const GET = withAdmin(async ({ searchParams }) => {
timezone,
});
- // Fetch invoices
- const invoices = await prisma.invoice.findMany({
- where: {
- ...(programId
- ? { programId }
- : {
- AND: [
- {
- programId: {
- not: ACME_PROGRAM_ID,
- },
+ const invoiceWhere: Prisma.InvoiceWhereInput = {
+ ...(programId
+ ? { programId }
+ : {
+ AND: [
+ {
+ programId: {
+ not: ACME_PROGRAM_ID,
},
- {
- program: {
- isNot: null,
- },
+ },
+ {
+ program: {
+ isNot: null,
},
- ],
- }),
- status: status || {
- not: "failed",
- },
- createdAt: {
- gte: startDate,
- lte: endDate,
- },
+ },
+ ],
+ }),
+ status: status || {
+ not: "failed",
},
- include: {
- program: {
- select: {
- name: true,
- logo: true,
+ createdAt: {
+ gte: startDate,
+ lte: endDate,
+ },
+ };
+
+ const [invoices, totalInvoices] = await Promise.all([
+ prisma.invoice.findMany({
+ where: invoiceWhere,
+ include: {
+ program: {
+ select: {
+ name: true,
+ logo: true,
+ },
},
},
- },
- orderBy: {
- createdAt: "desc",
- },
- });
+ orderBy: {
+ createdAt: "desc",
+ },
+ skip: ((page ?? 1) - 1) * pageSize,
+ take: pageSize,
+ }),
+ prisma.invoice.count({
+ where: invoiceWhere,
+ }),
+ ]);
const timeseriesData = await getPayoutsTimeseries({
programId,
@@ -98,5 +110,6 @@ export const GET = withAdmin(async ({ searchParams }) => {
return NextResponse.json({
invoices: formattedInvoices,
timeseriesData,
+ totalInvoices,
});
});