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.

update endpoints

Kiran K1e244527faf2
diff --git a/apps/web/app/api/referrals/sales/count/route.ts b/apps/web/app/api/referrals/sales/count/route.ts
new file mode 100644
index 0000000000..e212938170
--- /dev/null
+++ b/apps/web/app/api/referrals/sales/count/route.ts
@@ -0,0 +1,23 @@
+import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
+import { prisma } from "@/lib/prisma";
+import { withAuth } from "@/lib/referrals/auth";
+import { getPartnerSalesCountQuerySchema } from "@/lib/zod/schemas/partners";
+import { NextResponse } from "next/server";
+
+// GET /api/referrals/sales/count – get sales count for a link
+export const GET = withAuth(async ({ searchParams, link }) => {
+ const parsed = getPartnerSalesCountQuerySchema.parse(searchParams);
+ const { startDate, endDate } = getStartEndDates(parsed);
+
+ const salesCount = await prisma.sale.count({
+ where: {
+ linkId: link.id,
+ createdAt: {
+ gte: startDate.toISOString(),
+ lte: endDate.toISOString(),
+ },
+ },
+ });
+
+ return NextResponse.json({ count: salesCount });
+});
diff --git a/apps/web/app/api/referrals/sales/route.ts b/apps/web/app/api/referrals/sales/route.ts
new file mode 100644
index 0000000000..70f7cd954b
--- /dev/null
+++ b/apps/web/app/api/referrals/sales/route.ts
@@ -0,0 +1,30 @@
+import { prisma } from "@/lib/prisma";
+import { withAuth } from "@/lib/referrals/auth";
+import z from "@/lib/zod";
+import { PartnerSaleResponseSchema } from "@/lib/zod/schemas/partners";
+import { NextResponse } from "next/server";
+
+// GET /api/referrals/sales – get sales for a link
+export const GET = withAuth(async ({ link }) => {
+ const sales = await prisma.sale.findMany({
+ where: {
+ linkId: link.id,
+ },
+ select: {
+ id: true,
+ amount: true,
+ earnings: true,
+ currency: true,
+ status: true,
+ createdAt: true,
+ updatedAt: true,
+ customer: true,
+ },
+ take: 10,
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ return NextResponse.json(z.array(PartnerSaleResponseSchema).parse(sales));
+});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
index 76fca3d872..c9000988af 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
@@ -2,6 +2,7 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { ReferralsEmbed } from "@/ui/embed/referrals-embed";
+import { LoadingSpinner } from "@dub/ui";
import { redirect } from "next/navigation";
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
@@ -35,13 +36,9 @@ export default function ReferralsPageClient() {
redirect(`/${slug}/settings`);
}
- if (!publicToken) {
- return <div>Loading...</div>;
- }
-
- return (
- <>
- <ReferralsEmbed publicToken={publicToken} />
- </>
+ return publicToken ? (
+ <ReferralsEmbed publicToken={publicToken} />
+ ) : (
+ <LoadingSpinner className="my-auto h-full w-5 text-gray-400" />
);
}
diff --git a/apps/web/app/app.dub.co/embed/page.tsx b/apps/web/app/app.dub.co/embed/page.tsx
index 450571379e..c920f89d35 100644
--- a/apps/web/app/app.dub.co/embed/page.tsx
+++ b/apps/web/app/app.dub.co/embed/page.tsx
@@ -1,9 +1,5 @@
import { ReferralsEmbedPageClient } from "./page-client";
export default async function ReferralsEmbedPage() {
- return (
- <>
- <ReferralsEmbedPageClient />
- </>
- );
+ return <ReferralsEmbedPageClient />;
}
diff --git a/apps/web/app/app.dub.co/embed/sale-table.tsx b/apps/web/app/app.dub.co/embed/sale-table.tsx
index f9afb3e6d0..66709ed9f8 100644
--- a/apps/web/app/app.dub.co/embed/sale-table.tsx
+++ b/apps/web/app/app.dub.co/embed/sale-table.tsx
@@ -1,55 +1,46 @@
"use client";
-import { IntervalOptions } from "@/lib/analytics/types";
+import { PartnerSaleResponse } from "@/lib/types";
+import { SaleStatusBadges } from "@/ui/partners/sale-status-badges";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
-import { Table, usePagination, useRouterStuff, useTable } from "@dub/ui";
+import {
+ StatusBadge,
+ Table,
+ usePagination,
+ useRouterStuff,
+ useTable,
+} from "@dub/ui";
import { CircleDollar } from "@dub/ui/src/icons";
-import { currencyFormatter, formatDate } from "@dub/utils";
-import useReferralAnalytics from "./use-referral-analytics";
-import useReferralEvents from "./use-referral-events";
+import { currencyFormatter, fetcher, formatDate } from "@dub/utils";
+import useSWR from "swr";
export function SaleTable({ limit }: { limit?: number }) {
- const { queryParams, searchParamsObj } = useRouterStuff();
+ const { pagination, setPagination } = usePagination(limit);
+ const { queryParams, searchParamsObj, getQueryString } = useRouterStuff();
const {
- start,
- end,
- interval,
- sortBy = "timestamp",
- order = "desc",
- } = searchParamsObj as {
- start?: string;
- end?: string;
- interval?: IntervalOptions;
+ data: sales,
+ isLoading,
+ error,
+ } = useSWR<PartnerSaleResponse[]>(
+ `/api/referrals/sales${getQueryString()}`,
+ fetcher,
+ );
+
+ const { data: salesCount } = useSWR<{ count: number }>(
+ `/api/referrals/sales/count${getQueryString()}`,
+ fetcher,
+ );
+
+ const { sortBy = "timestamp", order = "desc" } = searchParamsObj as {
sortBy?: "timestamp";
order?: "asc" | "desc";
};
- const { data: { sales: totalSaleEvents } = {} } = useReferralAnalytics({
- interval,
- start: start ? new Date(start) : undefined,
- end: end ? new Date(end) : undefined,
- });
-
- const {
- data: saleEvents,
- loading,
- error,
- } = useReferralEvents({
- event: "sales",
- interval: interval as any,
- start: start ? new Date(start) : undefined,
- end: end ? new Date(end) : undefined,
- order,
- sortBy,
- });
-
- const { pagination, setPagination } = usePagination(limit);
-
const { table, ...tableProps } = useTable({
- data: saleEvents?.slice(0, limit) || [],
- loading,
+ data: sales?.slice(0, limit) || [],
+ loading: isLoading,
error: error ? "Failed to fetch sales events." : undefined,
columns: [
{
@@ -57,7 +48,7 @@ export function SaleTable({ limit }: { limit?: number }) {
header: "Date",
accessorKey: "timestamp",
cell: ({ row }) => {
- return formatDate(row.original.timestamp, { month: "short" });
+ return formatDate(row.original.createdAt, { month: "short" });
},
},
{
@@ -73,7 +64,7 @@ export function SaleTable({ limit }: { limit?: number }) {
header: "Sale Amount",
accessorKey: "sale",
cell: ({ row }) => {
- return currencyFormatter(row.original.sale.amount / 100, {
+ return currencyFormatter(row.original.amount / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
@@ -90,6 +81,18 @@ export function SaleTable({ limit }: { limit?: number }) {
});
},
},
+ {
+ header: "Status",
+ cell: ({ row }) => {
+ const badge = SaleStatusBadges[row.original.status];
+
+ return (
+ <StatusBadge icon={null} variant={badge.variant}>
+ {badge.label}
+ </StatusBadge>
+ );
+ },
+ },
],
...(!limit && {
pagination,
@@ -105,7 +108,7 @@ export function SaleTable({ limit }: { limit?: number }) {
},
}),
}),
- rowCount: totalSaleEvents,
+ rowCount: salesCount?.count || 0,
emptyState: (
<AnimatedEmptyState
title="No sales found"
@@ -128,7 +131,7 @@ export function SaleTable({ limit }: { limit?: number }) {
<SimpleDateRangePicker className="w-full sm:min-w-[200px] md:w-fit" />
</div>
)}
- {loading || saleEvents?.length ? (
+ {isLoading || sales?.length ? (
<Table
{...tableProps}
table={table}