The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Keep a complete email out of the candidate query
Raficd6eb5d5f332
diff --git a/apps/web/lib/api/partners/search/build-search-query.ts b/apps/web/lib/api/partners/search/build-search-query.tsindex 1a9453d4e4..6df925947a 100644--- a/apps/web/lib/api/partners/search/build-search-query.ts+++ b/apps/web/lib/api/partners/search/build-search-query.ts@@ -1,4 +1,7 @@-import { isExactPartnerIdQuery } from "@/lib/api/partners/program-enrollment-query";+import {+ isExactEmailQuery,+ isExactPartnerIdQuery,+} from "@/lib/api/partners/program-enrollment-query";import { getPartnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";import * as z from "zod/v4";import {@@ -62,10 +65,15 @@ export function buildPartnerSearchCandidateQuery({// Exact email and tenant lookups remain database-only. The search provider// is responsible only for finding relevance-ranked free-text candidates.//- // A pasted partner ID joins them, and unlike a complete email it needs no- // fallback: the provider indexes the ID as a plain token, so a miss there is- // the same miss the primary key already gave, without the round trip.- if (!query || email || tenantId || isExactPartnerIdQuery(query)) {+ // A complete email address or a pasted partner ID in the search joins them.+ // The database matches both exactly, so a miss returns no partners.+ if (+ !query ||+ email ||+ tenantId ||+ isExactEmailQuery(query) ||+ isExactPartnerIdQuery(query)+ ) {return null;}diff --git a/apps/web/lib/api/partners/search/find-candidates.ts b/apps/web/lib/api/partners/search/find-candidates.tsindex 71f1d7b86b..6a934335d8 100644--- a/apps/web/lib/api/partners/search/find-candidates.ts+++ b/apps/web/lib/api/partners/search/find-candidates.ts@@ -1,4 +1,3 @@-import { isExactEmailQuery } from "@/lib/api/partners/program-enrollment-query";import type {PartnerSearchCandidateQuery,PartnerSearchProvider,@@ -14,19 +13,14 @@ interface FindPartnerSearchCandidatesOptions {}/**- * Returns ranked candidates from the search provider, or null to use the- * database search path instead. We will use the database when the provider- * fails, or when the query is a complete email address to match exactly.+ * Returns ranked candidates from the search provider. We will use the+ * database search path instead, and return null, when the provider fails.*/export async function findPartnerSearchCandidates(searchProvider: PartnerSearchProvider,query: PartnerSearchCandidateQuery,{ throwOnError = false }: FindPartnerSearchCandidatesOptions = {},): Promise<PartnerSearchResult | null> {- if (isExactEmailQuery(query.query)) {- return null;- }-try {return await searchProvider.searchCandidates(query);} catch (error) {diff --git a/apps/web/tests/partners/partner-search-find-candidates.test.ts b/apps/web/tests/partners/partner-search-find-candidates.test.tsindex 3a8095ebb0..269016870e 100644--- a/apps/web/tests/partners/partner-search-find-candidates.test.ts+++ b/apps/web/tests/partners/partner-search-find-candidates.test.ts@@ -26,34 +26,14 @@ describe("findPartnerSearchCandidates", () => {vi.spyOn(console, "error").mockImplementation(() => {});});- it("sends a complete email to the database path, without calling the provider", async () => {- // The database matches the address exactly, so an address that matches no- // partner returns no rows instead of fuzzy matches from the provider.+ it("returns the provider's candidates", async () => {const provider = createProvider();await expect(- findPartnerSearchCandidates(provider, query("[email protected]")),- ).resolves.toBeNull();-- expect(provider.searchCandidates).not.toHaveBeenCalled();+ findPartnerSearchCandidates(provider, query("steven")),+ ).resolves.toEqual({ hits: [{ id: "pge_1" }] });});- it.each([- ["half-typed", "steven@"],- ["domain only", "@dub.co"],- ["no dot in the domain", "steven@dub"],- ["a name", "steven tey"],- ])(- "does not treat %s as an address, so the provider is queried",- async (_label, search) => {- const provider = createProvider();-- await findPartnerSearchCandidates(provider, query(search));-- expect(provider.searchCandidates).toHaveBeenCalledOnce();- },- );-it("degrades to the database search path when the provider throws", async () => {const provider = createProvider(vi.fn().mockRejectedValue(new Error("down")),diff --git a/apps/web/tests/partners/partner-search-query.test.ts b/apps/web/tests/partners/partner-search-query.test.tsindex 497b4cf8c5..2f9310fa0a 100644--- a/apps/web/tests/partners/partner-search-query.test.ts+++ b/apps/web/tests/partners/partner-search-query.test.ts@@ -56,11 +56,25 @@ describe("buildPartnerSearchCandidateQuery", () => {["missing search", { ...defaultInput, search: undefined }],["empty search", { ...defaultInput, search: " " }],["exact email", { ...defaultInput, email: "[email protected]" }],+ [+ "complete email in the search",+ { ...defaultInput, search: "[email protected]" },+ ],["tenant ID", { ...defaultInput, tenantId: "tenant_test" }],])("keeps %s on the database path", (_name, input) => {expect(buildPartnerSearchCandidateQuery(input)).toBeNull();});+ it.each([+ ["half-typed", "steven@"],+ ["domain only", "@dub.co"],+ ["no dot in the domain", "steven@dub"],+ ])("sends a %s email to the provider", (_label, search) => {+ expect(+ buildPartnerSearchCandidateQuery({ programId: "prog_1", search }),+ ).toMatchObject({ query: search });+ });+describe("pasted partner IDs", () => {it.each([["24-char suffix, the production minimum", "pn_dlszeepb38rvcnrfbd0srkzb"],