@@ -36,9 +36,36 @@ export type ProductSearchResult = {
36
36
page : number
37
37
}
38
38
39
+ export type CartModel = {
40
+ id : string
41
+ userId : string
42
+ cartItemTotal : number
43
+ cartItemPromoSavings : number
44
+ shippingTotal : number
45
+ shippingPromoSavings : number
46
+ cartTotal : number
47
+ isCheckOut : boolean
48
+ items : CartItem [ ]
49
+ }
50
+
51
+ export type CartItem = {
52
+ quantity : number
53
+ price : number
54
+ productId : string
55
+ productName : string
56
+ productPrice : number
57
+ productDescription : string
58
+ productImagePath : string
59
+ inventoryId : string
60
+ inventoryLocation : string
61
+ inventoryDescription : string
62
+ inventoryWebsite : string
63
+ }
64
+
39
65
const API_URL = "https://localhost:5000" ;
40
66
const PRODUCT_URL = `${ API_URL } /api-gw/product-catalog/api/products` ;
41
67
const PRODUCT_SEARCH_URL = `${ API_URL } /api-gw/product-catalog/api/products/search` ;
68
+ const CART_URL = `${ API_URL } /api-gw/shopping-cart/api/carts` ;
42
69
43
70
const axios = Axios . create ( {
44
71
httpsAgent : new https . Agent ( {
@@ -75,14 +102,16 @@ export const getUserInfo = async (request: Request) => {
75
102
}
76
103
77
104
export async function searchProduct ( request : Request , query : string , price : number , page : number , pageSize : number = 10 ) {
78
- const { data } = await axios . get < any > (
105
+ const response = await axios . get < any > (
79
106
`${ PRODUCT_SEARCH_URL } /${ price } /${ page } /${ pageSize } `
80
107
, {
81
108
headers : {
82
109
cookie : request . headers . get ( "Cookie" ) ?. toString ( )
83
110
} as any
84
111
} ) ;
85
112
113
+ const { data } = response ;
114
+
86
115
const result = {
87
116
products : data . results ,
88
117
categoryTags : data . categoryTags ,
@@ -91,21 +120,17 @@ export async function searchProduct(request: Request, query: string, price: numb
91
120
totalItem : data . total
92
121
} as ProductSearchResult ;
93
122
94
- //console.log(result)
95
- return result ;
123
+ return { productSearchResult : result } ;
96
124
}
97
125
98
126
export async function getProductById ( request : Request , id : string ) {
99
- console . log ( "product_id" , id ) ;
100
127
const { data } = await axios . get < any > (
101
128
`${ PRODUCT_URL } /${ id } `
102
129
, {
103
130
headers : {
104
131
cookie : request . headers . get ( "Cookie" ) ?. toString ( )
105
132
} as any
106
133
} ) ;
107
-
108
- // console.log(data as ProductDetailModel);
109
134
return data as ProductDetailModel ;
110
135
}
111
136
@@ -118,3 +143,69 @@ export async function createUserSession(userId: string, redirectTo: string) {
118
143
} ,
119
144
} ) ;
120
145
}
146
+
147
+ export async function getCartForCurrentUser ( request : Request ) {
148
+ const cookie = request . headers . get ( "Cookie" ) ?. toString ( ) ! ;
149
+ const response = await axios . get < any > (
150
+ CART_URL
151
+ , {
152
+ headers : {
153
+ cookie : cookie
154
+ } as any
155
+ } ) ;
156
+
157
+ const { data } = response ;
158
+ const xsrfToken = convertCookie ( cookie ) [ 'XSRF-TOKEN' ] ;
159
+
160
+ console . log ( data as CartModel ) ;
161
+ return { cartData : data as CartModel , csrf : xsrfToken } ;
162
+ }
163
+
164
+ export async function updateCartForCurrentUser ( request : Request , productId : string ) {
165
+ const userData = await getUserInfo ( request ) ;
166
+ const { cartData, csrf } = await getCartForCurrentUser ( request ) ;
167
+ const cookie = request . headers . get ( "Cookie" ) ?. toString ( ) ;
168
+
169
+ if ( cartData . id === null ) {
170
+ // create new cart
171
+ const { data } = await axios . post < any > (
172
+ CART_URL ,
173
+ {
174
+ productId : productId ,
175
+ userId : userData . userId ,
176
+ quantity : 1 ,
177
+ } ,
178
+ {
179
+ headers : {
180
+ cookie : cookie ,
181
+ "X-XSRF-TOKEN" : csrf ,
182
+ } as any
183
+ } ) ;
184
+ return data as CartModel ;
185
+ } else {
186
+ // update cart
187
+ const { data } = await axios . put < any > (
188
+ CART_URL ,
189
+ {
190
+ productId : productId ,
191
+ quantity : 1 ,
192
+ } ,
193
+ {
194
+ headers : {
195
+ cookie : cookie ,
196
+ "X-XSRF-TOKEN" : csrf ,
197
+ } as any
198
+ } ) ;
199
+ return data as CartModel ;
200
+ }
201
+ }
202
+
203
+ function convertCookie ( cookie : string ) {
204
+ const str : string [ ] | any = cookie ?. toString ( ) . split ( '; ' ) ;
205
+ const result : any = { } ;
206
+ for ( let i in str ) {
207
+ const cur = str [ i ] . split ( '=' ) ;
208
+ result [ cur [ 0 ] ] = cur [ 1 ] ;
209
+ }
210
+ return result ;
211
+ }
0 commit comments