@@ -260,7 +260,8 @@ export function addToCart(cartId: string, productId: string): string {
260260 const cart = collections . getText ( consts . cartsCollection , cartId ) ;
261261 const cartItemId = cartId + "_" + productId ;
262262
263- if ( cart === null || cart === "" ) {
263+ if ( cart === null || cart === "" || cart . trim ( ) === "" ) {
264+ // If the cart is null or empty, create a new cart with the product
264265 const upsertResult = upsertCart ( cartId , productId ) ;
265266 if ( upsertResult !== cartId ) {
266267 console . log ( "Failed to create new cart:" ) ;
@@ -273,7 +274,7 @@ export function addToCart(cartId: string, productId: string): string {
273274 return quantityResult ;
274275 }
275276 } else {
276- const cartItems = cart . split ( "," ) ;
277+ const cartItems = cart . split ( "," ) . filter ( ( item ) => item . trim ( ) !== "" ) ;
277278 if ( cartItems . includes ( productId ) ) {
278279 const cartItemQuantity = collections . getText (
279280 consts . cartItemsCollection ,
@@ -297,8 +298,7 @@ export function addToCart(cartId: string, productId: string): string {
297298 return upsertQuantityResult . error ;
298299 }
299300 } else {
300- const updatedCart = cart . length > 0 ? cart + "," + productId : productId ;
301- const upsertResult = upsertCart ( cartId , updatedCart ) ;
301+ const upsertResult = upsertCart ( cartId , cart + "," + productId ) ;
302302 if ( upsertResult !== cartId ) {
303303 console . log ( "Failed to update cart with new product:" ) ;
304304 return upsertResult ;
@@ -348,30 +348,20 @@ export function decreaseQuantity(cartId: string, productId: string): string {
348348
349349export function removeFromCart ( cartId : string , productId : string ) : string {
350350 const cartItemId = cartId + "_" + productId ;
351- const cart = collections . getText ( consts . cartsCollection , cartId ) ;
352- if ( cart === null || cart === "" ) {
353- return "Cart not found" ;
354- }
355- const cartItems = cart . split ( "," ) ;
356-
357- const newCartItems : string [ ] = [ ] ;
358-
359- // Manually filter out the cartItemId
360- for ( let i = 0 ; i < cartItems . length ; i ++ ) {
361- if ( cartItems [ i ] !== productId ) {
362- newCartItems . push ( cartItems [ i ] ) ;
363- }
364- }
365- const newCart = newCartItems . join ( "," ) ;
366-
367351 const result = collections . remove ( consts . cartItemsCollection , cartItemId ) ;
368352 if ( ! result . isSuccessful ) {
369353 return result . error ;
370354 }
371355
372- const cartRes = collections . upsert ( consts . cartsCollection , cartId , newCart ) ;
373- if ( ! cartRes . isSuccessful ) {
374- return cartRes . error ;
356+ // Check if the cart is now empty
357+ const cart = collections . getText ( consts . cartsCollection , cartId ) ;
358+ const cartItems = cart . split ( "," ) . filter ( ( item ) => item !== productId ) ;
359+ if ( cartItems . length === 0 ) {
360+ // Optionally remove the cart if it's empty
361+ collections . remove ( consts . cartsCollection , cartId ) ;
362+ } else {
363+ // Update the cart to remove the product ID
364+ collections . upsert ( consts . cartsCollection , cartId , cartItems . join ( "," ) ) ;
375365 }
376366
377367 return "success" ;
0 commit comments