This commit is contained in:
2025-12-07 16:26:42 +03:30
parent b7452d793d
commit 8b08873bd2
3 changed files with 74 additions and 13 deletions
+40 -1
View File
@@ -8,7 +8,7 @@ import { Food } from '../foods/entities/food.entity';
import { UserAddress } from '../users/entities/user-address.entity';
import { CartService } from '../cart/providers/cart.service';
import { OrderStatus } from './interface/order-status';
import { PaymentStatusEnum } from '../payments/interface/payment';
import { PaymentMethodEnum, PaymentStatusEnum } from '../payments/interface/payment';
import { Cart } from '../cart/interfaces/cart.interface';
import { PaymentMethod } from '../payments/entities/payment-method.entity';
// import { PaymentGatewayService } from '../payments/services/payment-gateway.service.tss';
@@ -277,6 +277,12 @@ export class OrdersService {
if (!order) {
throw new NotFoundException('Order not found');
}
if (order.status !== OrderStatus.Pending) {
throw new BadRequestException('Order must be pending before accepting');
}
if (order.paymentMethod?.method === PaymentMethodEnum.Online && order.paymentStatus !== PaymentStatusEnum.Paid) {
throw new BadRequestException('Order must be paid online before accepting');
}
order.status = OrderStatus.Confirmed;
await this.em.persistAndFlush(order);
return order;
@@ -371,6 +377,39 @@ export class OrdersService {
return order;
}
async updateStatus(orderId: string, status: string, restId: string) {
const order = await this.em.findOne(Order, { id: orderId, restaurant: { id: restId } });
if (!order) {
throw new NotFoundException('Order not found');
}
// Map string status to OrderStatus enum
const statusMap: Record<string, OrderStatus> = {
ReadyForCustomerPickup: OrderStatus.ReadyForCustomerPickup,
readyForCustomerPickup: OrderStatus.ReadyForCustomerPickup,
ReadyForDineIn: OrderStatus.ReadyForDineIn,
readyForDineIn: OrderStatus.ReadyForDineIn,
ReadyForDeliveryCar: OrderStatus.ReadyForDeliveryCar,
readyForDeliveryCar: OrderStatus.ReadyForDeliveryCar,
ReadyForDeliveryCourier: OrderStatus.ReadyForDeliveryCourier,
readyForDeliveryCourier: OrderStatus.ReadyForDeliveryCourier,
shipping: OrderStatus.Shipping,
Delivered: OrderStatus.Delivered,
delivered: OrderStatus.Delivered,
};
const newStatus = statusMap[status];
if (!newStatus) {
throw new BadRequestException(
`Invalid status: ${status}. Allowed statuses: shipping, Delivered, ReadyForCustomerPickup, ReadyForDineIn, ReadyForDeliveryCar, ReadyForDeliveryCourier`,
);
}
order.status = newStatus;
await this.em.persistAndFlush(order);
return order;
}
/**
* Cleanup job to handle abandoned orders (pending payment for >30 minutes)
* Runs every 10 minutes to check for abandoned orders