feat: add bulk to cart add

This commit is contained in:
mahyargdz
2025-09-14 15:38:25 +03:30
parent be82059172
commit eb7305366c
3 changed files with 124 additions and 0 deletions
+60
View File
@@ -3,6 +3,7 @@ import { ClientSession, Types, startSession } from "mongoose";
import { CartRepository, CartShipItemRepo } from "./cart.repository";
import { AddToCartDTO } from "./DTO/addToCart.dto";
import { BulkAddToCartDTO } from "./DTO/bulkAddToCart.dto";
import { CartDTO } from "./DTO/cart.dto";
import { RemoveFromCartDTO } from "./DTO/removeFromCart.dto";
import { UpdateCartDTO } from "./DTO/updateCart.dto";
@@ -79,6 +80,65 @@ class CartService {
}
}
async bulkAddToCartS(bulkAddToCartDto: BulkAddToCartDTO, userId: string) {
const session = await startSession();
session.startTransaction();
try {
const { items } = bulkAddToCartDto;
// Get or create cart
let cart = await this.cartRepo.model.findOne({ user: userId });
if (!cart) {
cart = (await this.cartRepo.model.create([{ user: userId }], { session }))[0];
}
// Validate all products before making any changes
for (const item of items) {
const existingCartItem = cart.items.find(
(cartItem) => cartItem.product === item.productId && cartItem.variant.toString() === item.variantId,
);
const existingQuantity = existingCartItem ? existingCartItem.quantity : 0;
await this.validateProduct(item.productId, item.variantId, existingQuantity + item.quantity);
}
// Process each item
for (const item of items) {
const existingCartItem = cart.items.find(
(cartItem) => cartItem.product === item.productId && cartItem.variant.toString() === item.variantId,
);
if (existingCartItem) {
// Update quantity if item already exists in the cart
existingCartItem.quantity += item.quantity;
} else {
// Add new item to the cart
cart.items.push({
product: item.productId,
quantity: item.quantity,
variant: new Types.ObjectId(item.variantId),
});
}
}
await cart.save({ session });
// Commit the transaction
await session.commitTransaction();
session.endSession();
return {
message: CartMessage.ItemAdded,
cart: await this.getCartS(userId),
};
} catch (error) {
// Rollback any changes made in the transaction
await session.abortTransaction();
session.endSession();
throw error; // Re-throw the error to be handled by the calling errorHandler
}
}
async updateCartS(updateCartDto: UpdateCartDTO, userId: string) {
const { productId, variantId, quantity } = updateCartDto;