From bf5641f744fbe542ef5f34634fb2e2d8e8b25f44 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Tue, 30 Sep 2025 18:51:34 +0530 Subject: [PATCH 1/2] Add tenantId filter to commissions API and schema --- apps/web/app/(ee)/api/commissions/route.ts | 12 ++++++++++++ apps/web/lib/zod/schemas/commissions.ts | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/apps/web/app/(ee)/api/commissions/route.ts b/apps/web/app/(ee)/api/commissions/route.ts index d33eec75524..cffcc2a151f 100644 --- a/apps/web/app/(ee)/api/commissions/route.ts +++ b/apps/web/app/(ee)/api/commissions/route.ts @@ -20,6 +20,7 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => { customerId, payoutId, partnerId, + tenantId, invoiceId, groupId, page, @@ -67,6 +68,17 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => { }, }, }), + ...(tenantId && + !partnerId && { + partner: { + programs: { + some: { + tenantId, + programId, + }, + }, + }, + }), }, include: { customer: true, diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts index 46bdfbbd2c0..a2e3affe279 100644 --- a/apps/web/lib/zod/schemas/commissions.ts +++ b/apps/web/lib/zod/schemas/commissions.ts @@ -64,6 +64,12 @@ export const getCommissionsQuerySchema = z .string() .optional() .describe("Filter the list of commissions by the associated partner."), + tenantId: z + .string() + .optional() + .describe( + "Filter the list of commissions by the associated partner tenantId.", + ), groupId: z .string() .optional() From fe312ca8d993d71a8cd00ac77dc4e67acfce8fb5 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Tue, 30 Sep 2025 10:44:02 -0700 Subject: [PATCH 2/2] small tweaks --- apps/web/app/(ee)/api/commissions/route.ts | 35 ++++++++++++++-------- apps/web/lib/zod/schemas/commissions.ts | 6 ++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/apps/web/app/(ee)/api/commissions/route.ts b/apps/web/app/(ee)/api/commissions/route.ts index cffcc2a151f..0c5f485fba0 100644 --- a/apps/web/app/(ee)/api/commissions/route.ts +++ b/apps/web/app/(ee)/api/commissions/route.ts @@ -1,5 +1,6 @@ import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates"; import { transformCustomerForCommission } from "@/lib/api/customers/transform-customer"; +import { DubApiError } from "@/lib/api/errors"; import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw"; import { withWorkspace } from "@/lib/auth"; import { @@ -14,7 +15,7 @@ import { z } from "zod"; export const GET = withWorkspace(async ({ workspace, searchParams }) => { const programId = getDefaultProgramIdOrThrow(workspace); - const { + let { status, type, customerId, @@ -38,6 +39,27 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => { end, }); + if (tenantId && !partnerId) { + const partner = await prisma.programEnrollment.findUnique({ + where: { + tenantId_programId: { + tenantId, + programId, + }, + }, + select: { + partnerId: true, + }, + }); + if (!partner) { + throw new DubApiError({ + code: "not_found", + message: `Partner with specified tenantId ${tenantId} not found.`, + }); + } + partnerId = partner.partnerId; + } + const commissions = await prisma.commission.findMany({ where: invoiceId ? { @@ -68,17 +90,6 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => { }, }, }), - ...(tenantId && - !partnerId && { - partner: { - programs: { - some: { - tenantId, - programId, - }, - }, - }, - }), }, include: { customer: true, diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts index a2e3affe279..7500cf895d4 100644 --- a/apps/web/lib/zod/schemas/commissions.ts +++ b/apps/web/lib/zod/schemas/commissions.ts @@ -63,12 +63,14 @@ export const getCommissionsQuerySchema = z partnerId: z .string() .optional() - .describe("Filter the list of commissions by the associated partner."), + .describe( + "Filter the list of commissions by the associated partner. When specified, takes precedence over `tenantId`.", + ), tenantId: z .string() .optional() .describe( - "Filter the list of commissions by the associated partner tenantId.", + "Filter the list of commissions by the associated partner's `tenantId` (their unique ID within your database).", ), groupId: z .string()