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 partner customers stats

Steven Tey6686f049b61f
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
index b688026986..2e98fa1291 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
@@ -10,6 +10,7 @@ import {
import { generateRandomName } from "@/lib/names";
import { PartnerProfileCustomerSchema } from "@/lib/zod/schemas/partner-profile";
import { prisma } from "@dub/prisma";
+import { CommissionType } from "@dub/prisma/client";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
@@ -41,6 +42,19 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
where: {
id: customerId,
},
+ include: {
+ // find the first sale commission for this customer and partner
+ commissions: {
+ where: {
+ partnerId: partner.id,
+ type: CommissionType.sale,
+ },
+ take: 1,
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
+ },
});
if (!customer || customer?.projectId !== program.workspaceId) {
@@ -65,15 +79,18 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
// get the first partner link that this customer interacted with
const firstLinkId = events[events.length - 1].link_id;
const link = links.find((link) => link.id === firstLinkId);
+ const firstSaleAt =
+ customer.commissions[0]?.createdAt ?? customer.firstSaleAt;
+ // TODO: Calculate based on events (more accurate for multi-tenant setups where customers can interact with multiple partners)
const timeToLead =
customer.clickedAt && customer.createdAt
? customer.createdAt.getTime() - customer.clickedAt.getTime()
: null;
const timeToSale =
- customer.firstSaleAt && customer.createdAt
- ? customer.firstSaleAt.getTime() - customer.createdAt.getTime()
+ firstSaleAt && customer.createdAt
+ ? firstSaleAt.getTime() - customer.createdAt.getTime()
: null;
return NextResponse.json(
@@ -82,6 +99,7 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
}).parse({
...transformCustomer({
...customer,
+ firstSaleAt,
email: customer.email
? customerDataSharingEnabledAt
? customer.email
@@ -90,6 +108,7 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
}),
activity: {
...customer,
+ firstSaleAt,
timeToLead,
timeToSale,
events,
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
index 7a97df3311..d832b8c152 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
@@ -12,6 +12,7 @@ import {
getPartnerCustomersQuerySchema,
} from "@/lib/zod/schemas/partner-profile";
import { prisma, sanitizeFullTextSearch } from "@dub/prisma";
+import { CommissionType } from "@dub/prisma/client";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
@@ -61,6 +62,16 @@ export const GET = withPartnerProfile(
},
include: {
link: true,
+ commissions: {
+ where: {
+ partnerId: partner.id,
+ type: CommissionType.sale,
+ },
+ take: 1,
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
},
orderBy: {
[sortBy]: sortOrder,
@@ -71,6 +82,10 @@ export const GET = withPartnerProfile(
// Map customers with their data
const customersWithData = customers.map((customer) => {
+ const firstSaleAt =
+ customer.commissions[0]?.createdAt ?? customer.firstSaleAt;
+
+ // TODO: Calculate based on events (more accurate for multi-tenant setups where customers can interact with multiple partners)
const timeToLead =
customer.clickedAt && customer.createdAt
? customer.createdAt.getTime() - customer.clickedAt.getTime()
@@ -81,6 +96,7 @@ export const GET = withPartnerProfile(
}).parse({
...transformCustomer({
...customer,
+ firstSaleAt,
email: customer.email
? customerDataSharingEnabledAt
? customer.email
@@ -89,6 +105,7 @@ export const GET = withPartnerProfile(
}),
activity: {
...customer,
+ firstSaleAt,
timeToLead,
timeToSale: null,
events: [],
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page-client.tsx
index d1e7ac950f..556e56289b 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page-client.tsx
@@ -278,12 +278,7 @@ export function ProgramCustomersPageClient() {
onPaginationChange: setPagination,
columnVisibility,
onColumnVisibilityChange: setColumnVisibility,
- sortableColumns: [
- "saleAmount",
- "createdAt",
- "firstSaleAt",
- "subscriptionCanceledAt",
- ],
+ sortableColumns: ["saleAmount", "createdAt"],
sortBy,
sortOrder,
onSortChange: ({ sortBy, sortOrder }) =>
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/[customerId]/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/[customerId]/page-client.tsx
index 7b7755d310..2e211e084d 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/[customerId]/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/[customerId]/page-client.tsx
@@ -10,10 +10,11 @@ import { CustomerSalesTable } from "@/ui/customers/customer-sales-table";
import { PageContent } from "@/ui/layout/page-content";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
import { ProgramRewardList } from "@/ui/partners/program-reward-list";
-import { ChevronRight, MoneyBill2, Tooltip, UserCheck } from "@dub/ui";
+import { ChevronRight, MoneyBill2, Tooltip, User } from "@dub/ui";
import { cn, fetcher, formatDate } from "@dub/utils";
import { addMonths, isBefore } from "date-fns";
import { AlertCircle } from "lucide-react";
+import Link from "next/link";
import { redirect, useParams } from "next/navigation";
import { memo, useMemo } from "react";
import useSWR from "swr";
@@ -53,17 +54,17 @@ export function ProgramCustomerPageClient() {
<PageContent
title={
<div className="flex items-center gap-1.5">
- <div
- // href={`/programs/${programSlug}/customers`}
- // aria-label="Back to customers"
- // title="Back to customers"
+ <Link
+ href={`/programs/${programSlug}/customers`}
+ aria-label="Back to customers"
+ title="Back to customers"
className={cn(
"bg-bg-subtle flex size-8 shrink-0 items-center justify-center rounded-lg",
- // "hover:bg-bg-emphasis transition-[transform,background-color] duration-150 active:scale-95",
+ "hover:bg-bg-emphasis transition-[transform,background-color] duration-150 active:scale-95",
)}
>
- <UserCheck className="size-4" />
- </div>
+ <User className="size-4" />
+ </Link>
<ChevronRight className="text-content-muted size-2.5 shrink-0 [&_*]:stroke-2" />
<div>
{customer ? (
diff --git a/apps/web/ui/layout/sidebar/partners-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/partners-sidebar-nav.tsx
index 08c19748a0..6e7c90912a 100644
--- a/apps/web/ui/layout/sidebar/partners-sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/partners-sidebar-nav.tsx
@@ -30,6 +30,7 @@ import { ReactNode, useMemo } from "react";
import { CursorRays } from "./icons/cursor-rays";
import { Hyperlink } from "./icons/hyperlink";
import { LinesY } from "./icons/lines-y";
+import { User } from "./icons/user";
import { PartnerProgramDropdown } from "./partner-program-dropdown";
import { PayoutStats } from "./payout-stats";
import { ProgramHelpSupport } from "./program-help-support";
@@ -222,7 +223,7 @@ const NAV_AREAS: SidebarNavAreas<SidebarNavData> = {
},
{
name: "Customers",
- icon: UserCheck,
+ icon: User,
href: `/programs/${programSlug}/customers` as `/${string}`,
locked: isUnapproved,
},