add discount
This commit is contained in:
@@ -67,7 +67,6 @@ export class OrdersService {
|
||||
async checkout(userId: string, restaurantId: string) {
|
||||
const cart = await this.cartService.findOneOrFail(userId, restaurantId);
|
||||
const validated = await this.validateCartForOrder(userId, restaurantId, cart);
|
||||
console.log(cart);
|
||||
|
||||
const order = await this.em.transactional(async em => {
|
||||
const order = em.create(Order, {
|
||||
@@ -148,7 +147,7 @@ console.log(cart);
|
||||
}
|
||||
|
||||
async createOrderForUser(restaurantId: string, dto: AdminCreateOrderDto): Promise<Order> {
|
||||
const user = await this.getUserOrFail(dto.userId);
|
||||
const user = dto.userId ? await this.getUserOrFail(dto.userId) : null;
|
||||
|
||||
const [restaurant, delivery] = await Promise.all([
|
||||
this.getRestaurantOrFail(restaurantId),
|
||||
@@ -162,7 +161,7 @@ console.log(cart);
|
||||
|
||||
const userAddress =
|
||||
delivery.method === DeliveryMethodEnum.DeliveryCourier
|
||||
? await this.resolveUserAddressForOrder(user, dto.addressId!)
|
||||
? await this.resolveUserAddressForOrder(user!, dto.addressId!)
|
||||
: null;
|
||||
|
||||
const orderItemsData = await this.buildOrderItemsDataFromDto(dto.items, restaurantId);
|
||||
@@ -170,7 +169,12 @@ console.log(cart);
|
||||
const subTotal = orderItemsData.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0);
|
||||
const itemsDiscount = orderItemsData.reduce((sum, item) => sum + item.discount * item.quantity, 0);
|
||||
const deliveryFee = delivery.deliveryFeeType === DeliveryFeeTypeEnum.FIXED ? Number(delivery.deliveryFee) : 0;
|
||||
const totalDiscount = itemsDiscount;
|
||||
const adminDiscount = dto.discountAmount ?? 0;
|
||||
const maxDiscount = subTotal - itemsDiscount;
|
||||
if (adminDiscount > maxDiscount) {
|
||||
throw new BadRequestException(OrderMessage.DISCOUNT_AMOUNT_EXCEEDS_ORDER_TOTAL);
|
||||
}
|
||||
const totalDiscount = itemsDiscount + adminDiscount;
|
||||
const total = subTotal - totalDiscount + deliveryFee;
|
||||
const totalItems = orderItemsData.reduce((sum, item) => sum + item.quantity, 0);
|
||||
|
||||
@@ -182,7 +186,7 @@ console.log(cart);
|
||||
userAddress,
|
||||
carAddress: delivery.method === DeliveryMethodEnum.DeliveryCar ? (dto.carAddress ?? null) : null,
|
||||
paymentMethod,
|
||||
couponDiscount: 0,
|
||||
couponDiscount: adminDiscount,
|
||||
couponDetail: null,
|
||||
itemsDiscount,
|
||||
totalDiscount,
|
||||
@@ -222,7 +226,7 @@ console.log(cart);
|
||||
await this.inventoryService.deductFromInventory(em, bulkReserveFoodDto);
|
||||
await em.flush();
|
||||
|
||||
this.logger.debug(`Admin created order ${order.id} for user ${user.id} (restaurant: ${restaurantId})`);
|
||||
this.logger.debug(`Admin created order ${order.id} for user ${user?.id || 'anonymous'} (restaurant: ${restaurantId})`);
|
||||
return order;
|
||||
});
|
||||
|
||||
@@ -639,6 +643,10 @@ console.log(cart);
|
||||
if (delivery.method === DeliveryMethodEnum.DeliveryCar && !dto.carAddress) {
|
||||
throw new BadRequestException(OrderMessage.CAR_ADDRESS_REQUIRED);
|
||||
}
|
||||
|
||||
if (delivery.method === DeliveryMethodEnum.DeliveryCourier && !dto.userId) {
|
||||
throw new BadRequestException(OrderMessage.USER_REQUIRED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user