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

Commit 6111d75

Browse files
author
Steph Dietz
committed
revert breaking change
1 parent 64a060e commit 6111d75

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

backend/functions/assembly/crud.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ 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 === "" || cart.trim() === "") {
264-
// If the cart is null or empty, create a new cart with the product
263+
if (cart === null || cart === "") {
265264
const upsertResult = upsertCart(cartId, productId);
266265
if (upsertResult !== cartId) {
267266
console.log("Failed to create new cart:");
@@ -274,7 +273,7 @@ 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(",");
278277
if (cartItems.includes(productId)) {
279278
const cartItemQuantity = collections.getText(
280279
consts.cartItemsCollection,
@@ -298,7 +297,8 @@ export function addToCart(cartId: string, productId: string): string {
298297
return upsertQuantityResult.error;
299298
}
300299
} else {
301-
const upsertResult = upsertCart(cartId, cart + "," + productId);
300+
const updatedCart = cart.length > 0 ? cart + "," + productId : productId;
301+
const upsertResult = upsertCart(cartId, updatedCart);
302302
if (upsertResult !== cartId) {
303303
console.log("Failed to update cart with new product:");
304304
return upsertResult;
@@ -348,20 +348,30 @@ 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+
351367
const result = collections.remove(consts.cartItemsCollection, cartItemId);
352368
if (!result.isSuccessful) {
353369
return result.error;
354370
}
355371

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(","));
372+
const cartRes = collections.upsert(consts.cartsCollection, cartId, newCart);
373+
if (!cartRes.isSuccessful) {
374+
return cartRes.error;
365375
}
366376

367377
return "success";

0 commit comments

Comments
 (0)