Fix company search filters and build regressions
This commit is contained in:
parent
a8abb68e36
commit
11efad0312
3 changed files with 71 additions and 32 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { ConvexHttpClient } from "convex/browser"
|
||||
|
||||
import { Prisma } from "@prisma/client"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { env } from "@/lib/env"
|
||||
|
|
@ -59,18 +60,31 @@ export async function GET(request: Request) {
|
|||
const search = url.searchParams.get("search")?.trim() ?? ""
|
||||
|
||||
try {
|
||||
const slugSearch = search ? normalizeSlug(search) ?? slugify(search) : null
|
||||
const orFilters: Prisma.CompanyWhereInput[] = []
|
||||
if (search) {
|
||||
orFilters.push({
|
||||
name: {
|
||||
contains: search,
|
||||
mode: Prisma.QueryMode.insensitive,
|
||||
} as unknown as Prisma.StringFilter<"Company">,
|
||||
})
|
||||
if (slugSearch) {
|
||||
orFilters.push({
|
||||
slug: {
|
||||
contains: slugSearch,
|
||||
mode: Prisma.QueryMode.insensitive,
|
||||
} as unknown as Prisma.StringFilter<"Company">,
|
||||
})
|
||||
}
|
||||
}
|
||||
const where: Prisma.CompanyWhereInput = {
|
||||
tenantId,
|
||||
...(orFilters.length > 0 ? { OR: orFilters } : {}),
|
||||
}
|
||||
|
||||
const companies = await prisma.company.findMany({
|
||||
where: {
|
||||
tenantId,
|
||||
...(search
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: search, mode: "insensitive" } },
|
||||
{ slug: { contains: normalizeSlug(search) ?? slugify(search), mode: "insensitive" } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
where,
|
||||
orderBy: { name: "asc" },
|
||||
take: 20,
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue