The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor to /earnings/analytics instead of /earnings/top
Steven Teyf7b49a014f63
diff --git a/apps/web/app/(ee)/api/partner-profile/earnings/analytics/route.ts b/apps/web/app/(ee)/api/partner-profile/earnings/analytics/route.tsnew file mode 100644index 0000000000..0153387674--- /dev/null+++ b/apps/web/app/(ee)/api/partner-profile/earnings/analytics/route.ts@@ -0,0 +1,27 @@+import { getEarningsProgramId } from "@/lib/api/partner-profile/get-earnings-program-id";+import { getPartnerEarningsByGroup } from "@/lib/api/partner-profile/get-partner-earnings-by-group";+import { withPartnerProfile } from "@/lib/auth/partner";+import { partnerProfileEarningsAnalyticsQuerySchema } from "@/lib/zod/schemas/partner-profile";+import { NextResponse } from "next/server";++// GET /api/partner-profile/earnings/analytics - highest-earning programs, links, or commission types for a partner+export const GET = withPartnerProfile(async ({ partner, searchParams }) => {+ const { groupBy, programIdOrSlug, ...filters } =+ partnerProfileEarningsAnalyticsQuerySchema.parse(searchParams);++ const programId = await getEarningsProgramId({+ partnerId: partner.id,+ programIdOrSlug,+ });++ const earnings = await getPartnerEarningsByGroup({+ partnerId: partner.id,+ programId,+ filters: {+ ...filters,+ groupBy,+ },+ });++ return NextResponse.json(earnings);+});diff --git a/apps/web/app/(ee)/api/partner-profile/earnings/top/route.ts b/apps/web/app/(ee)/api/partner-profile/earnings/top/route.tsdeleted file mode 100644index 874739735f..0000000000--- a/apps/web/app/(ee)/api/partner-profile/earnings/top/route.ts+++ /dev/null@@ -1,24 +0,0 @@-import { getEarningsProgramId } from "@/lib/api/partner-profile/get-earnings-program-id";-import { getPartnerTopEarnings } from "@/lib/api/partner-profile/get-partner-top-earnings";-import { withPartnerProfile } from "@/lib/auth/partner";-import { partnerProfileTopEarningsQuerySchema } from "@/lib/zod/schemas/partner-profile";-import { NextResponse } from "next/server";--// GET /api/partner-profile/earnings/top - get the programs or links with the highest earnings for a partner in a date range-export const GET = withPartnerProfile(async ({ partner, searchParams }) => {- const { programIdOrSlug, ...filters } =- partnerProfileTopEarningsQuerySchema.parse(searchParams);-- const programId = await getEarningsProgramId({- partnerId: partner.id,- programIdOrSlug,- });-- const topEarnings = await getPartnerTopEarnings({- partnerId: partner.id,- programId,- filters,- });-- return NextResponse.json(topEarnings);-});diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/top-links-card.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/top-links-card.tsxindex d2969e84c3..be9141f6d4 100644--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/top-links-card.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/top-links-card.tsx@@ -1,7 +1,7 @@"use client";import { testIds } from "@/lib/e2e/test-ids";-import { usePartnerProfileTopEarnings } from "@/lib/swr/use-partner-profile-earnings";+import { usePartnerProfileEarningsAnalytics } from "@/lib/swr/use-partner-profile-earnings";import { Hyperlink } from "@dub/ui/icons";import { cn, currencyFormatter, getPrettyUrl } from "@dub/utils";import Link from "next/link";@@ -16,7 +16,7 @@ import { useOverviewDateRange } from "./use-overview-date-range";export function TopLinksCard() {const { start, end, interval } = useOverviewDateRange();- const { data: links, error } = usePartnerProfileTopEarnings({+ const { data: links, error } = usePartnerProfileEarningsAnalytics({groupBy: "linkId",limit: 6,interval,diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/use-top-program-earnings.ts b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/use-top-program-earnings.tsindex 734cf2f26b..3be7e242c5 100644--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/use-top-program-earnings.ts+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/use-top-program-earnings.ts@@ -1,11 +1,11 @@-import { usePartnerProfileTopEarnings } from "@/lib/swr/use-partner-profile-earnings";+import { usePartnerProfileEarningsAnalytics } from "@/lib/swr/use-partner-profile-earnings";import { useOverviewDateRange } from "./use-overview-date-range";// shared by the earnings chart tooltip and the Top programs card, so both use one requestexport function useTopProgramEarnings() {const { start, end, interval } = useOverviewDateRange();- return usePartnerProfileTopEarnings({+ return usePartnerProfileEarningsAnalytics({groupBy: "programId",limit: 100,interval,diff --git a/apps/web/lib/api/partner-profile/get-partner-top-earnings.ts b/apps/web/lib/api/partner-profile/get-partner-earnings-by-group.tssimilarity index 79%rename from apps/web/lib/api/partner-profile/get-partner-top-earnings.tsrename to apps/web/lib/api/partner-profile/get-partner-earnings-by-group.tsindex 30e28fe8b7..2286df7fa5 100644--- a/apps/web/lib/api/partner-profile/get-partner-top-earnings.ts+++ b/apps/web/lib/api/partner-profile/get-partner-earnings-by-group.ts@@ -1,9 +1,10 @@import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";import { prisma } from "@/lib/prisma";import {- partnerProfileTopEarningsQuerySchema,+ partnerProfileEarningsAnalyticsQuerySchema,PartnerProfileTopLinkEarningsSchema,PartnerProfileTopProgramEarningsSchema,+ PartnerProfileTypeEarningsSchema,} from "@/lib/zod/schemas/partner-profile";import { Prisma } from "@prisma/client";import * as z from "zod/v4";@@ -16,7 +17,7 @@ const programSelect = {logo: true,} satisfies Prisma.ProgramSelect;-export async function getPartnerTopEarnings({+export async function getPartnerEarningsByGroup({partnerId,programId,filters,@@ -24,9 +25,11 @@ export async function getPartnerTopEarnings({partnerId: string;programId?: string; // if not provided, earnings across all programs (except the network program) are summedfilters: Omit<- z.infer<typeof partnerProfileTopEarningsQuerySchema>,- "programIdOrSlug"- >;+ z.infer<typeof partnerProfileEarningsAnalyticsQuerySchema>,+ "programIdOrSlug" | "groupBy"+ > & {+ groupBy: "programId" | "linkId" | "type";+ };}) {const { groupBy, limit, type, status, interval, start, end, timezone } =filters;@@ -84,6 +87,29 @@ export async function getPartnerTopEarnings({);}+ if (groupBy === "type") {+ const sums = await prisma.commission.groupBy({+ by: ["type"],+ where,+ _sum: {+ earnings: true,+ },+ orderBy: {+ _sum: {+ earnings: "desc",+ },+ },+ take: limit,+ });++ return z.array(PartnerProfileTypeEarningsSchema).parse(+ sums.map(({ type, _sum }) => ({+ type,+ earnings: _sum.earnings ?? 0,+ })),+ );+ }+const sums = await prisma.commission.groupBy({by: ["linkId"],where: {diff --git a/apps/web/lib/swr/use-partner-profile-earnings.ts b/apps/web/lib/swr/use-partner-profile-earnings.tsindex 8b7bd55c4d..3d3b0d7ec9 100644--- a/apps/web/lib/swr/use-partner-profile-earnings.ts+++ b/apps/web/lib/swr/use-partner-profile-earnings.ts@@ -4,9 +4,9 @@ import useSWR from "swr";import { DUB_PARTNERS_ANALYTICS_INTERVAL } from "../analytics/constants";import { IntervalOptions } from "../analytics/types";import {+ PartnerProfileEarningsAnalyticsByGroup,+ PartnerProfileEarningsAnalyticsGroupBy,PartnerProfileEarningsResponse,- PartnerProfileTopLinkEarnings,- PartnerProfileTopProgramEarnings,} from "../types";type DateRange = {@@ -15,18 +15,6 @@ type DateRange = {end?: Date;};-type PartnerProfileEarningsTimeseries = {- start: string;- earnings: number;- groupBy?: string;- data?: Record<string, number>;-}[];--type TopEarningsByGroup = {- programId: PartnerProfileTopProgramEarnings;- linkId: PartnerProfileTopLinkEarnings;-};-// GET /api/partner-profile/earningsexport function usePartnerProfileEarnings({pageSize,@@ -39,6 +27,13 @@ export function usePartnerProfileEarnings({);}+type PartnerProfileEarningsTimeseries = {+ start: string;+ earnings: number;+ groupBy?: string;+ data?: Record<string, number>;+}[];+// GET /api/partner-profile/earnings/timeseriesexport function usePartnerProfileEarningsTimeseries({groupBy,@@ -51,13 +46,20 @@ export function usePartnerProfileEarningsTimeseries({);}-// GET /api/partner-profile/earnings/top-export function usePartnerProfileTopEarnings<- T extends keyof TopEarningsByGroup,->({ groupBy, limit, ...dateRange }: DateRange & { groupBy: T; limit: number }) {- return usePartnerProfileEarningsSWR<TopEarningsByGroup[T][]>(- "/top",- { groupBy, limit: String(limit) },+// GET /api/partner-profile/earnings/analytics+export function usePartnerProfileEarningsAnalytics<+ G extends PartnerProfileEarningsAnalyticsGroupBy,+>({+ groupBy,+ limit,+ ...dateRange+}: DateRange & { groupBy: G; limit?: number }) {+ return usePartnerProfileEarningsSWR<PartnerProfileEarningsAnalyticsByGroup[G]>(+ "/analytics",+ {+ groupBy,+ ...(limit !== undefined && { limit: String(limit) }),+ },dateRange,);}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 3470e7eae1..44d4826896 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -9,6 +9,8 @@ import {PartnerProfileLinkSchema,PartnerProfileTopLinkEarningsSchema,PartnerProfileTopProgramEarningsSchema,+ PartnerProfileTypeEarningsSchema,+ partnerProfileEarningsAnalyticsQuerySchema,partnerSubmittedLeadsCountByStatusSchema,partnerUserSchema,} from "@/lib/zod/schemas/partner-profile";@@ -526,6 +528,23 @@ export type PartnerProfileTopLinkEarnings = z.infer<typeof PartnerProfileTopLinkEarningsSchema>;+export type PartnerProfileEarningsAnalyticsQuery = z.infer<+ typeof partnerProfileEarningsAnalyticsQuerySchema+>;++export type PartnerProfileEarningsAnalyticsGroupBy =+ PartnerProfileEarningsAnalyticsQuery["groupBy"];++export type PartnerProfileTypeEarnings = z.infer<+ typeof PartnerProfileTypeEarningsSchema+>;++export type PartnerProfileEarningsAnalyticsByGroup = {+ programId: PartnerProfileTopProgramEarnings[];+ linkId: PartnerProfileTopLinkEarnings[];+ type: PartnerProfileTypeEarnings[];+};+export type CustomerProps = z.infer<typeof CustomerSchema>;export type PartnerPlatformProps = z.infer<typeof partnerPlatformSchema>;diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.tsindex 9b81ed4098..5cac1bf92e 100644--- a/apps/web/lib/zod/schemas/partner-profile.ts+++ b/apps/web/lib/zod/schemas/partner-profile.ts@@ -125,11 +125,11 @@ export const partnerProfileEarningsTimeseriesQuerySchema =.enum(["type", "linkId", "programId"]).optional().describe(- "The field to group the earnings by. Each period then includes a `data` object with the earnings of each group.",+ "The field to group the earnings by. If omitted, each period is the total earnings. Otherwise each period includes a `data` object with the earnings of each group.",),});-export const partnerProfileTopEarningsQuerySchema =+export const partnerProfileEarningsAnalyticsQuerySchema =partnerProfileEarningsCountQuerySchema.pick({programIdOrSlug: true,@@ -142,8 +142,10 @@ export const partnerProfileTopEarningsQuerySchema =}).extend({groupBy: z- .enum(["programId", "linkId"])- .describe("The field to sum the earnings by."),+ .enum(["programId", "linkId", "type"])+ .describe(+ "The field to sum earnings by. Returns the highest-earning programs, links, or commission types.",+ ),limit: z.coerce.number().int()@@ -169,6 +171,11 @@ export const PartnerProfileTopLinkEarningsSchema = LinkSchema.pick({earnings: z.number(),});+export const PartnerProfileTypeEarningsSchema = z.object({+ type: z.enum(CommissionType),+ earnings: z.number(),+});+export const PartnerProfileLinkSchema = LinkSchema.pick({id: true,domain: true,diff --git a/apps/web/playwright/api/partner-profile/earnings.spec.ts b/apps/web/playwright/api/partner-profile/earnings.spec.tsindex 25cc009b67..ae411d5fb8 100644--- a/apps/web/playwright/api/partner-profile/earnings.spec.ts+++ b/apps/web/playwright/api/partner-profile/earnings.spec.ts@@ -281,7 +281,7 @@ test.describe("GET /partner-profile/earnings", () => {await withPartnerApi(playwright, async (partnerApi) => {const { status, data } = await partnerApi.get<{ id: string; earnings: number }[]- >("/api/partner-profile/earnings/top?groupBy=programId");+ >("/api/partner-profile/earnings/analytics?groupBy=programId");expect(status).toEqual(200);expect(data).toEqual([@@ -291,6 +291,17 @@ test.describe("GET /partner-profile/earnings", () => {});});+ test("sums earnings by commission type", async ({ playwright }) => {+ await withPartnerApi(playwright, async (partnerApi) => {+ const { status, data } = await partnerApi.get<+ { type: string; earnings: number }[]+ >("/api/partner-profile/earnings/analytics?groupBy=type");++ expect(status).toEqual(200);+ expect(data).toEqual([{ type: "custom", earnings: 7500 }]);+ });+ });+test("groups the timeseries by program", async ({ playwright, program }) => {await withPartnerApi(playwright, async (partnerApi) => {const { status, data } = await partnerApi.get<TimeseriesPoint[]>(