The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
improve revenue calculation accuracy
Steven Teyc8045972e81e
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.tsxindex c53afcb922..cd2e510938 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@@ -7,6 +7,7 @@ import { FilterButtonTableRow } from "@/ui/shared/filter-button-table-row";import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";import { InvoiceStatus } from "@dub/prisma/client";import {+ Badge,Button,Filter,StatusBadge,@@ -26,7 +27,7 @@ import {} from "@dub/utils";import NumberFlow from "@number-flow/react";import Link from "next/link";-import { Fragment, Suspense, useCallback, useMemo, useState } from "react";+import { Fragment, Suspense, useCallback, useMemo } from "react";import useSWR from "swr";interface TimeseriesData {@@ -47,11 +48,29 @@ interface InvoiceData {total: number;}-type Tab = {- id: "payouts" | "fees" | "total";- label: string;- colorClassName: string;-};+const payoutTabs = [+ {+ id: "payouts",+ label: "Payouts",+ colorClassName: "text-blue-500/50 bg-blue-500/50 border-blue-500",+ },+ {+ id: "fees",+ label: "Fees",+ colorClassName: "text-red-500/50 bg-red-500/50 border-red-500",+ },+ {+ id: "total",+ label: "Total",+ colorClassName: "text-green-500/50 bg-green-500/50 border-green-500",+ },+] as const;++type PayoutTab = (typeof payoutTabs)[number]["id"];++function isPayoutTab(value: string): value is PayoutTab {+ return payoutTabs.some((tab) => tab.id === value);+}export default function PayoutsPage() {return (@@ -63,7 +82,8 @@ export default function PayoutsPage() {function PayoutsPageClient() {const { queryParams, getQueryString, searchParamsObj } = useRouterStuff();- const { interval, start, end, status, programId } = searchParamsObj;+ const { interval, start, end, status, programId, tab } = searchParamsObj;+ const selectedTab = isPayoutTab(tab) ? tab : "payouts";const { data: { invoices, timeseriesData } = {}, isLoading } = useSWR<{invoices: InvoiceData[];@@ -72,6 +92,41 @@ function PayoutsPageClient() {keepPreviousData: true,});+ const previousPeriodQueryString = useMemo(() => {+ if (!timeseriesData || timeseriesData.length === 0) {+ return null;+ }++ const currentStart = new Date(timeseriesData[0].date);+ const currentEnd = new Date(timeseriesData[timeseriesData.length - 1].date);+ const periodDurationMs = currentEnd.getTime() - currentStart.getTime();+ const previousEnd = new Date(currentStart.getTime() - 1);+ const previousStart = new Date(previousEnd.getTime() - periodDurationMs);++ const toDateParam = (date: Date) => date.toISOString().slice(0, 10);++ return getQueryString({+ start: toDateParam(previousStart),+ end: toDateParam(previousEnd),+ });+ }, [getQueryString, timeseriesData]);++ const {+ data: { timeseriesData: previousPeriodTimeseriesData } = {},+ isLoading: isPreviousPeriodLoading,+ } = useSWR<{+ invoices: InvoiceData[];+ timeseriesData: TimeseriesData[];+ }>(+ previousPeriodQueryString+ ? `/api/admin/payouts${previousPeriodQueryString}`+ : null,+ fetcher,+ {+ keepPreviousData: true,+ },+ );+// Extract unique programs from invoicesconst programs = useMemo(() => {if (!invoices) return [];@@ -177,28 +232,8 @@ function PayoutsPageClient() {[queryParams],);- const tabs: Tab[] = [- {- id: "payouts",- label: "Payouts",- colorClassName: "text-blue-500/50 bg-blue-500/50 border-blue-500",- },- {- id: "fees",- label: "Fees",- colorClassName: "text-red-500/50 bg-red-500/50 border-red-500",- },- {- id: "total",- label: "Total",- colorClassName: "text-green-500/50 bg-green-500/50 border-green-500",- },- ];-- const [selectedTab, setSelectedTab] = useState<"payouts" | "fees" | "total">(- "payouts",- );- const tab = tabs.find(({ id }) => id === selectedTab) ?? tabs[0];+ const tabs = payoutTabs;+ const activeTab = tabs.find(({ id }) => id === selectedTab) ?? tabs[0];// take the last 12 monthsconst chartData =@@ -225,6 +260,43 @@ function PayoutsPageClient() {};}, [timeseriesData]);+ const percentChanges = useMemo(() => {+ const getPeriodTotal = (series: TimeseriesData[], key: PayoutTab) =>+ series.reduce((acc, point) => acc + point[key], 0);++ const getPercentChange = (key: PayoutTab) => {+ if (+ !timeseriesData ||+ timeseriesData.length === 0 ||+ isPreviousPeriodLoading+ ) {+ return 0;+ }++ if (+ !previousPeriodTimeseriesData ||+ previousPeriodTimeseriesData.length === 0+ ) {+ return 0;+ }++ const previousTotal = getPeriodTotal(previousPeriodTimeseriesData, key);+ const currentTotal = getPeriodTotal(timeseriesData, key);++ if (previousTotal === 0) {+ return currentTotal === 0 ? 0 : 100;+ }++ return (currentTotal / previousTotal - 1) * 100;+ };++ return {+ payouts: getPercentChange("payouts"),+ fees: getPercentChange("fees"),+ total: getPercentChange("total"),+ };+ }, [isPreviousPeriodLoading, previousPeriodTimeseriesData, timeseriesData]);+const { pagination, setPagination } = usePagination();const { table, ...tableProps } = useTable({@@ -370,7 +442,6 @@ function PayoutsPageClient() {<buttonkey={id}onClick={() => {- setSelectedTab(id);queryParams({set: { tab: id },});@@ -396,16 +467,33 @@ function PayoutsPageClient() {</div><div className="mt-1 flex h-12 items-center">{(totals[id] || totals[id] === 0) && !isLoading ? (- <NumberFlow- value={(totals[id] ?? 0) / 100}- className="text-xl font-medium sm:text-3xl"- format={{- style: "currency",- currency: "USD",- // @ts-ignore – trailingZeroDisplay is a valid option but TS is outdated- trailingZeroDisplay: "stripIfInteger",- }}- />+ <div className="flex items-center gap-2">+ <NumberFlow+ value={(totals[id] ?? 0) / 100}+ className="text-xl font-medium sm:text-3xl"+ format={{+ style: "currency",+ currency: "USD",+ // @ts-ignore – trailingZeroDisplay is a valid option but TS is outdated+ trailingZeroDisplay: "stripIfInteger",+ }}+ />+ {percentChanges[id] ? (+ <Badge+ variant={+ percentChanges[id] >= 0+ ? "green"+ : percentChanges[id] < 0+ ? "red"+ : "neutral"+ }+ className="rounded-md px-2 py-1 text-xs"+ >+ {percentChanges[id] >= 0 ? "+" : ""}+ {percentChanges[id].toFixed(1)}%+ </Badge>+ ) : null}+ </div>) : (<div className="h-10 w-24 animate-pulse rounded-md bg-neutral-200" />)}@@ -425,7 +513,7 @@ function PayoutsPageClient() {id: "value",valueAccessor: (d) => d.values.value,isActive: true,- colorClassName: tab.colorClassName,+ colorClassName: activeTab.colorClassName,},]}tooltipClassName="p-0"@@ -445,11 +533,11 @@ function PayoutsPageClient() {<divclassName={cn("h-2 w-2 rounded-sm shadow-[inset_0_0_0_1px_#0003]",- tab.colorClassName,+ activeTab.colorClassName,)}/><p className="capitalize text-neutral-600">- {tab.label}+ {activeTab.label}</p></div><p className="text-right font-medium text-neutral-900">diff --git a/apps/web/app/(ee)/api/admin/payouts/get-payouts-timeseries.ts b/apps/web/app/(ee)/api/admin/payouts/get-payouts-timeseries.tsindex 5d6d2c1372..5256f4931e 100644--- a/apps/web/app/(ee)/api/admin/payouts/get-payouts-timeseries.ts+++ b/apps/web/app/(ee)/api/admin/payouts/get-payouts-timeseries.ts@@ -24,6 +24,7 @@ export async function getPayoutsTimeseries({endDate,granularity,timezone = "UTC",+ excludeCreditCardFees = false,}: {programId?: string;status?: InvoiceStatus;@@ -31,6 +32,7 @@ export async function getPayoutsTimeseries({endDate: Date;granularity: keyof typeof sqlGranularityMap;timezone?: string;+ excludeCreditCardFees?: boolean;}) {const { dateFormat, dateIncrement, startFunction, formatString } =sqlGranularityMap[granularity];@@ -63,11 +65,31 @@ export async function getPayoutsTimeseries({fees: number;total: number;}>(+ // Optionally remove card processing (2.9%) from invoice fees.+ // amount and fee are stored in cents, so we round the computed deduction.`SELECTDATE_FORMAT(CONVERT_TZ(createdAt, "UTC", ?), ?) as date,SUM(amount) as payouts,- SUM(fee) as fees,- SUM(total) as total+ SUM(+ ${+ excludeCreditCardFees+ ? `CASE+ WHEN paymentMethod = 'card' THEN fee - ROUND(amount * 0.029)+ ELSE fee+ END`+ : "fee"+ }+ ) as fees,+ SUM(+ amount + ${+ excludeCreditCardFees+ ? `CASE+ WHEN paymentMethod = 'card' THEN fee - ROUND(amount * 0.029)+ ELSE fee+ END`+ : "fee"+ }+ ) as totalFROM InvoiceWHERE ${whereClauses.join(" AND ")}GROUP BY DATE_FORMAT(CONVERT_TZ(createdAt, "UTC", ?), ?)diff --git a/apps/web/app/(ee)/api/admin/revenue/route.ts b/apps/web/app/(ee)/api/admin/revenue/route.tsindex 197ee6317f..b4c46cc6b1 100644--- a/apps/web/app/(ee)/api/admin/revenue/route.ts+++ b/apps/web/app/(ee)/api/admin/revenue/route.ts@@ -48,6 +48,7 @@ export const GET = withAdmin(async ({ searchParams }) => {endDate,granularity: revenueGranularity,timezone,+ excludeCreditCardFees: true,}),]);