The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Treat a missing count aggregate as unanswered, not zero
Rafi5ee9755e5118
diff --git a/apps/web/lib/api/partners/get-partners-count.ts b/apps/web/lib/api/partners/get-partners-count.tsindex da08c28225..36dad50ff8 100644--- a/apps/web/lib/api/partners/get-partners-count.ts+++ b/apps/web/lib/api/partners/get-partners-count.ts@@ -261,11 +261,6 @@ export async function getPartnersCount<T>(})) as T;}- // Counting the candidate IDs reports a floor, because the provider truncated- // them to the candidate ceiling — "999 partners" when there are twelve- // thousand. Ask the provider for the real total instead, but only when it can- // see every filter that applies: `partnerIds`, `tenantId`, and the metric- // ranges are database-only, so leaving them out would over-count.// Every filter the provider does not carry. `email` is absent because it stops// a candidate query being built at all. Adding a filter to// buildProgramEnrollmentWhereForList without adding it here or to the@@ -296,7 +291,11 @@ export async function getPartnersCount<T>(}}- // Get absolute count of partners+ // Counting the candidate IDs reports a floor rather than a total, because the+ // provider already truncated them to the candidate ceiling. Every path that+ // lands here accepts that: a database-only filter, a declined short prefix, or+ // a failed aggregation. It stays consistent with the list, which is capped at+ // the same ceiling, but a broad query will under-report.const count = await prisma.programEnrollment.count({where: {...buildProgramEnrollmentWhereForList(enrollmentBase),diff --git a/apps/web/lib/api/partners/search/providers/turbopuffer.ts b/apps/web/lib/api/partners/search/providers/turbopuffer.tsindex d45385270c..09dd2e41a6 100644--- a/apps/web/lib/api/partners/search/providers/turbopuffer.ts+++ b/apps/web/lib/api/partners/search/providers/turbopuffer.ts@@ -397,7 +397,10 @@ export function createTurbopufferPartnerSearchProvider({const total = response.aggregations?.total;- return typeof total === "number" ? total : 0;+ // A missing aggregate means the response was not what we asked for.+ // Reporting it as zero would render an unanswered count as an exact+ // empty result.+ return typeof total === "number" ? total : null;},async upsert(documents) {diff --git a/apps/web/tests/partners/get-partners-count-search.test.ts b/apps/web/tests/partners/get-partners-count-search.test.tsindex 74f190ea27..abb0015576 100644--- a/apps/web/tests/partners/get-partners-count-search.test.ts+++ b/apps/web/tests/partners/get-partners-count-search.test.ts@@ -82,14 +82,14 @@ describe("getPartnersCount search", () => {// The aggregation only knows the filters it was given, so counting with// one of these applied would over-count.const searchProvider = createSearchProvider(12_000);- mocks.count.mockResolvedValue(3);+ mocks.count.mockResolvedValue(2);const count = await getPartnersCount<number>({ programId: "prog_test", search: "examp", ...extra },{ searchProvider },);- expect(count).toBe(3);+ expect(count).toBe(2);expect(searchProvider.countCandidates).not.toHaveBeenCalled();expect(mocks.count).toHaveBeenCalled();},@@ -115,7 +115,7 @@ describe("getPartnersCount search", () => {(searchProvider.countCandidates as ReturnType<typeof vi.fn>).mockRejectedValue(new Error("aggregation down"));- mocks.count.mockResolvedValue(7);+ mocks.count.mockResolvedValue(2);vi.spyOn(console, "error").mockImplementation(() => {});const count = await getPartnersCount<number>(@@ -123,7 +123,7 @@ describe("getPartnersCount search", () => {{ searchProvider },);- expect(count).toBe(7);+ expect(count).toBe(2);});it("groups database-filtered relevance candidates", async () => {diff --git a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.tsindex c92ce5e499..52e8b5dba2 100644--- a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts+++ b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts@@ -390,6 +390,29 @@ describe("Turbopuffer partner search provider", () => {).rejects.toThrow("Partner search candidate limit");});+ it("counts a three-character prefix, the shortest it will answer", async () => {+ const total = await createProvider().countCandidates({+ programId: "prog_test",+ query: "ale",+ limit: 10,+ });++ expect(total).toBe(12_000);+ });++ it("returns null when the response carries no aggregate", async () => {+ // Zero would render an unanswered count as an exact empty result.+ mocks.query.mockResolvedValue({});++ await expect(+ createProvider().countCandidates({+ programId: "prog_test",+ query: "creator",+ limit: 10,+ }),+ ).resolves.toBeNull();+ });+it("counts matches without the candidate ceiling", async () => {const total = await createProvider().countCandidates({programId: "prog_test",