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

Commit 2a13fbf

Browse files
author
Steph Dietz
committed
update remove from cart function
1 parent 6111d75 commit 2a13fbf

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

backend/functions/assembly/crud.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,21 @@ export function decreaseQuantity(cartId: string, productId: string): string {
348348

349349
export 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+
// Fetch the current cart and update it
357+
const cart = collections.getText(consts.cartsCollection, cartId);
358+
const cartItems = cart.split(",").filter((item) => item !== productId);
359+
360+
if (cartItems.length === 0) {
361+
// If the cart is empty, remove the cart from the collection
362+
collections.remove(consts.cartsCollection, cartId);
363+
} else {
364+
// Update the cart with the remaining items
365+
collections.upsert(consts.cartsCollection, cartId, cartItems.join(","));
375366
}
376367

377368
return "success";

0 commit comments

Comments
 (0)