The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Match similar customer emails across the whole program
Rafi02237b437fc8
diff --git a/apps/web/lib/api/fraud/rules/check-customer-email-match.ts b/apps/web/lib/api/fraud/rules/check-customer-email-match.tsindex 554695e3d7..2414563c66 100644--- a/apps/web/lib/api/fraud/rules/check-customer-email-match.ts+++ b/apps/web/lib/api/fraud/rules/check-customer-email-match.ts@@ -6,8 +6,10 @@ import { FraudEventContext } from "@/lib/types";import { CustomerEmailMatchType } from "@/lib/zod/schemas/fraud";import { defineFraudRule } from "../define-fraud-rule";-// Partner's email matches a customer's email, shares the same email domain,-// or the customer's domain matches a previously referred customer.+// Partner's email matches a customer's email, the customer's email is a+// variant of a customer referred in the same program, the partner and the+// customer share an email domain, or the customer's domain matches a+// previously referred customer.export const checkCustomerEmailMatch = defineFraudRule({type: "customerEmailMatch",evaluate: async ({ program, partner, customer }: FraudEventContext) => {@@ -38,17 +40,17 @@ export const checkCustomerEmailMatch = defineFraudRule({("isFirstConversion" in customer && customer.isFirstConversion) ||!("isFirstConversion" in customer);- // 2. Similar email match — customer's normalized email matches- // a previously referred customer from the same partner.- // Require the raw emails to differ so duplicate rows for the same- // address (e.g. created by importers or promo-code attribution)- // don't trigger; only actual variants do.+ // 2. Similar email match: the customer's normalized email matches a+ // customer referred by any partner in the same program, because variants+ // of one inbox can come through different partners.+ // Require the raw emails to differ. Importers and promo-code attribution+ // can create duplicate rows for one address, and those should not count.if (shouldCheckHistoricalMatch) {const similarCustomer = await prisma.customer.findFirst({where: {projectId: program.workspaceId,normalizedEmail: normalizedCustomerEmail,- partnerId: partner.id,+ programId: program.id,id: {not: customer.id,},diff --git a/apps/web/tests/fraud/index.test.ts b/apps/web/tests/fraud/index.test.tsindex 6c1f829f51..7985506a6e 100644--- a/apps/web/tests/fraud/index.test.ts+++ b/apps/web/tests/fraud/index.test.ts@@ -5,6 +5,7 @@ import { randomCustomer, randomId } from "tests/utils/helpers";import {E2E_FRAUD_PARTNER,E2E_FRAUD_REFERRAL_SOURCE_BANNED_DOMAIN,+ E2E_PARTNERS,E2E_TRACK_CLICK_HEADERS,} from "tests/utils/resource";import { verifyFraudEvent } from "tests/utils/verify-fraud-event";@@ -200,6 +201,64 @@ describe.concurrent("/fraud/**", async () => {});});+ test("FraudRuleType = customerEmailMatch (similar email match across partners)", async () => {+ const localPart = randomEmailLocalPart();++ // Referred by another partner in the same program+ const firstCustomer = {+ ...randomCustomer(),+ email: `${localPart}@gmail.com`,+ clickLink: E2E_PARTNERS[0].shortLink,+ };++ const similarCustomer = {+ ...randomCustomer(),+ email: `${localPart}[email protected]`,+ clickLink: E2E_FRAUD_PARTNER.links.customerEmailMatch,+ };++ for (const customer of [firstCustomer, similarCustomer]) {+ const clickResponse = await http.post<{ clickId: string }>({+ path: "/track/click",+ headers: { ...E2E_TRACK_CLICK_HEADERS },+ body: {+ domain: customer.clickLink.domain,+ key: customer.clickLink.key,+ },+ });++ await http.post<TrackLeadResponse>({+ path: "/track/lead",+ body: {+ eventName: "Signup",+ clickId: clickResponse.data.clickId,+ customerId: customer.externalId,+ customerName: customer.name,+ customerEmail: customer.email,+ customerAvatar: customer.avatar,+ },+ });+ }++ const { data: firstCustomers } = await http.get<Customer[]>({+ path: "/customers",+ query: { externalId: firstCustomer.externalId },+ });++ expect(firstCustomers.length).toBe(1);++ await verifyFraudEvent({+ http,+ partner: E2E_FRAUD_PARTNER,+ customer: similarCustomer,+ ruleType: "customerEmailMatch",+ metadata: {+ matchType: CustomerEmailMatchType.SIMILAR_EMAIL_MATCH,+ matchedCustomerId: firstCustomers[0].id,+ },+ });+ });+test("FraudRuleType = customerEmailSuspiciousDomain", async () => {const clickLink = E2E_FRAUD_PARTNER.links.customerEmailSuspiciousDomain;