mirror of
https://github.com/Nezumi-2711/multitenant-ecommerce.git
synced 2026-09-22 20:01:36 +00:00
feat: implement filtering for the product
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user