remove inventory
This commit is contained in:
@@ -3,8 +3,8 @@ import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { Order } from '../entities/order.entity';
|
||||
import { OrderItem } from '../entities/order-item.entity';
|
||||
import { User } from '../../../users/entities/user.entity';
|
||||
import { Shop } from ../../..shops/entities/shop.entity';
|
||||
import { Product } from ../../..products/entities/product.entity';
|
||||
import { Shop } from '../../../shops/entities/shop.entity';
|
||||
import { Product } from '../../../products/entities/product.entity';
|
||||
import { CartService } from '../../../cart/providers/cart.service';
|
||||
import { OrderStatus, OrderUserAddress, OrderCarAddress } from '../interface/order.interface';
|
||||
import { PaymentMethodEnum, PaymentStatusEnum } from '../../../payments/interface/payment';
|
||||
@@ -17,8 +17,6 @@ import { OrderRepository } from '../repositories/order.repository';
|
||||
import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { Payment } from 'src/modules/payments/entities/payment.entity';
|
||||
import { InventoryService } from 'src/modules/inventory/inventory.service';
|
||||
import { BulkReserveFoodDto } from 'src/modules/inventory/dto/bulk-reserve-product.dto';
|
||||
import { StatusTransitionRef } from '../interface/order.interface';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.events';
|
||||
@@ -55,7 +53,6 @@ export class OrdersService {
|
||||
private readonly cartService: CartService,
|
||||
private readonly orderRepository: OrderRepository,
|
||||
private readonly paymentsService: PaymentsService,
|
||||
private readonly inventoryService: InventoryService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) { }
|
||||
|
||||
@@ -116,14 +113,6 @@ export class OrdersService {
|
||||
});
|
||||
|
||||
em.persist(payment);
|
||||
// reserve stock based on payment method.
|
||||
const bulkReserveFoodDto: BulkReserveFoodDto = {
|
||||
items: validated.orderItemsData.map(item => ({
|
||||
foodId: item.product.id,
|
||||
quantity: item.quantity,
|
||||
})),
|
||||
};
|
||||
await this.inventoryService.deductFromInventory(em, bulkReserveFoodDto);
|
||||
await em.flush();
|
||||
this.logger.debug(`Order ${order.id} created for user ${userId} (shop ${restaurantId})`);
|
||||
return order;
|
||||
@@ -426,15 +415,13 @@ export class OrdersService {
|
||||
const orderItemsData: OrderItemData[] = [];
|
||||
|
||||
for (const cartItem of cart.items) {
|
||||
const product = await this.em.findOne(Product, { id: cartItem.foodId }, { populate: ['shop', 'inventory'] });
|
||||
const product = await this.em.findOne(Product, { id: cartItem.foodId }, { populate: ['shop'] });
|
||||
if (!product) throw new NotFoundException(OrderMessage.FOOD_NOT_FOUND);
|
||||
|
||||
if (product.shop.id !== restaurantId) {
|
||||
throw new BadRequestException(OrderMessage.FOOD_NOT_BELONGS_TO_RESTAURANT);
|
||||
}
|
||||
|
||||
this.assertFoodHasSufficientStock(product, cartItem.quantity);
|
||||
|
||||
orderItemsData.push({
|
||||
product,
|
||||
quantity: cartItem.quantity,
|
||||
@@ -446,12 +433,6 @@ export class OrdersService {
|
||||
return orderItemsData;
|
||||
}
|
||||
|
||||
private assertFoodHasSufficientStock(product: Product, quantity: number) {
|
||||
const availableStock = product.inventory?.availableStock ?? 0;
|
||||
if (availableStock < quantity) {
|
||||
throw new BadRequestException(OrderMessage.FOOD_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getStats(restId: string) {
|
||||
|
||||
Reference in New Issue
Block a user