feat: implement filtering for the product

This commit is contained in:
2025-07-21 21:32:40 +07:00
parent c9248b0ba4
commit 7732e680d4
11 changed files with 233 additions and 30 deletions
+23 -4
View File
@@ -1,13 +1,33 @@
import type { Where } from "payload";
import { baseProcedure, createTRPCRouter } from "@/trpc/init";
import { z } from "zod";
import { baseProcedure, createTRPCRouter } from "@/trpc/init";
import { Category } from "@/payload-types";
export const productsRouter = createTRPCRouter({
getMany: baseProcedure.input(z.object({
category: z.string().nullable().optional(),
minPrice: z.string().nullable().optional(),
maxPrice: z.string().nullable().optional(),
})).query(async ({ ctx, input }) => {
const where: Where = {}
const where: Where = {
price: {},
}
if (input.minPrice && input.maxPrice) {
where.price = {
greater_than_equal: input.minPrice,
less_than_equal: input.maxPrice,
}
} else if (input.minPrice) {
where.price = {
greater_than_equal: input.minPrice,
}
} else if (input.maxPrice) {
where.price = {
less_than_equal: input.maxPrice,
}
}
if (input.category) {
const categoriesData = await ctx.db.find({
@@ -38,13 +58,12 @@ export const productsRouter = createTRPCRouter({
subCategoriesSlugs.push(
...parentCategory.subcategories.map((subcategory) => subcategory.slug)
);
}
if (parentCategory) {
where['category.slug'] = {
in: [parentCategory.slug, ...subCategoriesSlugs],
}
}
}
const data = await ctx.db.find({