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

Commit 5bf3c2c

Browse files
author
Steph Dietz
committed
re-add decrease quantity function
1 parent dba4694 commit 5bf3c2c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

backend/functions/assembly/crud.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,37 @@ export function addToCart(cartId: string, productId: string): string {
314314
return "success";
315315
}
316316

317+
export function decreaseQuantity(cartId: string, productId: string): string {
318+
const cartItemId = cartId + "_" + productId;
319+
const cartItemQuantity = collections.getText(
320+
consts.cartItemsCollection,
321+
cartItemId,
322+
);
323+
324+
if (cartItemQuantity === null || cartItemQuantity === "") {
325+
console.log("Failed to retrieve cart item quantity for:");
326+
return "error";
327+
}
328+
329+
const newQuantity = parseFloat(cartItemQuantity) - 1;
330+
if (newQuantity === 0) {
331+
return removeFromCart(cartId, productId);
332+
}
333+
334+
const upsertQuantityResult = collections.upsert(
335+
consts.cartItemsCollection,
336+
cartItemId,
337+
newQuantity.toString(),
338+
);
339+
340+
if (!upsertQuantityResult.isSuccessful) {
341+
console.log("Failed to update quantity:");
342+
return upsertQuantityResult.error;
343+
}
344+
345+
return "success";
346+
}
347+
317348
export function removeFromCart(cartId: string, productId: string): string {
318349
const cartItemId = cartId + "_" + productId;
319350
const result = collections.remove(consts.cartItemsCollection, cartItemId);

0 commit comments

Comments
 (0)