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.

Filtering

Tim Wilson025c63dcf8ac
diff --git a/apps/web/app/api/customers/count/route.ts b/apps/web/app/api/customers/count/route.ts
index 207a7e57d8..76653fbf7c 100644
--- a/apps/web/app/api/customers/count/route.ts
+++ b/apps/web/app/api/customers/count/route.ts
@@ -1,30 +1,54 @@
import { withWorkspace } from "@/lib/auth";
import { getCustomersCountQuerySchema } from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
+import { Prisma } from "@dub/prisma/client";
import { NextResponse } from "next/server";
// GET /api/customers/count
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
- const { email, externalId, search } =
+ const { email, externalId, search, country, groupBy } =
getCustomersCountQuerySchema.parse(searchParams);
+ const commonWhere: Prisma.CustomerWhereInput = {
+ projectId: workspace.id,
+ ...(email
+ ? { email }
+ : externalId
+ ? { externalId }
+ : {
+ ...(search && {
+ OR: [
+ { email: { startsWith: search } },
+ { externalId: { startsWith: search } },
+ { name: { startsWith: search } },
+ ],
+ }),
+ ...(country && {
+ country,
+ }),
+ }),
+ };
+
+ // Get customer count by country
+ if (groupBy === "country") {
+ const customers = await prisma.customer.groupBy({
+ by: ["country"],
+ where: {
+ ...commonWhere,
+ },
+ _count: true,
+ orderBy: {
+ _count: {
+ country: "desc",
+ },
+ },
+ });
+
+ return NextResponse.json(customers);
+ }
+
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 } },
- ],
- }
- : {}),
- },
+ where: commonWhere,
});
return NextResponse.json(count);
diff --git a/apps/web/lib/swr/use-customers-count.ts b/apps/web/lib/swr/use-customers-count.ts
index f5eeb0113d..efcceec787 100644
--- a/apps/web/lib/swr/use-customers-count.ts
+++ b/apps/web/lib/swr/use-customers-count.ts
@@ -1,12 +1,19 @@
import { fetcher } from "@dub/utils";
import useSWR from "swr";
+import { z } from "zod";
+import { getCustomersCountQuerySchema } from "../zod/schemas/customers";
import useWorkspace from "./use-workspace";
-export default function useCustomersCount() {
+export default function useCustomersCount<T = number>({
+ query,
+}: {
+ query?: z.infer<typeof getCustomersCountQuerySchema>;
+} = {}) {
const { id: workspaceId } = useWorkspace();
- const { data, error } = useSWR<number>(
- workspaceId && `/api/customers/count?workspaceId=${workspaceId}`,
+ const { data, error } = useSWR<T>(
+ workspaceId &&
+ `/api/customers/count?${new URLSearchParams({ workspaceId, ...query })}`,
fetcher,
);
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 0c9d260b05..800bddc4a6 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -63,11 +63,15 @@ export const getCustomersQuerySchemaExtended = getCustomersQuerySchema.merge(
}),
);
-export const getCustomersCountQuerySchema = getCustomersQuerySchema.omit({
- includeExpandedFields: true,
- page: true,
- pageSize: true,
-});
+export const getCustomersCountQuerySchema = getCustomersQuerySchema
+ .omit({
+ includeExpandedFields: true,
+ page: true,
+ pageSize: true,
+ sortBy: true,
+ sortOrder: true,
+ })
+ .extend({ groupBy: z.enum(["country"]).optional() });
export const createCustomerBodySchema = z.object({
email: z
diff --git a/apps/web/ui/customers/customer-table/use-customer-filters.tsx b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
index d7c0f982d0..03d9262ca2 100644
--- a/apps/web/ui/customers/customer-table/use-customer-filters.tsx
+++ b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
@@ -1,3 +1,4 @@
+import useCustomersCount from "@/lib/swr/use-customers-count";
import useWorkspace from "@/lib/swr/use-workspace";
import { useRouterStuff } from "@dub/ui";
import { FlagWavy } from "@dub/ui/icons";
@@ -8,17 +9,17 @@ export function useCustomerFilters(extraSearchParams: Record<string, string>) {
const { searchParamsObj, queryParams } = useRouterStuff();
const { id: workspaceId } = useWorkspace();
- // const { data: countriesCount } = useCustomersCount<
- // | {
- // country: string;
- // _count: number;
- // }[]
- // | undefined
- // >({
- // groupBy: "country",
- // });
-
- const countriesCount = [];
+ const { data: countriesCount } = useCustomersCount<
+ | {
+ country: string;
+ _count: number;
+ }[]
+ | undefined
+ >({
+ query: {
+ groupBy: "country",
+ },
+ });
const filters = useMemo(
() => [