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.

address CR feedback, improve admin/payouts

Steven Teyd52438d99c10
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 e973d9420a..29a27798c0 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
@@ -48,6 +48,13 @@ interface InvoiceData {
total: number;
}
+interface TopProgramData {
+ id: string;
+ name: string;
+ logo: string | null;
+ payouts: number;
+}
+
const payoutTabs = [
{
id: "payouts",
@@ -87,14 +94,17 @@ function PayoutsPageClient() {
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 {
+ data: { invoices, timeseriesData, totalInvoices, programs } = {},
+ isLoading,
+ } = useSWR<{
+ invoices: InvoiceData[];
+ timeseriesData: TimeseriesData[];
+ totalInvoices: number;
+ programs: TopProgramData[];
+ }>(`/api/admin/payouts${getQueryString()}`, fetcher, {
+ keepPreviousData: true,
+ });
const previousPeriodQueryString = useMemo(() => {
if (!timeseriesData || timeseriesData.length === 0) {
@@ -131,27 +141,6 @@ function PayoutsPageClient() {
},
);
- // Extract unique programs from invoices
- const programs = useMemo(() => {
- if (!invoices) return [];
- const programMap = new Map<
- string,
- { id: string; name: string; logo: string }
- >();
- invoices.forEach((invoice) => {
- if (!programMap.has(invoice.programId)) {
- programMap.set(invoice.programId, {
- id: invoice.programId,
- name: invoice.programName,
- logo: invoice.programLogo,
- });
- }
- });
- return Array.from(programMap.values()).sort((a, b) =>
- a.name.localeCompare(b.name),
- );
- }, [invoices]);
-
// Filter configuration
const filters = useMemo(
() => [
@@ -160,7 +149,7 @@ function PayoutsPageClient() {
icon: GridIcon,
label: "Program",
options:
- programs.map((program) => ({
+ programs?.map((program) => ({
value: program.id,
label: program.name,
icon: (
@@ -170,6 +159,7 @@ function PayoutsPageClient() {
className="size-4 rounded-full"
/>
),
+ right: currencyFormatter(program.payouts),
})) ?? null,
},
{
diff --git a/apps/web/app/(ee)/api/admin/payouts/get-top-programs-by-payouts.ts b/apps/web/app/(ee)/api/admin/payouts/get-top-programs-by-payouts.ts
new file mode 100644
index 0000000000..3d4c954b1b
--- /dev/null
+++ b/apps/web/app/(ee)/api/admin/payouts/get-top-programs-by-payouts.ts
@@ -0,0 +1,78 @@
+import { prisma } from "@/lib/prisma";
+import { ACME_PROGRAM_ID, DEMO_PROGRAM_ID } from "@dub/utils";
+import { InvoiceStatus } from "@prisma/client";
+
+export async function getTopProgramsByPayouts({
+ status,
+ startDate,
+ endDate,
+}: {
+ status?: InvoiceStatus;
+ startDate: Date;
+ endDate: Date;
+}) {
+ const programPayouts = await prisma.invoice.groupBy({
+ by: ["programId"],
+ _sum: {
+ amount: true,
+ },
+ where: {
+ programId: {
+ not: null,
+ notIn: [ACME_PROGRAM_ID, DEMO_PROGRAM_ID],
+ },
+ program: {
+ is: {
+ NOT: {
+ slug: {
+ endsWith: "-staging",
+ },
+ },
+ },
+ },
+ status: status || {
+ not: "failed",
+ },
+ createdAt: {
+ gte: startDate,
+ lte: endDate,
+ },
+ },
+ orderBy: {
+ _sum: {
+ amount: "desc",
+ },
+ },
+ take: 250,
+ });
+
+ const programs = await prisma.program.findMany({
+ where: {
+ id: {
+ in: programPayouts
+ .map(({ programId }) => programId)
+ .filter((id): id is string => Boolean(id)),
+ },
+ },
+ select: {
+ id: true,
+ name: true,
+ logo: true,
+ },
+ });
+
+ const programIdMap = Object.fromEntries(
+ programs.map((program) => [program.id, program]),
+ );
+
+ return programPayouts.flatMap(({ programId, _sum }) => {
+ const program = programId ? programIdMap[programId] : null;
+ if (!program) return [];
+ return [
+ {
+ ...program,
+ payouts: _sum.amount || 0,
+ },
+ ];
+ });
+}
diff --git a/apps/web/app/(ee)/api/admin/payouts/route.ts b/apps/web/app/(ee)/api/admin/payouts/route.ts
index 73a891ba51..2ece37d528 100644
--- a/apps/web/app/(ee)/api/admin/payouts/route.ts
+++ b/apps/web/app/(ee)/api/admin/payouts/route.ts
@@ -8,6 +8,7 @@ import { InvoiceStatus, Prisma } from "@prisma/client";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
import { getPayoutsTimeseries } from "./get-payouts-timeseries";
+import { getTopProgramsByPayouts } from "./get-top-programs-by-payouts";
const adminPayoutsQuerySchema = z
.object({
@@ -71,7 +72,7 @@ export const GET = withAdmin(async ({ searchParams }) => {
},
};
- const [invoices, totalInvoices] = await Promise.all([
+ const [invoices, totalInvoices, timeseriesData, programs] = await Promise.all([
prisma.invoice.findMany({
where: invoiceWhere,
include: {
@@ -91,17 +92,18 @@ export const GET = withAdmin(async ({ searchParams }) => {
prisma.invoice.count({
where: invoiceWhere,
}),
+ getPayoutsTimeseries({
+ programId,
+ status,
+ startDate,
+ endDate,
+ granularity,
+ timezone,
+ }),
+ // not scoped to programId so the program filter always lists all top programs
+ getTopProgramsByPayouts({ status, startDate, endDate }),
]);
- const timeseriesData = await getPayoutsTimeseries({
- programId,
- status,
- startDate,
- endDate,
- granularity,
- timezone,
- });
-
const formattedInvoices = invoices.map((invoice) => ({
date: invoice.createdAt,
// we're coercing this cause we've filtered out invoices without a programId above
@@ -118,5 +120,6 @@ export const GET = withAdmin(async ({ searchParams }) => {
invoices: formattedInvoices,
timeseriesData,
totalInvoices,
+ programs,
});
});
diff --git a/apps/web/app/[domain]/page.tsx b/apps/web/app/[domain]/page.tsx
index ee340b2ad9..94b798631a 100644
--- a/apps/web/app/[domain]/page.tsx
+++ b/apps/web/app/[domain]/page.tsx
@@ -57,8 +57,8 @@ export default function CustomDomainPage() {
"animate-slide-up-fade motion-reduce:animate-fade-in [--offset:10px] [animation-delay:200ms] [animation-duration:1s] [animation-fill-mode:both]",
)}
>
- This custom domain is powered by Dub &ndash; the link management
- platform designed for modern marketing teams.
+ This custom domain is powered by Dub &ndash; the link attribution
+ platform for modern marketing teams.
</p>
</div>
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index c453d76ae8..22f0f13b88 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -540,7 +540,13 @@ export const authOptions: NextAuthOptions = {
trustedOrigins.find((origin) => new URL(origin).host === host) ??
baseUrl;
- const { origin, href } = new URL(url, base);
+ let resolved: URL;
+ try {
+ resolved = new URL(url, base);
+ } catch {
+ return base;
+ }
+ const { origin, href } = resolved;
return trustedOrigins.includes(origin) ? href : base;
},
jwt: async ({