Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 60e8f3e

Browse files
author
Steph Dietz
committed
fix breaking change to remove from cart
1 parent 31f8bea commit 60e8f3e

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

backend/functions/assembly/crud.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ export function addToCart(cartId: string, productId: string): string {
261261
const cartItemId = cartId + "_" + productId;
262262

263263
if (cart === null || cart === "") {
264-
// If cart does not exist or is empty, create a new cart with the product
265264
const upsertResult = upsertCart(cartId, productId);
266265
if (upsertResult !== cartId) {
267266
console.log("Failed to create new cart:");
@@ -274,10 +273,17 @@ export function addToCart(cartId: string, productId: string): string {
274273
return quantityResult;
275274
}
276275
} else {
277-
const cartItems = cart.split(",").filter((item) => item.trim() !== "");
276+
const cartItems = cart.split(",");
277+
let isProductInCart = false;
278278

279-
if (cartItems.includes(productId)) {
280-
// If product is already in the cart, increase the quantity
279+
for (let i = 0; i < cartItems.length; i++) {
280+
if (cartItems[i] === productId) {
281+
isProductInCart = true;
282+
break;
283+
}
284+
}
285+
286+
if (isProductInCart) {
281287
const cartItemQuantity = collections.getText(
282288
consts.cartItemsCollection,
283289
cartItemId,
@@ -300,8 +306,7 @@ export function addToCart(cartId: string, productId: string): string {
300306
return upsertQuantityResult.error;
301307
}
302308
} else {
303-
// Add the new product to the existing cart
304-
const updatedCart = cart.length > 0 ? cart + "," + productId : productId;
309+
const updatedCart = cart + "," + productId;
305310
const upsertResult = upsertCart(cartId, updatedCart);
306311
if (upsertResult !== cartId) {
307312
console.log("Failed to update cart with new product:");
@@ -357,16 +362,34 @@ export function removeFromCart(cartId: string, productId: string): string {
357362
return result.error;
358363
}
359364

360-
// Fetch the current cart and update it
361365
const cart = collections.getText(consts.cartsCollection, cartId);
362-
const cartItems = cart.split(",").filter((item) => item !== productId);
366+
if (cart === null || cart === "") {
367+
return "Cart not found";
368+
}
369+
370+
const cartItems = cart.split(",");
371+
let updatedCart = "";
372+
let itemFound = false;
373+
374+
for (let i = 0; i < cartItems.length; i++) {
375+
if (cartItems[i] !== productId) {
376+
if (updatedCart.length > 0) {
377+
updatedCart += ",";
378+
}
379+
updatedCart += cartItems[i];
380+
} else {
381+
itemFound = true;
382+
}
383+
}
384+
385+
if (!itemFound) {
386+
return "Item not found in cart";
387+
}
363388

364-
if (cartItems.length === 0) {
365-
// If the cart is empty, remove the cart from the collection
389+
if (updatedCart === "") {
366390
collections.remove(consts.cartsCollection, cartId);
367391
} else {
368-
// Update the cart with the remaining items
369-
collections.upsert(consts.cartsCollection, cartId, cartItems.join(","));
392+
collections.upsert(consts.cartsCollection, cartId, updatedCart);
370393
}
371394

372395
return "success";

0 commit comments

Comments
 (0)