add discount

This commit is contained in:
2026-07-01 09:47:58 +03:30
parent 5d0e862259
commit 77acab935a
5 changed files with 48 additions and 10 deletions
@@ -25,10 +25,10 @@ export class AdminOrderItemDto {
}
export class AdminCreateOrderDto {
@ApiProperty({ description: 'User ID to create the order for' })
@ApiPropertyOptional({ description: 'User ID to create the order for' })
@IsString()
@IsNotEmpty()
userId: string;
@IsOptional()
userId?: string;
@ApiProperty({ type: [AdminOrderItemDto], description: 'Order items' })
@IsArray()
@@ -47,6 +47,12 @@ export class AdminCreateOrderDto {
@IsNotEmpty()
paymentMethodId: string;
@ApiPropertyOptional({ description: 'Extra discount amount to apply on the order', minimum: 0 })
@IsOptional()
@IsNumber()
@Min(0)
discountAmount?: number;
@ApiPropertyOptional({ description: 'Order description or notes' })
@IsOptional()
@IsString()
+14 -6
View File
@@ -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);
}
}