Vivek Poddar
07/22/2022, 7:42 AMexport async function getProducts({
filters,
page,
limit,
orderBy = { Name: "asc" },
searchTerm = "",
}: {
filters: string[];
page: number;
limit: number;
orderBy?: Record<string, string>;
searchTerm: string;
}): Promise<
Prisma.ProductGetPayload<{
include: { Category: true };
}>[]
> {
const { skip, take } = getItemsPaginated({ page, limit });
const prismaDBFilters = filters.map((filter) => ({
Name: { equals: filter },
}));
if (prismaDBFilters.length) {
return await prisma.product.findMany({
orderBy,
skip,
take,
where: {
Name: {
contains: searchTerm,
},
Category: {
OR: prismaDBFilters,
},
},
include: {
Category: true,
},
});
} else {
return await prisma.product.findMany({
orderBy,
skip,
take,
where: {
Name: {
contains: searchTerm,
},
},
include: {
Category: true,
},
});
}
}