diff --git a/apps/web/app/(ee)/api/commissions/route.ts b/apps/web/app/(ee)/api/commissions/route.ts index d33eec75524..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,12 +15,13 @@ import { z } from "zod"; export const GET = withWorkspace(async ({ workspace, searchParams }) => { const programId = getDefaultProgramIdOrThrow(workspace); - const { + let { status, type, customerId, payoutId, partnerId, + tenantId, invoiceId, groupId, page, @@ -37,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 ? { diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts index 46bdfbbd2c0..7500cf895d4 100644 --- a/apps/web/lib/zod/schemas/commissions.ts +++ b/apps/web/lib/zod/schemas/commissions.ts @@ -63,7 +63,15 @@ 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's `tenantId` (their unique ID within your database).", + ), groupId: z .string() .optional()