The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add support for filtering by email / externalId in dub.customers.list
Steven Tey4145e55e7fe4
diff --git a/apps/web/app/api/customers/route.ts b/apps/web/app/api/customers/route.tsindex 62cfbefe7a..3e2d61d770 100644--- a/apps/web/app/api/customers/route.ts+++ b/apps/web/app/api/customers/route.ts@@ -6,16 +6,21 @@ import { generateRandomName } from "@/lib/names";import {createCustomerBodySchema,CustomerSchema,+ getCustomersQuerySchema,} from "@/lib/zod/schemas/customers";import { prisma } from "@dub/prisma";import { NextResponse } from "next/server";// GET /api/customers – Get all customersexport const GET = withWorkspace(- async ({ workspace }) => {+ async ({ workspace, searchParams }) => {+ const { email, externalId } = getCustomersQuerySchema.parse(searchParams);+const customers = await prisma.customer.findMany({where: {projectId: workspace.id,+ ...(email ? { email } : {}),+ ...(externalId ? { externalId } : {}),},take: 100,orderBy: {diff --git a/apps/web/lib/openapi/customers/get-customers.ts b/apps/web/lib/openapi/customers/get-customers.tsindex 59e7a93751..11550f25e2 100644--- a/apps/web/lib/openapi/customers/get-customers.ts+++ b/apps/web/lib/openapi/customers/get-customers.ts@@ -1,13 +1,19 @@import { openApiErrorResponses } from "@/lib/openapi/responses";import z from "@/lib/zod";import { ZodOpenApiOperationObject } from "zod-openapi";-import { CustomerSchema } from "../../zod/schemas/customers";+import {+ CustomerSchema,+ getCustomersQuerySchema,+} from "../../zod/schemas/customers";export const getCustomers: ZodOpenApiOperationObject = {operationId: "getCustomers","x-speakeasy-name-override": "list",summary: "Retrieve a list of customers",description: "Retrieve a list of customers for the authenticated workspace.",+ requestParams: {+ query: getCustomersQuerySchema,+ },responses: {"200": {description: "The list of customers.",diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.tsindex 6f45b83a96..a04aa7b027 100644--- a/apps/web/lib/zod/schemas/customers.ts+++ b/apps/web/lib/zod/schemas/customers.ts@@ -3,6 +3,21 @@ import { DiscountSchema } from "./discount";import { LinkSchema } from "./links";import { getPaginationQuerySchema } from "./misc";+export const getCustomersQuerySchema = z.object({+ email: z+ .string()+ .optional()+ .describe(+ "A case-sensitive filter on the list based on the customer's `email` field. The value must be a string.",+ ),+ externalId: z+ .string()+ .optional()+ .describe(+ "A case-sensitive filter on the list based on the customer's `externalId` field. The value must be a string.",+ ),+});+export const createCustomerBodySchema = z.object({email: z.string()diff --git a/apps/web/tests/customers/index.test.ts b/apps/web/tests/customers/index.test.tsindex 5b8464b261..ad82791291 100644--- a/apps/web/tests/customers/index.test.ts+++ b/apps/web/tests/customers/index.test.ts@@ -58,11 +58,12 @@ describe.sequential("/customers/**", async () => {test("GET /customers", async () => {const { status, data: customers } = await http.get<Customer[]>({- path: "/customers",+ path: `/customers?email=${customerRecord.email}`,});expect(status).toEqual(200);expect(customers.length).toBeGreaterThanOrEqual(1);+ expect(customers[0]).toStrictEqual(expectedCustomer);});test("PATCH /customers/{id}", async () => {