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.

Filter fraud commissions by eventId and trigger workflows on release

Kiran K7a7c05c7b039
diff --git a/apps/web/lib/api/commissions/get-commissions-count.ts b/apps/web/lib/api/commissions/get-commissions-count.ts
index 740ea9fe3f..c73e337916 100644
--- a/apps/web/lib/api/commissions/get-commissions-count.ts
+++ b/apps/web/lib/api/commissions/get-commissions-count.ts
@@ -30,27 +30,26 @@ export async function getCommissionsCount(filters: CommissionsCountFilters) {
programId,
} = filters;
- // Filter the commissions count based on the risk event group
- let customerIds: string[] | undefined;
+ // Filter the commissions based on the risk event group
+ let eventIds: string[] | undefined;
if (fraudEventGroupId) {
- const riskEvents = await prisma.fraudEvent.findMany({
+ const riskEventGroup = await prisma.fraudEventGroup.findUniqueOrThrow({
where: {
- fraudEventGroupId,
+ id: fraudEventGroupId,
programId,
},
select: {
- customerId: true,
- fraudEventGroup: {
+ type: true,
+ fraudEvents: {
select: {
- type: true,
+ eventId: true,
},
},
},
});
- if (riskEvents.length > 0) {
- const riskEventGroup = riskEvents[0].fraudEventGroup;
+ if (riskEventGroup.fraudEvents.length > 0) {
const isPartnerLevelRisk = PARTNER_LEVEL_FRAUD_RULES.includes(
riskEventGroup.type as (typeof PARTNER_LEVEL_FRAUD_RULES)[number],
);
@@ -58,7 +57,9 @@ export async function getCommissionsCount(filters: CommissionsCountFilters) {
// 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!);
+ eventIds = riskEventGroup.fraudEvents
+ .map((e) => e.eventId)
+ .filter((e) => e !== null) as string[];
}
}
}
@@ -127,9 +128,9 @@ export async function getCommissionsCount(filters: CommissionsCountFilters) {
? { notIn: customerFilter.values }
: { in: customerFilter.values },
}),
- ...(customerIds && {
- customerId: {
- in: customerIds,
+ ...(eventIds && {
+ eventId: {
+ in: eventIds,
},
}),
createdAt: {
diff --git a/apps/web/lib/api/commissions/get-commissions.ts b/apps/web/lib/api/commissions/get-commissions.ts
index 1a0ccf3923..ffb22ee105 100644
--- a/apps/web/lib/api/commissions/get-commissions.ts
+++ b/apps/web/lib/api/commissions/get-commissions.ts
@@ -62,26 +62,25 @@ export async function getCommissions(filters: CommissionsFilters) {
}
// Filter the commissions based on the risk event group
- let customerIds: string[] | undefined;
+ let eventIds: string[] | undefined;
if (fraudEventGroupId) {
- const riskEvents = await prisma.fraudEvent.findMany({
+ const riskEventGroup = await prisma.fraudEventGroup.findUniqueOrThrow({
where: {
- fraudEventGroupId,
+ id: fraudEventGroupId,
programId,
},
select: {
- customerId: true,
- fraudEventGroup: {
+ type: true,
+ fraudEvents: {
select: {
- type: true,
+ eventId: true,
},
},
},
});
- if (riskEvents.length > 0) {
- const riskEventGroup = riskEvents[0].fraudEventGroup;
+ if (riskEventGroup.fraudEvents.length > 0) {
const isPartnerLevelRisk = PARTNER_LEVEL_FRAUD_RULES.includes(
riskEventGroup.type as (typeof PARTNER_LEVEL_FRAUD_RULES)[number],
);
@@ -89,7 +88,9 @@ export async function getCommissions(filters: CommissionsFilters) {
// 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!);
+ eventIds = riskEventGroup.fraudEvents
+ .map((e) => e.eventId)
+ .filter((e) => e !== null) as string[];
}
}
}
@@ -181,9 +182,9 @@ export async function getCommissions(filters: CommissionsFilters) {
}),
customerId,
payoutId,
- ...(customerIds && {
- customerId: {
- in: customerIds,
+ ...(eventIds && {
+ eventId: {
+ in: eventIds,
},
}),
createdAt: {
diff --git a/apps/web/lib/api/commissions/update-partner-commission.ts b/apps/web/lib/api/commissions/update-partner-commission.ts
index 6c5888d3fd..5ee3f0fc9f 100644
--- a/apps/web/lib/api/commissions/update-partner-commission.ts
+++ b/apps/web/lib/api/commissions/update-partner-commission.ts
@@ -232,7 +232,7 @@ export async function updatePartnerCommission({
partnerId: commission.partnerId,
customerId: commission.customerId,
status: {
- in: ["pending", "processed"],
+ in: ["pending", "processed", "hold"],
},
id: {
not: commission.id,
diff --git a/apps/web/lib/api/fraud/release-hold-commissions.ts b/apps/web/lib/api/fraud/release-hold-commissions.ts
index 8664d03b36..c593b7a3cf 100644
--- a/apps/web/lib/api/fraud/release-hold-commissions.ts
+++ b/apps/web/lib/api/fraud/release-hold-commissions.ts
@@ -1,5 +1,6 @@
import { trackCommissionStatusUpdate } from "@/lib/api/commissions/track-commission-update-activity-log";
import { syncTotalCommissions } from "@/lib/api/partners/sync-total-commissions";
+import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { prisma } from "@/lib/prisma";
import { FraudEventStatus, FraudRuleType, Prisma } from "@prisma/client";
import { FRAUD_RULES_BY_TYPE, PARTNER_LEVEL_FRAUD_RULES } from "./constants";
@@ -139,6 +140,28 @@ export async function releaseHoldCommissions(riskGroupIds: string[]) {
programId,
});
+ const releasedEarnings = commissionsToRelease.reduce(
+ (sum, commission) => sum + commission.earnings,
+ 0,
+ );
+
+ if (releasedEarnings > 0) {
+ await executeWorkflows({
+ trigger: "partnerMetricsUpdated",
+ reason: "commission",
+ identity: {
+ workspaceId: groupsForPartner[0].program.workspaceId,
+ programId,
+ partnerId,
+ },
+ metrics: {
+ current: {
+ commissions: releasedEarnings,
+ },
+ },
+ });
+ }
+
releasedCount += commissionsToRelease.length;
}
diff --git a/apps/web/lib/zod/schemas/payouts.ts b/apps/web/lib/zod/schemas/payouts.ts
index 59c1de0c2a..55f66b675b 100644
--- a/apps/web/lib/zod/schemas/payouts.ts
+++ b/apps/web/lib/zod/schemas/payouts.ts
@@ -73,7 +73,6 @@ export const payoutsCountQuerySchema = payoutsQuerySchema
partnerId: true,
invoiceId: true,
groupId: true,
- tenantId: true,
})
.extend({
programId: z.string().optional(),