Skip to content

Commit eedb4a4

Browse files
committed
feat: add collection filtering
1 parent a2c9262 commit eedb4a4

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

src/app/api/products/route.ts

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,114 @@ import { NextResponse, NextRequest } from "next/server"
22
import getPrices from "@lib/util/get-product-prices"
33

44
import { initialize as initializeProductModule } from "@medusajs/product"
5-
import { FilterableProductProps } from "@medusajs/types/dist/product/common"
5+
import {
6+
FilterableProductProps,
7+
ProductDTO,
8+
} from "@medusajs/types/dist/product/common"
9+
import { notFound } from "next/navigation"
610

711
export async function GET(request: NextRequest) {
12+
const queryParams = Object.fromEntries(request.nextUrl.searchParams)
13+
14+
const { collection_id } = queryParams
15+
16+
if (collection_id) {
17+
const response = await getProductsByCollectionId(queryParams)
18+
19+
if (!response) {
20+
return notFound()
21+
}
22+
23+
return NextResponse.json(response)
24+
}
25+
26+
const response = await getProducts(queryParams)
27+
28+
if (!response) {
29+
return notFound()
30+
}
31+
32+
return NextResponse.json(response)
33+
}
34+
35+
async function getProductsByCollectionId(queryParams: Record<string, any>) {
836
const productService = await initializeProductModule()
937

10-
const { id, limit, offset, cart_id } = Object.fromEntries(
11-
request.nextUrl.searchParams
38+
const { limit, offset, cart_id, collection_id } = queryParams
39+
40+
const collectionIds = collection_id.split(",")
41+
42+
const data = await productService.listCollections(
43+
{ id: collectionIds },
44+
{
45+
relations: [
46+
"products",
47+
"products.variants",
48+
"products.tags",
49+
"products.status",
50+
"products.collection",
51+
],
52+
}
53+
)
54+
55+
const products = data.map((c) => c.products).flat() as ProductDTO[]
56+
57+
if (!products) return []
58+
59+
const publishedProducts = filterPublishedProducts(products)
60+
61+
const count = publishedProducts.length
62+
63+
const page = publishedProducts.slice(
64+
parseInt(offset),
65+
parseInt(offset) + parseInt(limit)
1266
)
1367

68+
const productsWithPrices = await getPrices(page, cart_id)
69+
70+
const nextPage = parseInt(offset) + parseInt(limit)
71+
72+
return {
73+
products: productsWithPrices,
74+
count,
75+
nextPage: count > nextPage ? nextPage : null,
76+
}
77+
}
78+
79+
async function getProducts(params: Record<string, any>) {
80+
const productService = await initializeProductModule()
81+
const { id, limit, offset, cart_id } = params
82+
1483
const filters = {} as FilterableProductProps
1584

1685
if (id) {
1786
filters.id = id.split(",")
1887
}
1988

2089
const [data, count] = await productService.listAndCount(filters, {
21-
relations: ["variants", "variants", "tags", "status"],
90+
relations: ["variants", "variants", "tags", "status", "collection"],
2291
take: parseInt(limit) || 100,
2392
skip: parseInt(offset) || 0,
2493
withDeleted: false,
2594
})
2695

27-
const publishedProducts = data.filter(
28-
(product) => product.status === "published"
29-
)
96+
if (!data) {
97+
return []
98+
}
99+
100+
const publishedProducts = filterPublishedProducts(data)
30101

31102
const productsWithPrices = await getPrices(publishedProducts, cart_id)
32103

33104
const nextPage = parseInt(offset) + parseInt(limit)
34105

35-
return NextResponse.json({
106+
return {
36107
products: productsWithPrices,
37108
count,
38109
nextPage: count > nextPage ? nextPage : null,
39-
})
110+
}
111+
}
112+
113+
function filterPublishedProducts(products: ProductDTO[]) {
114+
return products.filter((product) => product.status === "published")
40115
}

src/lib/util/get-product-prices.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ export default async function getVariants(data: ProductDTO[], cartId?: string) {
1414
// Map of variant id to variant object
1515
const variantsById = new Map<string, Record<string, any>>()
1616

17+
const productIds = data.map((p) => p.id)
18+
1719
const query = {
20+
id: productIds,
1821
expand: "variants,variants.prices,variants.options",
19-
} as Record<string, string>
22+
} as Record<string, any>
2023

2124
if (cartId) {
2225
query.cart_id = cartId

src/modules/products/components/option-select/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ const OptionSelect: React.FC<OptionSelectProps> = ({
1616
updateOption,
1717
title,
1818
}) => {
19-
console.log({ option })
20-
2119
const filteredOptions = option.values.map((v) => v.value).filter(onlyUnique)
2220

23-
console.log({ filteredOptions })
24-
2521
return (
2622
<div className="flex flex-col gap-y-3">
2723
<span className="text-base-semi">Select {title}</span>

0 commit comments

Comments
 (0)