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
@@ -19,6 +19,7 @@ export enum OrderStatus {
ReadyForDeliveryCar = 'readyForDeliveryCar',
ReadyForDeliveryCourier = 'readyForDeliveryCourier',
Shipping = 'shipping',
// Final status
Delivered = 'delivered',
}
+33 -12
View File
@@ -90,21 +90,42 @@ export class OrdersController {
return this.ordersService.rejectOrder(orderId, restId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/orders/:orderId/ready')
@ApiOperation({ summary: 'Ready for pickup or delivery ' })
@ApiParam({ name: 'orderId', description: 'Order ID' })
readyForPickup(@Param('orderId') orderId: string, @RestId() restId: string) {
return this.ordersService.readyForDelivery(orderId, restId);
}
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Patch('admin/orders/:orderId/ready')
// @ApiOperation({ summary: 'Ready for pickup or delivery ' })
// @ApiParam({ name: 'orderId', description: 'Order ID' })
// readyForPickup(@Param('orderId') orderId: string, @RestId() restId: string) {
// 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);
// }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/orders/:orderId/delivered')
@ApiOperation({ summary: 'Mark an order as delivered' })
@Patch('admin/orders/:orderId/:status')
@ApiOperation({ summary: 'Update an order status' })
@ApiParam({ name: 'orderId', description: 'Order ID' })
markAsDelivered(@Param('orderId') orderId: string, @RestId() restId: string) {
return this.ordersService.markAsDelivered(orderId, restId);
@ApiParam({
name: 'status',
description: 'Order status',
enum: [
'readyForCustomerPickup',
'readyForDineIn',
'readyForDeliveryCar',
'readyForDeliveryCourier',
'shipping',
'delivered',
],
})
updateStatus(@Param('orderId') orderId: string, @Param('status') status: string, @RestId() restId: string) {
return this.ordersService.updateStatus(orderId, status, restId);
}
}
+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