get desc on cancel order by user or admin

This commit is contained in:
2025-12-19 21:22:04 +03:30
parent 465810efc2
commit b7c8ba6862
4 changed files with 37 additions and 8 deletions
@@ -1,5 +1,5 @@
import { Controller, Get, Post, Param, UseGuards, Patch, Query } from '@nestjs/common'; import { Controller, Get, Post, Param, UseGuards, Patch, Query, Body } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiHeader } from '@nestjs/swagger'; import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiHeader, ApiBody } from '@nestjs/swagger';
import { OrdersService } from '../providers/orders.service'; import { OrdersService } from '../providers/orders.service';
import { AuthGuard } from '../../auth/guards/auth.guard'; import { AuthGuard } from '../../auth/guards/auth.guard';
import { UserId } from '../../../common/decorators/user-id.decorator'; import { UserId } from '../../../common/decorators/user-id.decorator';
@@ -8,16 +8,17 @@ import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
import { FindOrdersDto } from '../dto/find-orders.dto'; import { FindOrdersDto } from '../dto/find-orders.dto';
import { OrderStatus } from '../interface/order.interface'; import { OrderStatus } from '../interface/order.interface';
import { API_HEADER_SLUG } from 'src/common/constants/index'; import { API_HEADER_SLUG } from 'src/common/constants/index';
import { UpdateOrderStatusDto } from '../dto/update-order-status.dto';
@ApiTags('orders') @ApiTags('orders')
@ApiBearerAuth() @ApiBearerAuth()
@ApiHeader(API_HEADER_SLUG)
@Controller() @Controller()
export class OrdersController { export class OrdersController {
constructor(private readonly ordersService: OrdersService) {} constructor(private readonly ordersService: OrdersService) {}
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@Post('public/checkout') @Post('public/checkout')
@ApiHeader(API_HEADER_SLUG)
@ApiOperation({ summary: 'Checkout : create order and payment record' }) @ApiOperation({ summary: 'Checkout : create order and payment record' })
checkout(@UserId() userId: string, @RestId() restaurantId: string) { checkout(@UserId() userId: string, @RestId() restaurantId: string) {
return this.ordersService.checkout(userId, restaurantId); return this.ordersService.checkout(userId, restaurantId);
@@ -25,6 +26,7 @@ export class OrdersController {
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@Get('public/orders') @Get('public/orders')
@ApiHeader(API_HEADER_SLUG)
@ApiOperation({ summary: 'Get all orders with pagination and filters' }) @ApiOperation({ summary: 'Get all orders with pagination and filters' })
findAll(@RestId() restId: string, @Query() dto: FindOrdersDto, @UserId() userId: string) { findAll(@RestId() restId: string, @Query() dto: FindOrdersDto, @UserId() userId: string) {
return this.ordersService.findAllForUser(restId, dto, userId); return this.ordersService.findAllForUser(restId, dto, userId);
@@ -33,6 +35,7 @@ export class OrdersController {
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'Get an order By id for User' }) @ApiOperation({ summary: 'Get an order By id for User' })
@ApiParam({ name: 'orderId', description: 'Order ID' }) @ApiParam({ name: 'orderId', description: 'Order ID' })
@ApiHeader(API_HEADER_SLUG)
@Get('public/orders/:orderId') @Get('public/orders/:orderId')
findOne(@Param('orderId') orderId: string, @RestId() restId: string) { findOne(@Param('orderId') orderId: string, @RestId() restId: string) {
return this.ordersService.findOne(orderId, restId); return this.ordersService.findOne(orderId, restId);
@@ -45,12 +48,20 @@ export class OrdersController {
description: 'Order status', description: 'Order status',
enum: OrderStatus, enum: OrderStatus,
}) })
@ApiHeader(API_HEADER_SLUG)
@ApiBody({ type: UpdateOrderStatusDto })
@ApiOperation({ summary: 'Update status of an order By User' }) @ApiOperation({ summary: 'Update status of an order By User' })
@ApiParam({ name: 'id', description: 'Order ID' }) @ApiParam({ name: 'id', description: 'Order ID' })
cancelOrder(@Param('id') id: string, @Param('status') status: OrderStatus, @RestId() restId: string) { cancelOrder(
return this.ordersService.changeOrderStatus(id, restId, status, 'user'); @Body() dto: UpdateOrderStatusDto,
@Param('id') orderId: string,
@Param('status') status: OrderStatus,
@RestId() restId: string,
) {
return this.ordersService.changeOrderStatus(orderId, restId, status, 'user', dto.desc);
} }
/******************** Admin Routes **********************/
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@Get('admin/orders') @Get('admin/orders')
@ApiOperation({ summary: 'Get all orders with pagination and filters' }) @ApiOperation({ summary: 'Get all orders with pagination and filters' })
@@ -70,13 +81,19 @@ export class OrdersController {
@Patch('admin/orders/:orderId/:status') @Patch('admin/orders/:orderId/:status')
@ApiOperation({ summary: 'Update an order status' }) @ApiOperation({ summary: 'Update an order status' })
@ApiParam({ name: 'orderId', description: 'Order ID' }) @ApiParam({ name: 'orderId', description: 'Order ID' })
@ApiBody({ type: UpdateOrderStatusDto })
@ApiParam({ @ApiParam({
name: 'status', name: 'status',
description: 'Order status', description: 'Order status',
enum: OrderStatus, enum: OrderStatus,
}) })
updateStatus(@Param('orderId') orderId: string, @Param('status') status: OrderStatus, @RestId() restId: string) { updateStatus(
return this.ordersService.changeOrderStatus(orderId, restId, status, 'admin'); @Param('orderId') orderId: string,
@Body() dto: UpdateOrderStatusDto,
@Param('status') status: OrderStatus,
@RestId() restId: string,
) {
return this.ordersService.changeOrderStatus(orderId, restId, status, 'admin', dto.desc);
} }
// @UseGuards(AdminAuthGuard) // @UseGuards(AdminAuthGuard)
@@ -0,0 +1,10 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsString } from 'class-validator';
export class UpdateOrderStatusDto {
@ApiPropertyOptional({
description: 'Change Status description',
})
@IsOptional()
@IsString()
desc?: string;
}
@@ -26,7 +26,6 @@ export enum OrderStatus {
READY = 'ready', READY = 'ready',
SHIPPED = 'shipped', SHIPPED = 'shipped',
COMPLETED = 'completed', COMPLETED = 'completed',
CANCELED = 'canceled', CANCELED = 'canceled',
} }
@@ -260,13 +260,16 @@ export class OrdersService {
restId: string, restId: string,
toStatus: OrderStatus, toStatus: OrderStatus,
ref: StatusTransitionRef, ref: StatusTransitionRef,
desc?: string,
): Promise<Order> { ): Promise<Order> {
const order = await this.getOrderOrFail(orderId, restId); const order = await this.getOrderOrFail(orderId, restId);
this.assertStatusTransitionAllowed(order, toStatus, ref); this.assertStatusTransitionAllowed(order, toStatus, ref);
order.status = toStatus; order.status = toStatus;
order.history.push({ status: toStatus, changedAt: new Date(), desc: desc || null });
await this.em.persistAndFlush(order); await this.em.persistAndFlush(order);
this.eventEmitter.emit( this.eventEmitter.emit(
OrderStatusChangedEvent.name, OrderStatusChangedEvent.name,
new OrderStatusChangedEvent( new OrderStatusChangedEvent(