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.

fix timezone + 0 values

Steven Teybeafbf3ce4a7
diff --git a/apps/web/app/admin.dub.co/(dashboard)/payouts/client.tsx b/apps/web/app/admin.dub.co/(dashboard)/payouts/client.tsx
index a21778f361..69f8493eeb 100644
--- a/apps/web/app/admin.dub.co/(dashboard)/payouts/client.tsx
+++ b/apps/web/app/admin.dub.co/(dashboard)/payouts/client.tsx
@@ -39,9 +39,15 @@ export default function PayoutsPageClient() {
const { data: { invoices, timeseriesData } = {}, isLoading } = useSWR<{
invoices: InvoiceData[];
timeseriesData: TimeseriesData[];
- }>(`/api/admin/payouts${getQueryString()}`, fetcher, {
- keepPreviousData: true,
- });
+ }>(
+ `/api/admin/payouts${getQueryString({
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ })}`,
+ fetcher,
+ {
+ keepPreviousData: true,
+ },
+ );
const tabs: Tab[] = [
{
diff --git a/apps/web/app/api/admin/payouts/route.ts b/apps/web/app/api/admin/payouts/route.ts
index 8f1d7be148..d7d52d3cd4 100644
--- a/apps/web/app/api/admin/payouts/route.ts
+++ b/apps/web/app/api/admin/payouts/route.ts
@@ -3,10 +3,20 @@ import { withAdmin } from "@/lib/auth";
import { sqlGranularityMap } from "@/lib/planetscale/granularity";
import { prisma } from "@dub/prisma";
import { ACME_WORKSPACE_ID } from "@dub/utils";
+import { DateTime } from "luxon";
import { NextResponse } from "next/server";
+interface TimeseriesPoint {
+ payouts: number;
+ fees: number;
+}
+
+interface FormattedTimeseriesPoint extends TimeseriesPoint {
+ date: Date;
+}
+
export const GET = withAdmin(async ({ searchParams }) => {
- const { interval = "mtd", start, end } = searchParams;
+ const { interval = "mtd", start, end, timezone = "UTC" } = searchParams;
const { startDate, endDate, granularity } = getStartEndDates({
interval,
@@ -32,17 +42,15 @@ export const GET = withAdmin(async ({ searchParams }) => {
},
});
- // log total fees
- console.log(invoices.reduce((acc, invoice) => acc + invoice.fee, 0));
-
- const { dateFormat } = sqlGranularityMap[granularity];
+ const { dateFormat, dateIncrement, startFunction, formatString } =
+ sqlGranularityMap[granularity];
// Calculate timeseries data for payouts and fees
const timeseriesData = await prisma.$queryRaw<
{ date: Date; payouts: number; fees: number }[]
>`
SELECT
- DATE_FORMAT(paidAt, ${dateFormat}) as date,
+ DATE_FORMAT(CONVERT_TZ(paidAt, "UTC", ${timezone}), ${dateFormat}) as date,
SUM(amount) as payouts,
SUM(fee) as fees
FROM Invoice
@@ -52,7 +60,7 @@ export const GET = withAdmin(async ({ searchParams }) => {
AND paidAt IS NOT NULL
AND paidAt >= ${startDate}
AND paidAt <= ${endDate}
- GROUP BY DATE_FORMAT(paidAt, ${dateFormat})
+ GROUP BY DATE_FORMAT(CONVERT_TZ(paidAt, "UTC", ${timezone}), ${dateFormat})
ORDER BY date ASC;
`;
@@ -65,11 +73,37 @@ export const GET = withAdmin(async ({ searchParams }) => {
total: invoice.total,
}));
- const formattedTimeseriesData = timeseriesData.map((point) => ({
- date: new Date(point.date),
- payouts: Number(point.payouts),
- fees: Number(point.fees),
- }));
+ // Create a lookup object for the timeseries data
+ const timeseriesLookup: Record<string, TimeseriesPoint> = Object.fromEntries(
+ timeseriesData.map((item) => [
+ item.date,
+ {
+ payouts: Number(item.payouts),
+ fees: Number(item.fees),
+ },
+ ]),
+ );
+
+ // Backfill missing dates with 0 values
+ let currentDate = startFunction(
+ DateTime.fromJSDate(startDate).setZone(timezone),
+ );
+
+ const formattedTimeseriesData: FormattedTimeseriesPoint[] = [];
+
+ while (currentDate < endDate) {
+ const periodKey = currentDate.toFormat(formatString);
+
+ formattedTimeseriesData.push({
+ date: currentDate.toJSDate(),
+ ...(timeseriesLookup[periodKey] || {
+ payouts: 0,
+ fees: 0,
+ }),
+ });
+
+ currentDate = dateIncrement(currentDate);
+ }
return NextResponse.json({
invoices: formattedInvoices,