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.

Store normalized customer emails and detect similar-email fraud matches

Kiran Ka06ef3c25578
diff --git a/apps/web/app/(ee)/api/customers/[id]/route.ts b/apps/web/app/(ee)/api/customers/[id]/route.ts
index 041dbfaaac..e8efd2c574 100644
--- a/apps/web/app/(ee)/api/customers/[id]/route.ts
+++ b/apps/web/app/(ee)/api/customers/[id]/route.ts
@@ -3,6 +3,7 @@ import { transformCustomer } from "@/lib/api/customers/transform-customer";
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { isStored, storage } from "@/lib/storage";
import {
@@ -89,6 +90,9 @@ export const PATCH = withWorkspace(
data: {
name,
email,
+ ...(email !== undefined && {
+ normalizedEmail: getNormalizedEmail(email),
+ }),
avatar: finalCustomerAvatar,
externalId,
stripeCustomerId,
diff --git a/apps/web/app/(ee)/api/customers/route.ts b/apps/web/app/(ee)/api/customers/route.ts
index c6f7a4c15e..cb9f350233 100644
--- a/apps/web/app/(ee)/api/customers/route.ts
+++ b/apps/web/app/(ee)/api/customers/route.ts
@@ -5,6 +5,7 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { getCustomers } from "@/lib/customers/api/get-customers";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { generateRandomName } from "@/lib/names";
import { prisma } from "@/lib/prisma";
import { isStored, storage } from "@/lib/storage";
@@ -73,6 +74,7 @@ export const POST = withWorkspace(
id: customerId,
name: finalCustomerName,
email,
+ normalizedEmail: getNormalizedEmail(email),
avatar: finalCustomerAvatar,
externalId,
stripeCustomerId,
diff --git a/apps/web/app/(ee)/api/shopify/integration/webhook/customers-redact.ts b/apps/web/app/(ee)/api/shopify/integration/webhook/customers-redact.ts
index c771d4cca6..eee8b4ce29 100644
--- a/apps/web/app/(ee)/api/shopify/integration/webhook/customers-redact.ts
+++ b/apps/web/app/(ee)/api/shopify/integration/webhook/customers-redact.ts
@@ -39,6 +39,7 @@ export async function customersRedact({
data: {
name: generateRandomName(),
email: null,
+ normalizedEmail: null,
},
});
} catch (error) {
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index fc7284c2d1..02f6be6dcd 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -5,6 +5,7 @@ import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer"
import { includeTags } from "@/lib/api/links/include-tags";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
import { sendPartnerPostback } from "@/lib/postback/send-partner-postback";
@@ -83,6 +84,9 @@ export async function checkoutSessionCompleted({
const payload = {
name: stripeCustomerName,
email: stripeCustomerEmail,
+ ...(stripeCustomerEmail !== undefined && {
+ normalizedEmail: getNormalizedEmail(stripeCustomerEmail),
+ }),
// stripeCustomerId can potentially be null, so we use email as fallback
externalId: stripeCustomerId || stripeCustomerEmail,
projectId: workspace.id,
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
index 48b7125975..fb0605148b 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
@@ -2,6 +2,7 @@ import { createId } from "@/lib/api/create-id";
import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
@@ -132,6 +133,7 @@ export async function attributeViaPromotionCodeId({
name:
customerDetails.name || customerDetails.email || generateRandomName(),
email: customerDetails.email,
+ normalizedEmail: getNormalizedEmail(customerDetails.email),
externalId: clickEvent.click_id,
stripeCustomerId,
linkId: clickEvent.link_id,
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
index 2b6990632a..f977429e92 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/sync-customer.ts
@@ -3,6 +3,7 @@ import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer"
import { includeTags } from "@/lib/api/links/include-tags";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { constructWebhookPartner } from "@/lib/partners/constuct-webhook-partner";
@@ -72,7 +73,10 @@ export async function syncCustomer({
externalId: dubCustomerExternalId,
// Only update name/email when Stripe provides non-empty values
...(stripeCustomer.name && { name: stripeCustomer.name }),
- ...(stripeCustomer.email && { email: stripeCustomer.email }),
+ ...(stripeCustomer.email && {
+ email: stripeCustomer.email,
+ normalizedEmail: getNormalizedEmail(stripeCustomer.email),
+ }),
},
});
}
@@ -137,6 +141,7 @@ export async function syncCustomer({
id: createId({ prefix: "cus_" }),
name: stripeCustomer.name || stripeCustomer.email || generateRandomName(),
email: stripeCustomer.email,
+ normalizedEmail: getNormalizedEmail(stripeCustomer.email),
stripeCustomerId: stripeCustomer.id,
projectConnectId: stripeAccountId,
externalId: dubCustomerExternalId,
@@ -162,7 +167,10 @@ export async function syncCustomer({
externalId: dubCustomerExternalId,
// Only update name/email when Stripe provides non-empty values
...(stripeCustomer.name && { name: stripeCustomer.name }),
- ...(stripeCustomer.email && { email: stripeCustomer.email }),
+ ...(stripeCustomer.email && {
+ email: stripeCustomer.email,
+ normalizedEmail: getNormalizedEmail(stripeCustomer.email),
+ }),
},
});
}
diff --git a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
index 6631fa2d01..afbe134295 100644
--- a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
+++ b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
@@ -605,7 +605,7 @@ async function stepRunSideEffects(
if (shouldRunRiskMonitoring) {
const triggeredRules = await detectAndRecordFraudEvent({
- program: { id: programId },
+ program: { id: programId, workspaceId: workspace.id },
partner: pick(programEnrollment.partner, ["id", "email", "name"]),
programEnrollment: pick(programEnrollment, [
"status",
diff --git a/apps/web/lib/api/commissions/create-manual-commissions.ts b/apps/web/lib/api/commissions/create-manual-commissions.ts
index 113ca87a47..cc0343a2ad 100644
--- a/apps/web/lib/api/commissions/create-manual-commissions.ts
+++ b/apps/web/lib/api/commissions/create-manual-commissions.ts
@@ -1,6 +1,7 @@
import { convertCurrency } from "@/lib/analytics/convert-currency";
import { isFirstConversion } from "@/lib/analytics/is-first-conversion";
import { Session } from "@/lib/auth";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
import { prisma } from "@/lib/prisma";
@@ -486,6 +487,7 @@ async function createOrUpdateCustomer({
id,
name,
email,
+ normalizedEmail: getNormalizedEmail(email),
avatar,
externalId,
stripeCustomerId,
@@ -508,6 +510,9 @@ async function createOrUpdateCustomer({
data: {
name,
email,
+ ...(email !== undefined && {
+ normalizedEmail: getNormalizedEmail(email),
+ }),
avatar,
country,
stripeCustomerId,
diff --git a/apps/web/lib/api/conversions/track-lead.ts b/apps/web/lib/api/conversions/track-lead.ts
index 25f22f2277..3df85e3ffb 100644
--- a/apps/web/lib/api/conversions/track-lead.ts
+++ b/apps/web/lib/api/conversions/track-lead.ts
@@ -2,6 +2,7 @@ import { createId } from "@/lib/api/create-id";
import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
@@ -183,6 +184,7 @@ export const trackLead = async ({
id: finalCustomerId,
name: finalCustomerName,
email: customerEmail,
+ normalizedEmail: getNormalizedEmail(customerEmail),
avatar: finalCustomerAvatar,
externalId: customerExternalId,
projectId: workspace.id,
diff --git a/apps/web/lib/api/conversions/track-sale.ts b/apps/web/lib/api/conversions/track-sale.ts
index aa8c857ef5..6b431397d9 100644
--- a/apps/web/lib/api/conversions/track-sale.ts
+++ b/apps/web/lib/api/conversions/track-sale.ts
@@ -3,6 +3,7 @@ import { isFirstConversion } from "@/lib/analytics/is-first-conversion";
import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
@@ -223,6 +224,7 @@ export const trackSale = async ({
id: finalCustomerId,
name: finalCustomerName,
email: customerEmail,
+ normalizedEmail: getNormalizedEmail(customerEmail),
avatar: finalCustomerAvatar,
externalId: customerExternalId,
linkId: clickData.link_id,
diff --git a/apps/web/lib/api/customers/reattribute-customer.ts b/apps/web/lib/api/customers/reattribute-customer.ts
index ef9de21d3d..0a667d0036 100644
--- a/apps/web/lib/api/customers/reattribute-customer.ts
+++ b/apps/web/lib/api/customers/reattribute-customer.ts
@@ -163,6 +163,7 @@ export async function recreateCustomerForReattribution({
id: newCustomerId,
name: customer.name,
email: customer.email,
+ normalizedEmail: customer.normalizedEmail,
avatar: customer.avatar,
externalId: customer.externalId,
stripeCustomerId: customer.stripeCustomerId,
diff --git a/apps/web/lib/api/demo/create-demo-commission.ts b/apps/web/lib/api/demo/create-demo-commission.ts
index 85660836d1..a73e6c7bf2 100644
--- a/apps/web/lib/api/demo/create-demo-commission.ts
+++ b/apps/web/lib/api/demo/create-demo-commission.ts
@@ -3,6 +3,7 @@ import { createId } from "@/lib/api/create-id";
import { DubApiError } from "@/lib/api/errors";
import { updateLinkStatsForImporter } from "@/lib/api/links/update-link-stats-for-importer";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
import { EdgeLinkProps } from "@/lib/planetscale/types";
@@ -136,6 +137,7 @@ export async function createDemoCommission({
id: customerId,
name: customer.name || customer.email || generateRandomName(),
email: customer.email,
+ normalizedEmail: getNormalizedEmail(customer.email),
externalId: customer.externalId,
country: customer.country,
linkId: targetLink.id,
@@ -146,6 +148,9 @@ export async function createDemoCommission({
update: {
name: customer.name || customer.email || generateRandomName(),
email: customer.email,
+ ...(customer.email !== undefined && {
+ normalizedEmail: getNormalizedEmail(customer.email),
+ }),
country: customer.country,
},
});
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.ts
index 37ad7c719a..205d64eb62 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
@@ -1,25 +1,27 @@
import { extractEmailDomain } from "@/lib/email/extract-email-domain";
import { isGenericEmail } from "@/lib/email/is-generic-email";
+import { normalizeEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { FraudEventContext } from "@/lib/types";
import { CustomerEmailMatchType } from "@/lib/zod/schemas/fraud";
import { defineFraudRule } from "../define-fraud-rule";
-import { normalizeEmail } from "../utils";
// Partner's email matches a customer's email, shares the same email domain,
// or the customer's domain matches a previously referred customer.
export const checkCustomerEmailMatch = defineFraudRule({
type: "customerEmailMatch",
evaluate: async ({ program, partner, customer }: FraudEventContext) => {
- // Return false if either email is missing
- if (!partner.email || !customer.email) {
+ // Return false if the customer email is missing
+ if (!customer.email) {
return {
triggered: false,
};
}
// Normalize both emails
- const normalizedPartnerEmail = normalizeEmail(partner.email);
+ const normalizedPartnerEmail = partner.email
+ ? normalizeEmail(partner.email)
+ : null;
const normalizedCustomerEmail = normalizeEmail(customer.email);
// 1. Exact email match (strongest signal)
@@ -32,6 +34,45 @@ export const checkCustomerEmailMatch = defineFraudRule({
};
}
+ const shouldCheckHistoricalMatch =
+ ("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
+ if (shouldCheckHistoricalMatch) {
+ const similarCustomer = await prisma.customer.findFirst({
+ where: {
+ projectId: program.workspaceId,
+ normalizedEmail: normalizedCustomerEmail,
+ partnerId: partner.id,
+ id: {
+ not: customer.id,
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (similarCustomer) {
+ return {
+ triggered: true,
+ metadata: {
+ matchType: CustomerEmailMatchType.SIMILAR_EMAIL_MATCH,
+ matchedCustomerId: similarCustomer.id,
+ },
+ };
+ }
+ }
+
+ // Return false if the partner email is missing
+ if (!partner.email) {
+ return {
+ triggered: false,
+ };
+ }
+
// Extract domains for domain-level checks
const partnerEmailDomain = extractEmailDomain(partner.email);
const customerEmailDomain = extractEmailDomain(customer.email);
@@ -49,7 +90,7 @@ export const checkCustomerEmailMatch = defineFraudRule({
};
}
- // 2. Partner-customer domain match
+ // 3. Partner-customer domain match
if (partnerEmailDomain === customerEmailDomain) {
return {
triggered: true,
@@ -59,14 +100,9 @@ export const checkCustomerEmailMatch = defineFraudRule({
};
}
- // 3. Historical domain match — customer's email domain matches
+ // 4. Historical domain match — customer's email domain matches
// a previously referred customer from the same partner
-
- const shouldCheckHistoricalDomainMatch =
- ("isFirstConversion" in customer && customer.isFirstConversion) ||
- !("isFirstConversion" in customer);
-
- if (shouldCheckHistoricalDomainMatch) {
+ if (shouldCheckHistoricalMatch) {
const previousCustomer = await prisma.customer.findFirst({
where: {
programId: program.id,
diff --git a/apps/web/lib/api/fraud/utils.ts b/apps/web/lib/api/fraud/utils.ts
index 917a76f5a2..b9d839868b 100644
--- a/apps/web/lib/api/fraud/utils.ts
+++ b/apps/web/lib/api/fraud/utils.ts
@@ -16,31 +16,6 @@ type GetIdentityFieldsForFraudEventInput = Pick<
"type" | "partnerId" | "customerId" | "sourceProgramId" | "metadata"
>;
-// Normalize email for comparison
-export function normalizeEmail(email: string) {
- const trimmed = email.toLowerCase().trim();
- const parts = trimmed.split("@");
-
- if (parts.length !== 2) {
- return trimmed;
- }
-
- let [username, domain] = parts;
-
- // Strip plus addressing for all domains
- const plusIndex = username.indexOf("+");
- if (plusIndex !== -1) {
- username = username.substring(0, plusIndex);
- }
-
- // Gmail and Google Mail treat dots as irrelevant
- if (domain === "gmail.com" || domain === "googlemail.com") {
- username = username.replace(/\./g, "");
- }
-
- return `${username}@${domain}`;
-}
-
function createHashKey(value: string): string {
return createHash("sha256").update(value).digest("base64url").slice(0, 24);
}
diff --git a/apps/web/lib/application-events/mark-application-event-submitted-network.ts b/apps/web/lib/application-events/mark-application-event-submitted-network.ts
index 629d2fc391..12ad658f9f 100644
--- a/apps/web/lib/application-events/mark-application-event-submitted-network.ts
+++ b/apps/web/lib/application-events/mark-application-event-submitted-network.ts
@@ -1,4 +1,5 @@
import { createId } from "@/lib/api/create-id";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { nanoid, NETWORK_PROGRAM_ID, NETWORK_WORKSPACE_ID } from "@dub/utils";
import { Partner } from "@prisma/client";
@@ -108,6 +109,7 @@ export async function markApplicationEventSubmittedNetwork(
id: createId({ prefix: "cus_" }),
name: partner.name,
email: partner.email,
+ normalizedEmail: getNormalizedEmail(partner.email),
avatar: partner.image,
externalId: partner.id,
country: partner.country,
diff --git a/apps/web/lib/email/normalize-email.ts b/apps/web/lib/email/normalize-email.ts
new file mode 100644
index 0000000000..b0224369cd
--- /dev/null
+++ b/apps/web/lib/email/normalize-email.ts
@@ -0,0 +1,35 @@
+// Normalize email for comparison
+export function normalizeEmail(email: string) {
+ const trimmed = email.toLowerCase().trim();
+ const parts = trimmed.split("@");
+
+ if (parts.length !== 2) {
+ return trimmed;
+ }
+
+ let [username, domain] = parts;
+
+ // Strip plus addressing for all domains
+ const plusIndex = username.indexOf("+");
+ if (plusIndex !== -1) {
+ username = username.substring(0, plusIndex);
+ }
+
+ // Strip separators commonly used to create variants of the same address
+ username = username.replace(/[._-]/g, "");
+
+ if (!username) {
+ return trimmed;
+ }
+
+ // Gmail and Google Mail treat dots as irrelevant
+ if (domain === "googlemail.com") {
+ domain = "gmail.com";
+ }
+
+ return `${username}@${domain}`;
+}
+
+export function getNormalizedEmail(email: string | null | undefined) {
+ return email ? normalizeEmail(email) : null;
+}
diff --git a/apps/web/lib/firstpromoter/import-customers.ts b/apps/web/lib/firstpromoter/import-customers.ts
index d0736cb2ce..f169852725 100644
--- a/apps/web/lib/firstpromoter/import-customers.ts
+++ b/apps/web/lib/firstpromoter/import-customers.ts
@@ -1,3 +1,4 @@
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { chunk, nanoid, sleep } from "@dub/utils";
import { Customer, Link, Project } from "@prisma/client";
@@ -273,6 +274,7 @@ async function createCustomer({
id: customerId,
name: customerName,
email: customer.email,
+ normalizedEmail: getNormalizedEmail(customer.email),
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
clickId: clickEvent.click_id,
diff --git a/apps/web/lib/integrations/shopify/attribute-via-discount-code.ts b/apps/web/lib/integrations/shopify/attribute-via-discount-code.ts
index 9366634597..7e2181edd5 100644
--- a/apps/web/lib/integrations/shopify/attribute-via-discount-code.ts
+++ b/apps/web/lib/integrations/shopify/attribute-via-discount-code.ts
@@ -2,6 +2,7 @@ import { createId } from "@/lib/api/create-id";
import { includeTags } from "@/lib/api/links/include-tags";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
@@ -53,6 +54,7 @@ export async function attributeViaDiscountCode({
? `${orderCustomer.first_name} ${orderCustomer.last_name}`.trim()
: generateRandomName(),
email: orderCustomer?.email,
+ normalizedEmail: getNormalizedEmail(orderCustomer?.email),
externalId: orderCustomer?.id?.toString() || customerId,
linkId: link.id,
clickId,
diff --git a/apps/web/lib/integrations/shopify/create-lead.ts b/apps/web/lib/integrations/shopify/create-lead.ts
index e0ab241752..460ce40788 100644
--- a/apps/web/lib/integrations/shopify/create-lead.ts
+++ b/apps/web/lib/integrations/shopify/create-lead.ts
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { includeTags } from "@/lib/api/links/include-tags";
import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { generateRandomName } from "@/lib/names";
import { sendPartnerPostback } from "@/lib/postback/send-partner-postback";
import { prisma } from "@/lib/prisma";
@@ -82,6 +83,7 @@ export async function createShopifyLead({
externalId,
name,
email,
+ normalizedEmail: getNormalizedEmail(email),
projectId: workspaceId,
programId: partnerLink.programId,
partnerId: partnerLink.partnerId,
diff --git a/apps/web/lib/lemonsqueezy/import-customers.ts b/apps/web/lib/lemonsqueezy/import-customers.ts
index 25a6c6dbd0..c71578a004 100644
--- a/apps/web/lib/lemonsqueezy/import-customers.ts
+++ b/apps/web/lib/lemonsqueezy/import-customers.ts
@@ -1,3 +1,4 @@
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { chunk, nanoid } from "@dub/utils";
import { Customer, Link, Project } from "@prisma/client";
@@ -240,6 +241,7 @@ async function createCustomer({
id: createId({ prefix: "cus_" }),
name: customer.name || customer.email,
email: customer.email,
+ normalizedEmail: getNormalizedEmail(customer.email),
externalId,
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.ts
index 798a8fa58c..766a58f529 100644
--- a/apps/web/lib/partnerstack/import-customers.ts
+++ b/apps/web/lib/partnerstack/import-customers.ts
@@ -1,3 +1,4 @@
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { chunk, nanoid, sleep } from "@dub/utils";
import { Customer, Link, Project } from "@prisma/client";
@@ -289,6 +290,7 @@ async function createCustomer({
? customer.email || customer.customer_key
: customer.name,
email: customer.email,
+ normalizedEmail: getNormalizedEmail(customer.email),
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
clickId: clickEvent.click_id,
diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.ts
index f31234f594..71057abd04 100644
--- a/apps/web/lib/rewardful/import-customers.ts
+++ b/apps/web/lib/rewardful/import-customers.ts
@@ -1,3 +1,4 @@
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { chunk, nanoid } from "@dub/utils";
import { Customer, Program, Project } from "@prisma/client";
@@ -246,6 +247,7 @@ async function createCustomer({
? referral.customer.email
: referral.customer.name,
email: referral.customer.email,
+ normalizedEmail: getNormalizedEmail(referral.customer.email),
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
clickId: clickEvent.click_id,
diff --git a/apps/web/lib/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.ts
index 56392836db..8046a2d1e3 100644
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@ -1,3 +1,4 @@
+import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { prisma } from "@/lib/prisma";
import { chunk, nanoid, sleep } from "@dub/utils";
import { Customer, Link, Project } from "@prisma/client";
@@ -242,6 +243,7 @@ async function createReferral({
? customer.email
: customer.name,
email: customer.email,
+ normalizedEmail: getNormalizedEmail(customer.email),
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
clickId: clickEvent.click_id,
diff --git a/apps/web/lib/zod/schemas/fraud.ts b/apps/web/lib/zod/schemas/fraud.ts
index 0f333d60d8..073df3cefc 100644
--- a/apps/web/lib/zod/schemas/fraud.ts
+++ b/apps/web/lib/zod/schemas/fraud.ts
@@ -11,6 +11,7 @@ export enum CustomerEmailMatchType {
EXACT = "exact",
DOMAIN_MATCH = "domainMatch",
HISTORICAL_DOMAIN_MATCH = "historicalDomainMatch",
+ SIMILAR_EMAIL_MATCH = "similarEmailMatch",
}
export const fraudGroupSchema = z.object({
@@ -292,6 +293,7 @@ export const fraudEventSchemas = {
metadata: z
.object({
matchType: z.enum(CustomerEmailMatchType),
+ matchedCustomerId: z.string().optional(),
})
.nullable()
.optional(),
diff --git a/apps/web/lib/zod/schemas/schemas.ts b/apps/web/lib/zod/schemas/schemas.ts
index b22edff4fb..ff9ab1996e 100644
--- a/apps/web/lib/zod/schemas/schemas.ts
+++ b/apps/web/lib/zod/schemas/schemas.ts
@@ -4,6 +4,7 @@ import { ProgramEnrollmentSchema } from "./programs";
export const fraudEventContext = z.object({
program: z.object({
id: z.string(),
+ workspaceId: z.string(),
}),
partner: z.object({
id: z.string(),
diff --git a/apps/web/prisma/schema/customer.prisma b/apps/web/prisma/schema/customer.prisma
index ce74dc2a9d..8f12b45d50 100644
--- a/apps/web/prisma/schema/customer.prisma
+++ b/apps/web/prisma/schema/customer.prisma
@@ -2,6 +2,7 @@ model Customer {
id String @id
name String?
email String?
+ normalizedEmail String?
avatar String? @db.Text
externalId String?
stripeCustomerId String? @unique
@@ -36,6 +37,7 @@ model Customer {
@@unique([projectId, externalId])
@@index([projectId, email])
+ @@index([projectId, normalizedEmail])
@@index([projectId, createdAt])
@@index([projectId, saleAmount])
@@index([projectId, firstSaleAt])
diff --git a/apps/web/tests/misc/normalize-email.test.ts b/apps/web/tests/misc/normalize-email.test.ts
new file mode 100644
index 0000000000..072267ab48
--- /dev/null
+++ b/apps/web/tests/misc/normalize-email.test.ts
@@ -0,0 +1,64 @@
+import {
+ getNormalizedEmail,
+ normalizeEmail,
+} from "@/lib/email/normalize-email";
+import { describe, expect, it } from "vitest";
+
+describe("normalizeEmail", () => {
+ it.each([
+ ["[email protected]", "[email protected]", "gmail dots"],
+ ["[email protected]", "[email protected]", "gmail dots everywhere"],
+ ["[email protected]", "[email protected]", "underscore"],
+ ["[email protected]", "[email protected]", "dots on any domain"],
+ ["[email protected]", "[email protected]", "plus tag with separators"],
+ ["[email protected]", "[email protected]", "googlemail alias"],
+ [" [email protected] ", "[email protected]", "case and whitespace"],
+ [
+ "keeps domain dots",
+ ],
+ ])("%s -> %s (%s)", (input, expected) => {
+ expect(normalizeEmail(input)).toBe(expected);
+ });
+
+ it.each([
+ ["not-an-email", "not-an-email", "no @"],
+ ["a@[email protected]", "a@[email protected]", "multiple @"],
+ ["[email protected]", "[email protected]", "local part empty after stripping"],
+ ["[email protected]", "[email protected]", "local part empty after plus strip"],
+ ])("returns trimmed lowercase input for %s (%s)", (input, expected) => {
+ expect(normalizeEmail(input)).toBe(expected);
+ });
+
+ it.each([
+ ])("treats %s and %s as the same address", (a, b) => {
+ expect(normalizeEmail(a)).toBe(normalizeEmail(b));
+ });
+
+ it("keeps distinct addresses distinct", () => {
+ expect(normalizeEmail("[email protected]")).not.toBe(
+ normalizeEmail("[email protected]"),
+ );
+ expect(normalizeEmail("[email protected]")).not.toBe(
+ normalizeEmail("[email protected]"),
+ );
+ });
+});
+
+describe("getNormalizedEmail", () => {
+ it("returns null for empty values", () => {
+ expect(getNormalizedEmail(null)).toBeNull();
+ expect(getNormalizedEmail(undefined)).toBeNull();
+ expect(getNormalizedEmail("")).toBeNull();
+ });
+
+ it("normalizes non-empty values", () => {
+ expect(getNormalizedEmail("[email protected]")).toBe("[email protected]");
+ });
+});
diff --git a/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx b/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
index 05323dcbef..6e7a5a80ad 100644
--- a/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
+++ b/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
@@ -25,6 +25,10 @@ function getRowHref(row: EventDataProps, workspaceSlug?: string) {
return `/${workspaceSlug}/program/customers?partnerId=${row.partner.id}&search=${extractEmailDomain(row.customer.email)}`;
}
+ if (row.metadata?.matchType === CustomerEmailMatchType.SIMILAR_EMAIL_MATCH) {
+ return `/${workspaceSlug}/program/customers?partnerId=${row.partner.id}`;
+ }
+
return `/${workspaceSlug}/events?event=leads&interval=all&customerId=${row.customer.id}`;
}
@@ -32,6 +36,7 @@ const MATCH_TYPE_LABELS: Record<CustomerEmailMatchType, string> = {
[CustomerEmailMatchType.EXACT]: "Exact email match",
[CustomerEmailMatchType.DOMAIN_MATCH]: "Domain match",
[CustomerEmailMatchType.HISTORICAL_DOMAIN_MATCH]: "Historical domain match",
+ [CustomerEmailMatchType.SIMILAR_EMAIL_MATCH]: "Similar email match",
};
export function FraudMatchingCustomerEmailTable({