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 buildCustomerSearchWhere

Pedro Ladeira5ece6d0b23bb
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
index 958737a035..2af5aa7e81 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
@@ -626,7 +626,6 @@ function CreateCommissionSheetContent({
setSelectedCustomerId={(id) => {
setValue("customerId", id, { shouldDirty: true });
}}
- searchByCustomerId
/>
</div>
</div>
diff --git a/apps/web/lib/customers/api/customer-count-where.ts b/apps/web/lib/customers/api/customer-count-where.ts
index 0a6ebad698..a8c175a88a 100644
--- a/apps/web/lib/customers/api/customer-count-where.ts
+++ b/apps/web/lib/customers/api/customer-count-where.ts
@@ -7,6 +7,34 @@ type CustomerCountFilters = z.infer<typeof getCustomersCountQuerySchema> & {
workspaceId: string;
};
+export function buildCustomerSearchWhere({
+ email,
+ externalId,
+ search: rawSearch,
+}: {
+ email?: string | null;
+ externalId?: string | null;
+ search?: string | null;
+}): Prisma.CustomerWhereInput {
+ if (email) return { email };
+
+ if (externalId) return { externalId };
+
+ const search = rawSearch?.trim();
+ if (!search) return {};
+
+ const isExactCustomerIdQuery = /^cus_[a-z0-9]{24,}$/i.test(search);
+ if (isExactCustomerIdQuery) return { id: search };
+
+ if (search.includes("@")) return { email: search };
+
+ const q = sanitizeFullTextSearch(search);
+ return {
+ email: { search: q },
+ name: { search: q },
+ };
+}
+
export function buildCustomerCountWhere(filters: CustomerCountFilters) {
const {
programId,
@@ -28,18 +56,7 @@ export function buildCustomerCountWhere(filters: CustomerCountFilters) {
partnerId,
}),
projectId: workspaceId,
- ...(email
- ? { email }
- : externalId
- ? { externalId }
- : search
- ? search.includes("@")
- ? { email: search }
- : {
- email: { search: sanitizeFullTextSearch(search) },
- name: { search: sanitizeFullTextSearch(search) },
- }
- : {}),
+ ...buildCustomerSearchWhere({ email, externalId, search }),
// only filter by country if not grouping by country
...(country &&
groupBy !== "country" && {
diff --git a/apps/web/lib/customers/api/get-customers.ts b/apps/web/lib/customers/api/get-customers.ts
index 16905ee0ad..d42a2c50e0 100644
--- a/apps/web/lib/customers/api/get-customers.ts
+++ b/apps/web/lib/customers/api/get-customers.ts
@@ -1,8 +1,9 @@
-import { prisma, sanitizeFullTextSearch } from "@/lib/prisma";
+import { prisma } from "@/lib/prisma";
import { getCustomersQuerySchemaExtended } from "@/lib/zod/schemas/customers";
import * as z from "zod/v4";
import { DubApiError } from "../../api/errors";
import { buildPaginationQuery } from "../../api/pagination";
+import { buildCustomerSearchWhere } from "./customer-count-where";
type GetCustomersInput = z.infer<typeof getCustomersQuerySchemaExtended> & {
workspaceId: string;
@@ -62,18 +63,7 @@ export async function getCustomers(filters: GetCustomersInput) {
partnerId,
}),
projectId: workspaceId,
- ...(email
- ? { email }
- : externalId
- ? { externalId }
- : search
- ? search.includes("@")
- ? { email: search }
- : {
- email: { search: sanitizeFullTextSearch(search) },
- name: { search: sanitizeFullTextSearch(search) },
- }
- : {}),
+ ...buildCustomerSearchWhere({ email, externalId, search }),
...(country && {
country,
}),
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index e26b20c85f..ada63c0f81 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -29,7 +29,7 @@ export const getCustomersQuerySchema = z
.string()
.optional()
.describe(
- "A search query to filter customers by email, externalId, or name. If `email` or `externalId` is provided, this will be ignored.",
+ "A search query to filter customers by email, name, or customer ID (`cus_...`). If `email` or `externalId` is provided, this will be ignored.",
),
country: z
.string()
diff --git a/apps/web/ui/customers/customer-selector.tsx b/apps/web/ui/customers/customer-selector.tsx
index 55557974e3..6be8f28be5 100644
--- a/apps/web/ui/customers/customer-selector.tsx
+++ b/apps/web/ui/customers/customer-selector.tsx
@@ -9,19 +9,11 @@ import {
} from "../modals/add-customer-modal";
import { CustomerAvatar } from "./customer-avatar";
-function parseCustomerIdSearch(search: string) {
- const match = search.trim().match(/^cus_([0-9A-HJKMNP-TV-Z]{25,26})$/i);
- if (!match) return null;
-
- return `cus_${match[1].toUpperCase()}`;
-}
-
type CustomerSelectorProps = {
selectedCustomerId: string | null;
setSelectedCustomerId: (customerId: string) => void;
disabled?: boolean;
variant?: "default" | "header";
- searchByCustomerId?: boolean;
} & Partial<ComboboxProps<false, any>>;
export function CustomerSelector({
@@ -29,21 +21,14 @@ export function CustomerSelector({
setSelectedCustomerId,
disabled,
variant = "default",
- searchByCustomerId = false,
...rest
}: CustomerSelectorProps) {
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebounce(search, 500);
const [openPopover, setOpenPopover] = useState(false);
- const customerIdSearch = searchByCustomerId
- ? parseCustomerIdSearch(debouncedSearch)
- : null;
-
const { customers, loading } = useCustomers({
- query: customerIdSearch
- ? { customerIds: [customerIdSearch] }
- : { search: debouncedSearch },
+ query: { search: debouncedSearch },
});
const { customers: selectedCustomers, loading: selectedCustomersLoading } =
@@ -118,29 +103,24 @@ export function CustomerSelector({
}
caret={true}
placeholder={variant === "header" ? "" : "Select customer"}
- searchPlaceholder={
- variant === "header"
- ? "Search customers..."
- : "Search or create customer..."
- }
+ searchPlaceholder="Search by name, email, or ID"
onSearchChange={setSearch}
- {...(variant !== "header" &&
- !(searchByCustomerId && parseCustomerIdSearch(search)) && {
- createLabel: (search: string) =>
- `Create ${search ? `"${search}"` : "new customer"}`,
- onCreate: async (search: string | undefined) => {
- const trimmed = search?.trim() ?? "";
- setInitialData(
- trimmed
- ? trimmed.includes("@")
- ? { email: trimmed }
- : { name: trimmed }
- : undefined,
- );
- setShowAddCustomerModal(true);
- return true;
- },
- })}
+ {...(variant !== "header" && {
+ createLabel: (search: string) =>
+ `Create ${search ? `"${search}"` : "new customer"}`,
+ onCreate: async (search: string | undefined) => {
+ const trimmed = search?.trim() ?? "";
+ setInitialData(
+ trimmed
+ ? trimmed.includes("@")
+ ? { email: trimmed }
+ : { name: trimmed }
+ : undefined,
+ );
+ setShowAddCustomerModal(true);
+ return true;
+ },
+ })}
shouldFilter={false}
matchTriggerWidth
open={openPopover}
diff --git a/apps/web/ui/customers/customers-table/customers-table.tsx b/apps/web/ui/customers/customers-table/customers-table.tsx
index 176a8984d0..8252f6a62f 100644
--- a/apps/web/ui/customers/customers-table/customers-table.tsx
+++ b/apps/web/ui/customers/customers-table/customers-table.tsx
@@ -511,7 +511,7 @@ function CustomersFilters({
onSelectedFilterChange={setSelectedFilter}
/>
<SearchBoxPersisted
- placeholder="Search by email or name"
+ placeholder="Search by email, name, or ID"
inputClassName="md:w-[16rem]"
/>
</div>