The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix the test
Kiran K978eb0cbec47
diff --git a/apps/web/app/(ee)/api/fraud/events/route.ts b/apps/web/app/(ee)/api/fraud/events/route.tsindex f019efc811..2ed6020503 100644--- a/apps/web/app/(ee)/api/fraud/events/route.ts+++ b/apps/web/app/(ee)/api/fraud/events/route.ts@@ -6,6 +6,7 @@ import {fraudEventSchemas,} from "@/lib/zod/schemas/fraud";import { prisma } from "@dub/prisma";+import { FraudRuleType, Prisma } from "@dub/prisma/client";import { NextResponse } from "next/server";import { z } from "zod";@@ -13,32 +14,67 @@ import { z } from "zod";export const GET = withWorkspace(async ({ workspace, searchParams }) => {const programId = getDefaultProgramIdOrThrow(workspace);- const { groupId } = fraudEventQuerySchema.parse(searchParams);+ const parsedQueryParams = fraudEventQuerySchema.parse(searchParams);- const fraudEventGroup = await prisma.fraudEventGroup.findUnique({- where: {- id: groupId,- },- });+ let where: Prisma.FraudEventWhereInput = {};+ let eventGroupType: FraudRuleType | undefined;- if (!fraudEventGroup) {- throw new DubApiError({- code: "not_found",- message: "Fraud event group not found.",+ if ("groupId" in parsedQueryParams) {+ const { groupId } = parsedQueryParams;++ const fraudEventGroup = await prisma.fraudEventGroup.findUnique({+ where: {+ id: groupId,+ },+ select: {+ programId: true,+ type: true,+ },});++ if (!fraudEventGroup) {+ throw new DubApiError({+ code: "not_found",+ message: "Fraud event group not found.",+ });+ }++ if (fraudEventGroup.programId !== programId) {+ throw new DubApiError({+ code: "not_found",+ message: "Fraud event group not found in this program.",+ });+ }++ where = {+ fraudEventGroupId: groupId,+ };++ eventGroupType = fraudEventGroup.type;+ }++ if ("customerId" in parsedQueryParams && "type" in parsedQueryParams) {+ const { customerId, type } = parsedQueryParams;++ where = {+ customerId,+ fraudEventGroup: {+ type,+ },+ };++ eventGroupType = type;}- if (fraudEventGroup.programId !== programId) {+ if (!eventGroupType) {throw new DubApiError({code: "not_found",- message: "Fraud event group not found in this program.",+ message: "Fraud event group type not found.",});}const fraudEvents = await prisma.fraudEvent.findMany({- where: {- fraudEventGroupId: groupId,- },+ where,include: {partner: true,customer: true,@@ -48,7 +84,7 @@ export const GET = withWorkspace(},});- const zodSchema = fraudEventSchemas[fraudEventGroup.type];+ const zodSchema = fraudEventSchemas[eventGroupType];return NextResponse.json(z.array(zodSchema).parse(fraudEvents));},diff --git a/apps/web/lib/api/fraud/utils.ts b/apps/web/lib/api/fraud/utils.tsindex a9f1efca7e..6a32096b14 100644--- a/apps/web/lib/api/fraud/utils.ts+++ b/apps/web/lib/api/fraud/utils.ts@@ -73,17 +73,15 @@ export function createFraudEventGroupKey(input: CreateGroupKeyInput): string {return createHashKey(parts.join("|"));}-export function createFraudEventFingerprint({- programId,- partnerId,- type,- metadata,-}: CreateFingerprintInput) {+export function createFraudEventFingerprint(+ fraudEvent: CreateFingerprintInput,+) {try {+ const { programId, partnerId, type } = fraudEvent;+const identityFields = getIdentityFieldsForRule({- type,- partnerId,- metadata: metadata as Record<string, string>,+ ...fraudEvent,+ metadata: fraudEvent.metadata as Record<string, string>,});// Normalize identityFields keys so fingerprint is deterministic@@ -102,7 +100,7 @@ export function createFraudEventFingerprint({}}-export function getIdentityFieldsForRule({+function getIdentityFieldsForRule({type,partnerId,customerId,diff --git a/apps/web/lib/zod/schemas/fraud.ts b/apps/web/lib/zod/schemas/fraud.tsindex 762fcdd8e4..6d942a3350 100644--- a/apps/web/lib/zod/schemas/fraud.ts+++ b/apps/web/lib/zod/schemas/fraud.ts@@ -66,9 +66,15 @@ export const fraudEventGroupCountSchema = z.union([z.number(),]);-export const fraudEventQuerySchema = z.object({- groupId: z.string(),-});+export const fraudEventQuerySchema = z.union([+ z.object({+ groupId: z.string(),+ }),+ z.object({+ customerId: z.string(),+ type: z.nativeEnum(FraudRuleType),+ }),+]);export const resolveFraudEventGroupSchema = z.object({workspaceId: z.string(),diff --git a/apps/web/tests/fraud/index.test.ts b/apps/web/tests/fraud/index.test.tsindex 53b5aa38d4..c354f1dfb6 100644--- a/apps/web/tests/fraud/index.test.ts+++ b/apps/web/tests/fraud/index.test.ts@@ -196,39 +196,38 @@ const verifyFraudEvent = async ({// Wait until fraud event is availableconst fraudEvent = await waitForFraudEvent({http,- partnerId: E2E_FRAUD_PARTNER.id,customerId: customers[0].id,ruleType,});// Assert fraud event shapeexpect(fraudEvent).toStrictEqual({- id: expect.any(String),- type: ruleType,- status: "pending",- resolutionReason: null,- resolvedAt: null,- lastOccurrenceAt: expect.any(String),- count: 1,- groupKey: expect.any(String),- partner: {- id: E2E_FRAUD_PARTNER.id,- name: expect.any(String),- email: expect.any(String),- image: null,- },- user: null,+ createdAt: expect.any(String),+ customer: expect.objectContaining({+ id: customers[0].id,+ name: customers[0].name,+ email: customers[0].email,+ avatar: customers[0].avatar,+ }),+ ...(ruleType === "paidTrafficDetected" && {+ metadata: {+ source: "google",+ },+ }),+ ...(ruleType === "referralSourceBanned" && {+ metadata: {+ source: E2E_FRAUD_REFERRAL_SOURCE_BANNED_DOMAIN,+ },+ }),});};async function waitForFraudEvent({http,- partnerId,customerId,ruleType,}: {http: HttpClient;- partnerId: string;customerId: string;ruleType: FraudRuleType;}) {@@ -237,9 +236,8 @@ async function waitForFraudEvent({const { data } = await http.get<fraudEventGroupProps[]>({path: "/fraud/events",query: {- partnerId,- type: ruleType,customerId,+ type: ruleType,},});