order
This commit is contained in:
@@ -1,20 +1,25 @@
|
|||||||
import type { CouponType } from 'src/modules/coupons/interface/coupon';
|
import type { CouponType } from 'src/modules/coupons/interface/coupon';
|
||||||
|
|
||||||
export enum OrderStatus {
|
export enum OrderStatus {
|
||||||
|
// Initial status
|
||||||
Pending = 'pending',
|
Pending = 'pending',
|
||||||
|
|
||||||
|
// Cancellation statuses
|
||||||
|
CancelledBySystem = 'cancelledBySystem',
|
||||||
|
CancelledByUser = 'cancelledByUser',
|
||||||
|
RejectedByRestaurant = 'rejectedByRestaurant',
|
||||||
|
|
||||||
|
// Active processing statuses
|
||||||
Confirmed = 'confirmed',
|
Confirmed = 'confirmed',
|
||||||
Preparing = 'preparing',
|
Preparing = 'preparing',
|
||||||
|
|
||||||
RejectedByRestaurant = 'rejectedByRestaurant',
|
// Ready for pickup/delivery statuses
|
||||||
CancelledByUser = 'cancelledByUser',
|
|
||||||
CancelledBySystem = 'cancelledBySystem',
|
|
||||||
|
|
||||||
ReadyForCustomerPickup = 'readyForCustomerPickup',
|
ReadyForCustomerPickup = 'readyForCustomerPickup',
|
||||||
ReadyForDineIn = 'readyForDineIn',
|
ReadyForDineIn = 'readyForDineIn',
|
||||||
ReadyForDeliveryCar = 'readyForDeliveryCar',
|
ReadyForDeliveryCar = 'readyForDeliveryCar',
|
||||||
ReadyForDeliveryCourier = 'readyForDeliveryCourier',
|
ReadyForDeliveryCourier = 'readyForDeliveryCourier',
|
||||||
|
|
||||||
|
// Final status
|
||||||
Delivered = 'delivered',
|
Delivered = 'delivered',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,4 +98,13 @@ export class OrdersController {
|
|||||||
readyForPickup(@Param('orderId') orderId: string, @RestId() restId: string) {
|
readyForPickup(@Param('orderId') orderId: string, @RestId() restId: string) {
|
||||||
return this.ordersService.readyForDelivery(orderId, restId);
|
return this.ordersService.readyForDelivery(orderId, restId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@Patch('admin/orders/:orderId/delivered')
|
||||||
|
@ApiOperation({ summary: 'Mark an order as delivered' })
|
||||||
|
@ApiParam({ name: 'orderId', description: 'Order ID' })
|
||||||
|
markAsDelivered(@Param('orderId') orderId: string, @RestId() restId: string) {
|
||||||
|
return this.ordersService.markAsDelivered(orderId, restId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -287,7 +287,10 @@ export class OrdersService {
|
|||||||
if (!order) {
|
if (!order) {
|
||||||
throw new NotFoundException('Order not found');
|
throw new NotFoundException('Order not found');
|
||||||
}
|
}
|
||||||
order.status = OrderStatus.Confirmed;
|
if (order.status !== OrderStatus.Confirmed) {
|
||||||
|
throw new BadRequestException('Order must be confirmed before preparing');
|
||||||
|
}
|
||||||
|
order.status = OrderStatus.Preparing;
|
||||||
await this.em.persistAndFlush(order);
|
await this.em.persistAndFlush(order);
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
@@ -344,6 +347,30 @@ export class OrdersService {
|
|||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async markAsDelivered(orderId: string, restId: string) {
|
||||||
|
const order = await this.em.findOne(Order, { id: orderId, restaurant: { id: restId } });
|
||||||
|
if (!order) {
|
||||||
|
throw new NotFoundException('Order not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const readyStatuses = [
|
||||||
|
OrderStatus.ReadyForCustomerPickup,
|
||||||
|
OrderStatus.ReadyForDineIn,
|
||||||
|
OrderStatus.ReadyForDeliveryCar,
|
||||||
|
OrderStatus.ReadyForDeliveryCourier,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!readyStatuses.includes(order.status)) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`Order must be in a ready status to mark as delivered. Current status: ${order.status}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
order.status = OrderStatus.Delivered;
|
||||||
|
await this.em.persistAndFlush(order);
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleanup job to handle abandoned orders (pending payment for >30 minutes)
|
* Cleanup job to handle abandoned orders (pending payment for >30 minutes)
|
||||||
* Runs every 10 minutes to check for abandoned orders
|
* Runs every 10 minutes to check for abandoned orders
|
||||||
|
|||||||
Reference in New Issue
Block a user