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.

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.ts
index 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 customers
export 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.ts
index 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.ts
index 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.ts
index 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 () => {