up
This commit is contained in:
@@ -21,7 +21,6 @@ import { Cart, CartItem } from '../interfaces/cart.interface';
|
||||
import { CouponService } from 'src/modules/coupons/providers/coupon.service';
|
||||
import { OrderCouponDetail } from 'src/modules/orders/interface/order-status';
|
||||
import { CouponType } from 'src/modules/coupons/interface/coupon';
|
||||
import { Order } from '../../orders/entities/order.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CartService {
|
||||
@@ -95,7 +94,7 @@ export class CartService {
|
||||
const cart = await this.getOrCreateCart(userId, restaurantId);
|
||||
|
||||
// Find food
|
||||
const food = await this.em.findOne(Food, { id: foodId }, { populate: ['restaurant'] });
|
||||
const food = await this.em.findOne(Food, { id: foodId }, { populate: ['restaurant', 'inventory'] });
|
||||
if (!food) {
|
||||
throw new NotFoundException(`Food with ID ${foodId} not found`);
|
||||
}
|
||||
@@ -106,8 +105,9 @@ export class CartService {
|
||||
}
|
||||
|
||||
// Check stock availability
|
||||
if (food.stock < addItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${food.stock}`);
|
||||
const availableStock = food.inventory?.availableStock ?? 0;
|
||||
if (availableStock < addItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${availableStock}`);
|
||||
}
|
||||
|
||||
// Check if item already exists in cart
|
||||
@@ -119,8 +119,8 @@ export class CartService {
|
||||
const newQuantity = existingItem.quantity + addItemDto.quantity;
|
||||
|
||||
// Check stock for new total quantity
|
||||
if (food.stock < newQuantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${food.stock}`);
|
||||
if (availableStock < newQuantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${availableStock}`);
|
||||
}
|
||||
|
||||
const itemPrice = food.price || 0;
|
||||
@@ -221,7 +221,7 @@ export class CartService {
|
||||
// Process each item
|
||||
for (const addItemDto of bulkAddItemsDto.items) {
|
||||
// Find food
|
||||
const food = await this.em.findOne(Food, { id: addItemDto.foodId }, { populate: ['restaurant'] });
|
||||
const food = await this.em.findOne(Food, { id: addItemDto.foodId }, { populate: ['restaurant', 'inventory'] });
|
||||
if (!food) {
|
||||
throw new NotFoundException(`Food with ID ${addItemDto.foodId} not found`);
|
||||
}
|
||||
@@ -232,8 +232,9 @@ export class CartService {
|
||||
}
|
||||
|
||||
// Check stock availability
|
||||
if (food.stock < addItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${food.stock}`);
|
||||
const availableStock = food.inventory?.availableStock ?? 0;
|
||||
if (availableStock < addItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${availableStock}`);
|
||||
}
|
||||
|
||||
// Check if item already exists in cart
|
||||
@@ -245,8 +246,8 @@ export class CartService {
|
||||
const newQuantity = existingItem.quantity + addItemDto.quantity;
|
||||
|
||||
// Check stock for new total quantity
|
||||
if (food.stock < newQuantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${food.stock}`);
|
||||
if (availableStock < newQuantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${availableStock}`);
|
||||
}
|
||||
|
||||
const itemPrice = food.price || 0;
|
||||
@@ -307,14 +308,15 @@ export class CartService {
|
||||
const item = cart.items[itemIndex];
|
||||
|
||||
// Find food to check stock
|
||||
const food = await this.em.findOne(Food, { id: item.foodId }, { populate: ['restaurant'] });
|
||||
const food = await this.em.findOne(Food, { id: item.foodId }, { populate: ['restaurant', 'inventory'] });
|
||||
if (!food) {
|
||||
throw new NotFoundException(`Food with ID ${item.foodId} not found`);
|
||||
}
|
||||
|
||||
// Check stock availability
|
||||
if (food.stock < updateItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${food.stock}`);
|
||||
const availableStock = food.inventory?.availableStock ?? 0;
|
||||
if (availableStock < updateItemDto.quantity) {
|
||||
throw new BadRequestException(`Insufficient stock for food ${food.title}. Available: ${availableStock}`);
|
||||
}
|
||||
|
||||
// Update quantity
|
||||
|
||||
Reference in New Issue
Block a user