The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add partner earnings endpoints that work across all programs
Rafia57e4dc4e914
diff --git a/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts b/apps/web/app/(ee)/api/embed/referrals/earnings/route.tsindex db01df7424..a7fc0d6cda 100644--- a/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts+++ b/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts@@ -1,7 +1,10 @@import { getEarningsForPartner } from "@/lib/api/partner-profile/get-earnings-for-partner";import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";import { prisma } from "@/lib/prisma";-import { getPartnerEarningsQuerySchema } from "@/lib/zod/schemas/partner-profile";+import {+ getPartnerEarningsQuerySchema,+ PartnerEarningsSchema,+} from "@/lib/zod/schemas/partner-profile";import { NextResponse } from "next/server";import * as z from "zod/v4";@@ -40,7 +43,7 @@ export const GET = withReferralsEmbedToken(sortOrder: "desc",programId: programEnrollment.programId,partnerId: programEnrollment.partnerId,- }),+ }).then((earnings) => z.array(PartnerEarningsSchema).parse(earnings)),withTotal? prisma.commission.count({diff --git a/apps/web/app/(ee)/api/partner-profile/earnings/count/route.ts b/apps/web/app/(ee)/api/partner-profile/earnings/count/route.tsnew file mode 100644index 0000000000..ccb22793ac--- /dev/null+++ b/apps/web/app/(ee)/api/partner-profile/earnings/count/route.ts@@ -0,0 +1,24 @@+import { getEarningsProgramId } from "@/lib/api/partner-profile/get-earnings-program-id";+import { getPartnerEarningsCount } from "@/lib/api/partner-profile/get-partner-earnings-count";+import { withPartnerProfile } from "@/lib/auth/partner";+import { partnerProfileEarningsCountQuerySchema } from "@/lib/zod/schemas/partner-profile";+import { NextResponse } from "next/server";++// GET /api/partner-profile/earnings/count - get earnings count for a partner across all programs (or a single program)+export const GET = withPartnerProfile(async ({ partner, searchParams }) => {+ const { programIdOrSlug, ...filters } =+ partnerProfileEarningsCountQuerySchema.parse(searchParams);++ const programId = await getEarningsProgramId({+ partnerId: partner.id,+ programIdOrSlug,+ });++ const counts = await getPartnerEarningsCount({+ partnerId: partner.id,+ programId,+ filters,+ });++ return NextResponse.json(counts);+});diff --git a/apps/web/app/(ee)/api/partner-profile/earnings/route.ts b/apps/web/app/(ee)/api/partner-profile/earnings/route.tsnew file mode 100644index 0000000000..e49ff283b6--- /dev/null+++ b/apps/web/app/(ee)/api/partner-profile/earnings/route.ts@@ -0,0 +1,24 @@+import { getEarningsForPartner } from "@/lib/api/partner-profile/get-earnings-for-partner";+import { getEarningsProgramId } from "@/lib/api/partner-profile/get-earnings-program-id";+import { withPartnerProfile } from "@/lib/auth/partner";+import { partnerProfileEarningsQuerySchema } from "@/lib/zod/schemas/partner-profile";+import { NextResponse } from "next/server";++// GET /api/partner-profile/earnings - get earnings for a partner across all programs (or a single program)+export const GET = withPartnerProfile(async ({ partner, searchParams }) => {+ const { programIdOrSlug, ...filters } =+ partnerProfileEarningsQuerySchema.parse(searchParams);++ const programId = await getEarningsProgramId({+ partnerId: partner.id,+ programIdOrSlug,+ });++ const earnings = await getEarningsForPartner({+ ...filters,+ programId,+ partnerId: partner.id,+ });++ return NextResponse.json(earnings);+});diff --git a/apps/web/app/(ee)/api/partner-profile/earnings/timeseries/route.ts b/apps/web/app/(ee)/api/partner-profile/earnings/timeseries/route.tsnew file mode 100644index 0000000000..401e645df5--- /dev/null+++ b/apps/web/app/(ee)/api/partner-profile/earnings/timeseries/route.ts@@ -0,0 +1,24 @@+import { getEarningsProgramId } from "@/lib/api/partner-profile/get-earnings-program-id";+import { getPartnerEarningsTimeseries } from "@/lib/api/partner-profile/get-partner-earnings-timeseries";+import { withPartnerProfile } from "@/lib/auth/partner";+import { partnerProfileEarningsTimeseriesQuerySchema } from "@/lib/zod/schemas/partner-profile";+import { NextResponse } from "next/server";++// GET /api/partner-profile/earnings/timeseries - get earnings timeseries for a partner across all programs (or a single program)+export const GET = withPartnerProfile(async ({ partner, searchParams }) => {+ const { programIdOrSlug, ...filters } =+ partnerProfileEarningsTimeseriesQuerySchema.parse(searchParams);++ const programId = await getEarningsProgramId({+ partnerId: partner.id,+ programIdOrSlug,+ });++ const timeseries = await getPartnerEarningsTimeseries({+ partnerId: partner.id,+ programId,+ filters,+ });++ return NextResponse.json(timeseries);+});diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.tsindex 3e33bc1271..ab3943ef2e 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts@@ -1,8 +1,12 @@import { getEarningsForPartner } from "@/lib/api/partner-profile/get-earnings-for-partner";import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";import { withPartnerProfile } from "@/lib/auth/partner";-import { getPartnerEarningsQuerySchema } from "@/lib/zod/schemas/partner-profile";+import {+ getPartnerEarningsQuerySchema,+ PartnerEarningsSchema,+} from "@/lib/zod/schemas/partner-profile";import { NextResponse } from "next/server";+import * as z from "zod/v4";// GET /api/partner-profile/programs/[programId]/earnings – get earnings for a partner in a program enrollmentexport const GET = withPartnerProfile(@@ -21,6 +25,6 @@ export const GET = withPartnerProfile(partnerId,});- return NextResponse.json(earnings);+ return NextResponse.json(z.array(PartnerEarningsSchema).parse(earnings));},);diff --git a/apps/web/lib/api/partner-profile/get-earnings-for-partner.ts b/apps/web/lib/api/partner-profile/get-earnings-for-partner.tsindex 7dfd3fe7a0..b4e38f1a26 100644--- a/apps/web/lib/api/partner-profile/get-earnings-for-partner.ts+++ b/apps/web/lib/api/partner-profile/get-earnings-for-partner.ts@@ -3,7 +3,7 @@ import { generateRandomName } from "@/lib/names";import { prisma } from "@/lib/prisma";import {getPartnerEarningsQuerySchema,- PartnerEarningsSchema,+ PartnerProfileEarningsSchema,} from "@/lib/zod/schemas/partner-profile";import { NETWORK_PROGRAM_ID } from "@dub/utils";import { CommissionType, Partner } from "@prisma/client";@@ -79,6 +79,14 @@ export async function getEarningsForPartner(url: true,},},+ program: {+ select: {+ id: true,+ name: true,+ slug: true,+ logo: true,+ },+ },programEnrollment: {select: {customerDataSharingEnabledAt: true,@@ -113,7 +121,7 @@ export async function getEarningsForPartner(});}- return z.array(PartnerEarningsSchema).parse(+ return z.array(PartnerProfileEarningsSchema).parse(earnings.map(({ programEnrollment, ...e }) => {if (e.type === CommissionType.referral && e.sourcePartnerId) {const sourcePartner = sourcePartners.find(diff --git a/apps/web/lib/api/partner-profile/get-earnings-program-id.ts b/apps/web/lib/api/partner-profile/get-earnings-program-id.tsnew file mode 100644index 0000000000..8d7b045196--- /dev/null+++ b/apps/web/lib/api/partner-profile/get-earnings-program-id.ts@@ -0,0 +1,35 @@+import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";+import { NETWORK_PROGRAM_ID, NETWORK_PROGRAM_SLUG } from "@dub/utils";+import { DubApiError } from "../errors";++export async function getEarningsProgramId({+ partnerId,+ programIdOrSlug,+}: {+ partnerId: string;+ programIdOrSlug?: string;+}) {+ if (!programIdOrSlug) {+ return undefined;+ }++ if (+ [+ NETWORK_PROGRAM_ID.toLowerCase(),+ NETWORK_PROGRAM_SLUG.toLowerCase(),+ ].includes(programIdOrSlug.toLowerCase())+ ) {+ throw new DubApiError({+ code: "not_found",+ message: "Program not found.",+ });+ }++ const { programId } = await getProgramEnrollmentOrThrow({+ partnerId,+ programId: programIdOrSlug,+ include: {},+ });++ return programId;+}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex 301e4b64f8..5a0ec25e19 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -5,6 +5,7 @@ import {PartnerEarningsSchema,partnerPayoutMethodSchema,PartnerProfileCustomerSchema,+ PartnerProfileEarningsSchema,PartnerProfileLinkSchema,partnerSubmittedLeadsCountByStatusSchema,partnerUserSchema,@@ -511,6 +512,10 @@ export type CommissionResponse = z.infer<typeof CommissionEnrichedSchema>;export type PartnerEarningsResponse = z.infer<typeof PartnerEarningsSchema>;+export type PartnerProfileEarningsResponse = z.infer<+ typeof PartnerProfileEarningsSchema+>;+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 2f12c84533..bf634ca9f0 100644--- a/apps/web/lib/zod/schemas/partner-profile.ts+++ b/apps/web/lib/zod/schemas/partner-profile.ts@@ -27,6 +27,7 @@ import { DiscountSchema } from "./discount";import { LinkSchema } from "./links";import { getPaginationQuerySchema } from "./misc";import { payoutsQuerySchema } from "./payouts";+import { ProgramSchema } from "./programs";import { RewardSchema } from "./rewards";import { submittedLeadFormDataSchema } from "./submitted-lead-form";import { centsSchema } from "./utils";@@ -89,6 +90,37 @@ export const getPartnerEarningsTimeseriesSchema =timezone: z.string().optional(),});+const programIdOrSlugSchema = z+ .string()+ .optional()+ .describe(+ "The ID or slug of the program to filter earnings by. If not provided, earnings across all programs are returned.",+ );++export const PartnerProfileEarningsSchema = PartnerEarningsSchema.extend({+ program: ProgramSchema.pick({+ id: true,+ name: true,+ slug: true,+ logo: true,+ }),+});++export const partnerProfileEarningsQuerySchema =+ getPartnerEarningsQuerySchema.extend({+ programIdOrSlug: programIdOrSlugSchema,+ });++export const partnerProfileEarningsCountQuerySchema =+ getPartnerEarningsCountQuerySchema.extend({+ programIdOrSlug: programIdOrSlugSchema,+ });++export const partnerProfileEarningsTimeseriesQuerySchema =+ getPartnerEarningsTimeseriesSchema.extend({+ programIdOrSlug: programIdOrSlugSchema,+ });+export const PartnerProfileLinkSchema = LinkSchema.pick({id: true,domain: true,