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.

Do exact email query to database

Rafi10e984776c2e
diff --git a/apps/web/lib/api/partners/program-enrollment-query.ts b/apps/web/lib/api/partners/program-enrollment-query.ts
index adfc21f2b7..99a44f5ddc 100644
--- a/apps/web/lib/api/partners/program-enrollment-query.ts
+++ b/apps/web/lib/api/partners/program-enrollment-query.ts
@@ -3,6 +3,18 @@ import { getPartnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";
import { Prisma } from "@prisma/client";
import * as z from "zod/v4";
+/**
+ * A complete address, not merely something containing "@". `steven@` is what a
+ * half-typed query looks like, and treating that as an exact lookup returns
+ * nothing instead of the partial matches the caller wanted.
+ *
+ * `@dub.co` fails this on purpose: it is a domain search, which the email index
+ * cannot serve (a trailing-wildcard match), so it belongs on the search path.
+ */
+export function isExactEmailQuery(query: string): boolean {
+ return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/u.test(query);
+}
+
/**
* Email / search filters on `Partner` (exact email, exact partner ID, or full-text on email/name/company).
*/
@@ -17,7 +29,7 @@ export function buildPartnerEmailSearchWhere({
return { email };
}
if (search) {
- if (search.includes("@")) {
+ if (isExactEmailQuery(search)) {
return { email: search };
}
if (search.startsWith("pn_")) {
diff --git a/apps/web/lib/api/partners/search/build-search-query.ts b/apps/web/lib/api/partners/search/build-search-query.ts
index f945ab4795..137e2230ea 100644
--- a/apps/web/lib/api/partners/search/build-search-query.ts
+++ b/apps/web/lib/api/partners/search/build-search-query.ts
@@ -1,3 +1,4 @@
+import { isExactEmailQuery } from "@/lib/api/partners/program-enrollment-query";
import { getPartnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";
import * as z from "zod/v4";
import {
@@ -29,7 +30,11 @@ export function buildPartnerSearchCandidateQuery({
// Exact email and tenant lookups remain database-only. The search provider
// is responsible only for finding relevance-ranked free-text candidates.
- if (!query || email || tenantId) {
+ //
+ // A search that is itself a complete address is the same lookup: `email` is a
+ // unique indexed column, so the database answers it exactly, and routing it
+ // through the provider would spend a network round trip to rank one row.
+ if (!query || email || tenantId || isExactEmailQuery(query)) {
return null;
}
diff --git a/apps/web/tests/partners/partner-search-query.test.ts b/apps/web/tests/partners/partner-search-query.test.ts
index 6516ca8522..2ea835d9f1 100644
--- a/apps/web/tests/partners/partner-search-query.test.ts
+++ b/apps/web/tests/partners/partner-search-query.test.ts
@@ -27,4 +27,27 @@ describe("buildPartnerSearchCandidateQuery", () => {
])("keeps %s on the database path", (_name, input) => {
expect(buildPartnerSearchCandidateQuery(input)).toBeNull();
});
+
+ describe("exact email queries", () => {
+ "keeps %s on the database, which indexes email",
+ (search) => {
+ expect(
+ buildPartnerSearchCandidateQuery({ programId: "prog_1", search }),
+ ).toBeNull();
+ },
+ );
+
+ it.each([
+ ["half-typed", "steven@"],
+ ["domain only", "@dub.co"],
+ ["no dot in the domain", "steven@dub"],
+ ["two words", "[email protected] other"],
+ ["a name", "steven"],
+ ])("sends %s to the provider", (_label, search) => {
+ expect(
+ buildPartnerSearchCandidateQuery({ programId: "prog_1", search }),
+ ).toMatchObject({ query: search });
+ });
+ });
});