The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Match a complete email exactly in partner search
Rafi87fa100b7054
diff --git a/apps/web/lib/api/partners/search/find-candidates.ts b/apps/web/lib/api/partners/search/find-candidates.tsindex 0671a7619b..71f1d7b86b 100644--- a/apps/web/lib/api/partners/search/find-candidates.ts+++ b/apps/web/lib/api/partners/search/find-candidates.ts@@ -1,5 +1,4 @@import { isExactEmailQuery } from "@/lib/api/partners/program-enrollment-query";-import { prisma } from "@/lib/prisma";import type {PartnerSearchCandidateQuery,PartnerSearchProvider,@@ -15,16 +14,9 @@ interface FindPartnerSearchCandidatesOptions {}/**- * Resolves relevance candidates, falling back to the database search path when- * the provider fails. Returning null here is what the callers already do when- * no provider is configured, so a provider outage costs relevance ranking and- * the wider field coverage rather than the entire partners list.- *- * A complete email address takes the same exit, but only once the database- * confirms it: `email` is unique and indexed, so the lookup is a point read- * against a network round trip. A miss still goes to the provider, because an- * address matching nothing exactly is usually one the caller is still typing,- * `[email protected]` on the way to `[email protected]`, and n-grams still match it.+ * 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.*/export async function findPartnerSearchCandidates(searchProvider: PartnerSearchProvider,@@ -32,14 +24,7 @@ export async function findPartnerSearchCandidates({ throwOnError = false }: FindPartnerSearchCandidatesOptions = {},): Promise<PartnerSearchResult | null> {if (isExactEmailQuery(query.query)) {- const partner = await prisma.partner.findUnique({- where: { email: query.query },- select: { id: true },- });-- if (partner) {- return null;- }+ return null;}try {diff --git a/apps/web/tests/partners/partner-search-find-candidates.test.ts b/apps/web/tests/partners/partner-search-find-candidates.test.tsindex e06bc59b64..3a8095ebb0 100644--- a/apps/web/tests/partners/partner-search-find-candidates.test.ts+++ b/apps/web/tests/partners/partner-search-find-candidates.test.ts@@ -2,12 +2,6 @@ import type { PartnerSearchProvider } from "@/lib/api/partners/search";import { findPartnerSearchCandidates } from "@/lib/api/partners/search";import { beforeEach, describe, expect, it, vi } from "vitest";-const mocks = vi.hoisted(() => ({ findUnique: vi.fn() }));--vi.mock("@/lib/prisma", () => ({- prisma: { partner: { findUnique: mocks.findUnique } },-}));-function createProvider(searchCandidates = vi.fn().mockResolvedValue({ hits: [{ id: "pge_1" }] }),) {@@ -29,51 +23,33 @@ const query = (search: string) => ({describe("findPartnerSearchCandidates", () => {beforeEach(() => {- mocks.findUnique.mockReset();vi.spyOn(console, "error").mockImplementation(() => {});});- it("answers a known email from the database, without calling the provider", async () => {- mocks.findUnique.mockResolvedValue({ id: "pn_1" });+ 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.const provider = createProvider();await expect(findPartnerSearchCandidates(provider, query("[email protected]")),).resolves.toBeNull();- expect(mocks.findUnique).toHaveBeenCalledWith({- where: { email: "[email protected]" },- select: { id: true },- });expect(provider.searchCandidates).not.toHaveBeenCalled();});- it("falls back to the provider when no partner has that address", async () => {- // `[email protected]` on the way to `[email protected]` is a complete address that- // matches nothing, and n-grams still find the longer one.- mocks.findUnique.mockResolvedValue(null);- const provider = createProvider();-- await expect(- findPartnerSearchCandidates(provider, query("[email protected]")),- ).resolves.toEqual({ hits: [{ id: "pge_1" }] });-- expect(provider.searchCandidates).toHaveBeenCalledOnce();- });-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 database is not queried",+ "does not treat %s as an address, so the provider is queried",async (_label, search) => {const provider = createProvider();await findPartnerSearchCandidates(provider, query(search));- expect(mocks.findUnique).not.toHaveBeenCalled();expect(provider.searchCandidates).toHaveBeenCalledOnce();},);