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.

getCustomersCountQuerySchema

Steven Tey2563946db017
diff --git a/apps/web/app/api/customers/count/route.ts b/apps/web/app/api/customers/count/route.ts
index 8fd9ad75b5..207a7e57d8 100644
--- a/apps/web/app/api/customers/count/route.ts
+++ b/apps/web/app/api/customers/count/route.ts
@@ -1,12 +1,29 @@
import { withWorkspace } from "@/lib/auth";
+import { getCustomersCountQuerySchema } from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
// GET /api/customers/count
-export const GET = withWorkspace(async ({ workspace, params }) => {
+export const GET = withWorkspace(async ({ workspace, searchParams }) => {
+ const { email, externalId, search } =
+ getCustomersCountQuerySchema.parse(searchParams);
+
const count = await prisma.customer.count({
where: {
projectId: workspace.id,
+ ...(email
+ ? { email }
+ : externalId
+ ? { externalId }
+ : search
+ ? {
+ OR: [
+ { email: { startsWith: search } },
+ { externalId: { startsWith: search } },
+ { name: { startsWith: search } },
+ ],
+ }
+ : {}),
},
});
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 66fd65a9b4..884077ab63 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -44,6 +44,12 @@ export const getCustomersQuerySchemaExtended = getCustomersQuerySchema.merge(
}),
);
+export const getCustomersCountQuerySchema = getCustomersQuerySchema.omit({
+ includeExpandedFields: true,
+ page: true,
+ pageSize: true,
+});
+
export const createCustomerBodySchema = z.object({
email: z
.string()