@@ -2,39 +2,114 @@ import { NextResponse, NextRequest } from "next/server"
2
2
import getPrices from "@lib/util/get-product-prices"
3
3
4
4
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"
6
10
7
11
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 > ) {
8
36
const productService = await initializeProductModule ( )
9
37
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 )
12
66
)
13
67
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
+
14
83
const filters = { } as FilterableProductProps
15
84
16
85
if ( id ) {
17
86
filters . id = id . split ( "," )
18
87
}
19
88
20
89
const [ data , count ] = await productService . listAndCount ( filters , {
21
- relations : [ "variants" , "variants" , "tags" , "status" ] ,
90
+ relations : [ "variants" , "variants" , "tags" , "status" , "collection" ] ,
22
91
take : parseInt ( limit ) || 100 ,
23
92
skip : parseInt ( offset ) || 0 ,
24
93
withDeleted : false ,
25
94
} )
26
95
27
- const publishedProducts = data . filter (
28
- ( product ) => product . status === "published"
29
- )
96
+ if ( ! data ) {
97
+ return [ ]
98
+ }
99
+
100
+ const publishedProducts = filterPublishedProducts ( data )
30
101
31
102
const productsWithPrices = await getPrices ( publishedProducts , cart_id )
32
103
33
104
const nextPage = parseInt ( offset ) + parseInt ( limit )
34
105
35
- return NextResponse . json ( {
106
+ return {
36
107
products : productsWithPrices ,
37
108
count,
38
109
nextPage : count > nextPage ? nextPage : null ,
39
- } )
110
+ }
111
+ }
112
+
113
+ function filterPublishedProducts ( products : ProductDTO [ ] ) {
114
+ return products . filter ( ( product ) => product . status === "published" )
40
115
}
0 commit comments