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.

Include all partner commissions for partner-level fraud risks

Kiran K1d88fd1325eb
diff --git a/apps/web/lib/api/commissions/get-commissions-count.ts b/apps/web/lib/api/commissions/get-commissions-count.ts
index 14cc84086f..740ea9fe3f 100644
--- a/apps/web/lib/api/commissions/get-commissions-count.ts
+++ b/apps/web/lib/api/commissions/get-commissions-count.ts
@@ -4,6 +4,7 @@ import { getCommissionsCountQuerySchema } from "@/lib/zod/schemas/commissions";
import { parseFilterValue } from "@dub/utils";
import { CommissionStatus, CommissionType } from "@prisma/client";
import * as z from "zod/v4";
+import { PARTNER_LEVEL_FRAUD_RULES } from "../fraud/constants";
type CommissionsCountFilters = z.infer<
typeof getCommissionsCountQuerySchema
@@ -29,23 +30,37 @@ export async function getCommissionsCount(filters: CommissionsCountFilters) {
programId,
} = filters;
- // Resolve fraudEventGroupId to customerIds
+ // Filter the commissions count based on the risk event group
let customerIds: string[] | undefined;
if (fraudEventGroupId) {
const riskEvents = await prisma.fraudEvent.findMany({
where: {
fraudEventGroupId,
- customerId: {
- not: null,
- },
+ programId,
},
select: {
customerId: true,
+ fraudEventGroup: {
+ select: {
+ type: true,
+ },
+ },
},
});
- customerIds = riskEvents.map((e) => e.customerId!);
+ if (riskEvents.length > 0) {
+ const riskEventGroup = riskEvents[0].fraudEventGroup;
+ const isPartnerLevelRisk = PARTNER_LEVEL_FRAUD_RULES.includes(
+ riskEventGroup.type as (typeof PARTNER_LEVEL_FRAUD_RULES)[number],
+ );
+
+ // Partner-level: include all commissions for the partner
+ // Customer-level: include only commissions for the customers in the risk event group
+ if (!isPartnerLevelRisk) {
+ customerIds = riskEvents.map((e) => e.customerId!);
+ }
+ }
}
const { startDate, endDate } = getStartEndDates({
@@ -112,7 +127,7 @@ export async function getCommissionsCount(filters: CommissionsCountFilters) {
? { notIn: customerFilter.values }
: { in: customerFilter.values },
}),
- ...(customerIds?.length && {
+ ...(customerIds && {
customerId: {
in: customerIds,
},
diff --git a/apps/web/lib/api/commissions/get-commissions.ts b/apps/web/lib/api/commissions/get-commissions.ts
index b4be057775..1a0ccf3923 100644
--- a/apps/web/lib/api/commissions/get-commissions.ts
+++ b/apps/web/lib/api/commissions/get-commissions.ts
@@ -5,6 +5,7 @@ import { parseFilterValue } from "@dub/utils";
import { CommissionStatus, CommissionType } from "@prisma/client";
import * as z from "zod/v4";
import { DubApiError } from "../errors";
+import { PARTNER_LEVEL_FRAUD_RULES } from "../fraud/constants";
import { buildPaginationQuery } from "../pagination";
type CommissionsFilters = Omit<
@@ -60,23 +61,37 @@ export async function getCommissions(filters: CommissionsFilters) {
}
}
- // Resolve fraudEventGroupId to customerIds
+ // Filter the commissions based on the risk event group
let customerIds: string[] | undefined;
if (fraudEventGroupId) {
const riskEvents = await prisma.fraudEvent.findMany({
where: {
fraudEventGroupId,
- customerId: {
- not: null,
- },
+ programId,
},
select: {
customerId: true,
+ fraudEventGroup: {
+ select: {
+ type: true,
+ },
+ },
},
});
- customerIds = riskEvents.map((e) => e.customerId!);
+ if (riskEvents.length > 0) {
+ const riskEventGroup = riskEvents[0].fraudEventGroup;
+ const isPartnerLevelRisk = PARTNER_LEVEL_FRAUD_RULES.includes(
+ riskEventGroup.type as (typeof PARTNER_LEVEL_FRAUD_RULES)[number],
+ );
+
+ // Partner-level: include all commissions for the partner
+ // Customer-level: include only commissions for the customers in the risk event group
+ if (!isPartnerLevelRisk) {
+ customerIds = riskEvents.map((e) => e.customerId!);
+ }
+ }
}
const { startDate, endDate } = getStartEndDates({
@@ -166,7 +181,7 @@ export async function getCommissions(filters: CommissionsFilters) {
}),
customerId,
payoutId,
- ...(customerIds?.length && {
+ ...(customerIds && {
customerId: {
in: customerIds,
},
diff --git a/apps/web/ui/partners/fraud-risks/associated-commissions-table.tsx b/apps/web/ui/partners/fraud-risks/associated-commissions-table.tsx
index 46185413e3..de470b5df8 100644
--- a/apps/web/ui/partners/fraud-risks/associated-commissions-table.tsx
+++ b/apps/web/ui/partners/fraud-risks/associated-commissions-table.tsx
@@ -40,6 +40,7 @@ export function AssociatedCommissionsTable({
workspaceId: workspaceId!,
status: "hold",
fraudEventGroupId: fraudGroup.id,
+ partnerId: fraudGroup.partner.id,
};
const {