update cart
This commit is contained in:
@@ -3,7 +3,6 @@ import { Food } from '../../foods/entities/food.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CacheService } from '../../utils/cache.service';
|
||||
import { ulid } from 'ulid';
|
||||
import { AddItemToCartDto } from '../dto/add-item.dto';
|
||||
import { BulkAddItemsToCartDto } from '../dto/bulk-add-items.dto';
|
||||
import { UpdateItemQuantityDto } from '../dto/update-item.dto';
|
||||
@@ -76,13 +75,13 @@ export class CartService {
|
||||
/**
|
||||
* Add item to cart (increment quantity if item exists, otherwise create new)
|
||||
*/
|
||||
async addItem(userId: string, restaurantId: string, addItemDto: AddItemToCartDto): Promise<Cart> {
|
||||
async addItem(userId: string, restaurantId: string, foodId: string, addItemDto: AddItemToCartDto): Promise<Cart> {
|
||||
const cart = await this.getOrCreateCart(userId, restaurantId);
|
||||
|
||||
// Find food
|
||||
const food = await this.em.findOne(Food, { id: addItemDto.foodId }, { populate: ['restaurant'] });
|
||||
const food = await this.em.findOne(Food, { id: foodId }, { populate: ['restaurant'] });
|
||||
if (!food) {
|
||||
throw new NotFoundException(`Food with ID ${addItemDto.foodId} not found`);
|
||||
throw new NotFoundException(`Food with ID ${foodId} not found`);
|
||||
}
|
||||
|
||||
// Check if food belongs to the restaurant
|
||||
@@ -96,7 +95,7 @@ export class CartService {
|
||||
}
|
||||
|
||||
// Check if item already exists in cart
|
||||
const existingItemIndex = cart.items.findIndex(item => item.foodId === addItemDto.foodId);
|
||||
const existingItemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
|
||||
if (existingItemIndex >= 0) {
|
||||
// Update existing item quantity
|
||||
@@ -128,7 +127,6 @@ export class CartService {
|
||||
const itemFinalPrice = itemTotalPrice - itemTotalDiscount;
|
||||
|
||||
const cartItem: CartItem = {
|
||||
itemId: ulid(),
|
||||
foodId: food.id,
|
||||
foodTitle: food.title,
|
||||
quantity: addItemDto.quantity,
|
||||
@@ -147,6 +145,57 @@ export class CartService {
|
||||
return cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment item quantity in cart by 1
|
||||
*/
|
||||
async incrementItem(userId: string, restaurantId: string, foodId: string): Promise<Cart> {
|
||||
return this.addItem(userId, restaurantId, foodId, { foodId, quantity: 1 });
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement item quantity in cart by 1 (removes item if quantity reaches 0)
|
||||
*/
|
||||
async decrementItem(userId: string, restaurantId: string, foodId: string): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
throw new NotFoundException(`Item with food ID ${foodId} not found in cart`);
|
||||
}
|
||||
|
||||
const existingItem = cart.items[itemIndex];
|
||||
const newQuantity = existingItem.quantity - 1;
|
||||
|
||||
if (newQuantity <= 0) {
|
||||
// Remove item if quantity becomes 0 or less
|
||||
cart.items.splice(itemIndex, 1);
|
||||
} else {
|
||||
// Update item quantity and recalculate price
|
||||
const food = await this.em.findOne(Food, { id: foodId });
|
||||
if (!food) {
|
||||
throw new NotFoundException(`Food with ID ${foodId} not found`);
|
||||
}
|
||||
|
||||
const itemPrice = food.price || 0;
|
||||
const itemDiscount = food.discount || 0;
|
||||
const itemTotalPrice = itemPrice * newQuantity;
|
||||
const itemTotalDiscount = itemDiscount * newQuantity;
|
||||
const itemFinalPrice = itemTotalPrice - itemTotalDiscount;
|
||||
|
||||
cart.items[itemIndex] = {
|
||||
...existingItem,
|
||||
quantity: newQuantity,
|
||||
totalPrice: itemFinalPrice,
|
||||
};
|
||||
}
|
||||
|
||||
// Recalculate cart totals
|
||||
await this.recalculateCartTotals(cart);
|
||||
await this.saveCart(cart);
|
||||
|
||||
return cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk add items to cart (increments quantity if items exist)
|
||||
*/
|
||||
@@ -204,7 +253,6 @@ export class CartService {
|
||||
const itemFinalPrice = itemTotalPrice - itemTotalDiscount;
|
||||
|
||||
const cartItem: CartItem = {
|
||||
itemId: ulid(),
|
||||
foodId: food.id,
|
||||
foodTitle: food.title,
|
||||
quantity: addItemDto.quantity,
|
||||
@@ -230,14 +278,14 @@ export class CartService {
|
||||
async updateItemQuantity(
|
||||
userId: string,
|
||||
restaurantId: string,
|
||||
itemId: string,
|
||||
foodId: string,
|
||||
updateItemDto: UpdateItemQuantityDto,
|
||||
): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.itemId === itemId);
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
throw new NotFoundException(`Item with ID ${itemId} not found in cart`);
|
||||
throw new NotFoundException(`Item with food ID ${foodId} not found in cart`);
|
||||
}
|
||||
|
||||
const item = cart.items[itemIndex];
|
||||
@@ -276,12 +324,12 @@ export class CartService {
|
||||
/**
|
||||
* Remove item from cart
|
||||
*/
|
||||
async removeItem(userId: string, restaurantId: string, itemId: string): Promise<Cart> {
|
||||
async removeItem(userId: string, restaurantId: string, foodId: string): Promise<Cart> {
|
||||
const cart = await this.findOne(userId, restaurantId);
|
||||
|
||||
const itemIndex = cart.items.findIndex(item => item.itemId === itemId);
|
||||
const itemIndex = cart.items.findIndex(item => item.foodId === foodId);
|
||||
if (itemIndex < 0) {
|
||||
throw new NotFoundException(`Item with ID ${itemId} not found in cart`);
|
||||
throw new NotFoundException(`Item with food ID ${foodId} not found in cart`);
|
||||
}
|
||||
|
||||
// Remove item
|
||||
|
||||
Reference in New Issue
Block a user