From 8b08873bd2d7133aa216abef9ebf981a6857d0f4 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 7 Dec 2025 16:26:42 +0330 Subject: [PATCH] status --- src/modules/orders/interface/order-status.ts | 1 + src/modules/orders/orders.controller.ts | 45 ++++++++++++++------ src/modules/orders/orders.service.ts | 41 +++++++++++++++++- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/modules/orders/interface/order-status.ts b/src/modules/orders/interface/order-status.ts index 935e0b4..abed1d8 100644 --- a/src/modules/orders/interface/order-status.ts +++ b/src/modules/orders/interface/order-status.ts @@ -19,6 +19,7 @@ export enum OrderStatus { ReadyForDeliveryCar = 'readyForDeliveryCar', ReadyForDeliveryCourier = 'readyForDeliveryCourier', + Shipping = 'shipping', // Final status Delivered = 'delivered', } diff --git a/src/modules/orders/orders.controller.ts b/src/modules/orders/orders.controller.ts index 2ac5a28..48a4f59 100644 --- a/src/modules/orders/orders.controller.ts +++ b/src/modules/orders/orders.controller.ts @@ -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); } } diff --git a/src/modules/orders/orders.service.ts b/src/modules/orders/orders.service.ts index e2dfeb7..ca9db06 100644 --- a/src/modules/orders/orders.service.ts +++ b/src/modules/orders/orders.service.ts @@ -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 = { + 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