fix controller bugs

This commit is contained in:
2026-01-27 16:52:12 +03:30
parent a9ce94f073
commit 9b24e7013c
2 changed files with 57 additions and 39 deletions
@@ -12,6 +12,7 @@ import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { AssignDesignerDto } from '../dto/assign-designer.dto'; import { AssignDesignerDto } from '../dto/assign-designer.dto';
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto'; import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto'; import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
@ApiTags('orders') @ApiTags('orders')
@ApiBearerAuth() @ApiBearerAuth()
@@ -19,21 +20,14 @@ import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto';
export class OrderController { export class OrderController {
constructor(private readonly orderService: OrderService) { } constructor(private readonly orderService: OrderService) { }
@Post('public/order') @Post('public/orders')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'create order ' }) @ApiOperation({ summary: 'create order ' })
createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) { createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) {
return this.orderService.createOrderAsUser(userId, body); return this.orderService.createOrderAsUser(userId, body);
} }
@Get('public/order/:id') @Delete('public/orders/:id')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'order detail' })
orderDetail(@Param('id') orderId: string, @UserId() userId: string) {
return this.orderService.getOrderAsUser(userId, orderId);
}
@Delete('public/order/:id')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'Delete order ' }) @ApiOperation({ summary: 'Delete order ' })
deleteOrder(@Param('id') orderId: string, @UserId() userId: string) { deleteOrder(@Param('id') orderId: string, @UserId() userId: string) {
@@ -43,13 +37,18 @@ export class OrderController {
@Get('public/orders') @Get('public/orders')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'Get all orders with pagination and filters' }) @ApiOperation({ summary: 'Get all orders with pagination and filters' })
async findAll(@Query() dto: FindOrdersDto, @UserId() userId: string) { findAll(@Query() dto: FindOrdersDto, @UserId() userId: string) {
const orders = await this.orderService.findUserOrders(userId, dto); return this.orderService.findUserOrders(userId, dto);
return orders
} }
@Get('public/orders/:id')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'order detail' })
orderDetail(@Param('id') orderId: string, @UserId() userId: string) {
return this.orderService.getOrderAsUser(userId, orderId);
}
@Patch('public/order/:id/item') @Post('public/orders/:id/item')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'add item to order ' }) @ApiOperation({ summary: 'add item to order ' })
addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) { addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) {
@@ -57,15 +56,15 @@ export class OrderController {
} }
@Patch('public/order/:id/item/:itemId') @Patch('public/orders/:id/item/:itemId')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'Update order item ' }) @ApiOperation({ summary: 'Update order item ' })
updateOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Param(':itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) { updateOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Param('itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) {
return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body); return this.orderService.updateOrderItemAsUser(userId, orderId, +itemId, body);
} }
@Post('public/orders/:orderId/items/:orderItemId/confirm') @Patch('public/orders/:orderId/items/:orderItemId/confirm')
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'Confirm Invoice Item ' }) @ApiOperation({ summary: 'Confirm Invoice Item ' })
confirmOrderItem( confirmOrderItem(
@@ -89,22 +88,31 @@ export class OrderController {
/*========================== Admin Routes =====================*/ /*========================== Admin Routes =====================*/
@Post('admin/order') @Post('admin/orders')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'create order ' }) @ApiOperation({ summary: 'create order ' })
createOrderAsAdmin(@AdminId() adminId: string, @Body() body: CreateOrderAsAdminDto) { createOrderAsAdmin(@AdminId() adminId: string, @Body() body: CreateOrderAsAdminDto) {
return this.orderService.createOrderAsAdmin(adminId, body); return this.orderService.createOrderAsAdmin(adminId, body);
} }
@Get('admin/orders/:id')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'order detail' })
orderDetailAsAdmin(@Param('id') orderId: string) {
return this.orderService.findOrderOrFail(orderId);
}
@Patch('admin/orders/:orderId') @Patch('admin/orders/:orderId')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Update Order ' }) @ApiOperation({ summary: 'Update Order ' })
updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) { updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) {
return this.orderService.updateOrderAsAdmin(orderId, dto); return this.orderService.updateOrderAsAdmin(orderId, dto);
} }
@Delete('admin/orders/:orderId') @Delete('admin/orders/:orderId')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Hard Delete Order ' }) @ApiOperation({ summary: 'Hard Delete Order ' })
hardDelete(@Param('orderId') orderId: string) { hardDelete(@Param('orderId') orderId: string) {
return this.orderService.hardDeleteOrder(orderId); return this.orderService.hardDeleteOrder(orderId);
@@ -112,43 +120,43 @@ export class OrderController {
@Post('admin/orders/:orderId/designer') @Post('admin/orders/:orderId/designer')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Assign Order Designer ' }) @ApiOperation({ summary: 'Assign Order Designer ' })
assignDesigner(@Param('orderId') orderId: string, @Body() body: AssignDesignerDto) { assignDesigner(@Param('orderId') orderId: string, @Body() body: AssignDesignerDto) {
return this.orderService.assignDesigner(orderId, body.designerId); return this.orderService.assignDesigner(orderId, body.designerId);
} }
@Post('admin/orders/:orderId/invoice') @Post('admin/orders/:orderId/invoice')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Create Order invoice ' }) @ApiOperation({ summary: 'Create Order invoice ' })
createInvoice(@Param('orderId') orderId: string) { createInvoice(@Param('orderId') orderId: string) {
return this.orderService.createInvoice(orderId); return this.orderService.createInvoice(orderId);
} }
@Delete('admin/orders/:orderId/items/:orderItemId') @Delete('admin/orders/:id/items/:orderItemId')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Remove order Item ' }) @ApiOperation({ summary: 'Remove order Item ' })
removeOrderItemAdAdmin( removeOrderItemAdAdmin(
@Param('orderId') orderId: string, @Param('id') orderId: string,
@Param('orderItemId') orderItemId: string, @Param('orderItemId') orderItemId: string,
) { ) {
return this.orderService.removeOrderItemAsAdmin(orderId, +orderItemId); return this.orderService.removeOrderItemAsAdmin(orderId, +orderItemId);
} }
@Patch('admin/order/:id/item') @Post('admin/orders/:id/item')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'add item to order ' }) @ApiOperation({ summary: 'add item to order ' })
addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) { addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) {
return this.orderService.addOrderItemAsAdmin(orderId, body); return this.orderService.addOrderItemAsAdmin(orderId, body);
} }
@Patch('admin/order/:id/item/:itemId') @Patch('admin/orders/:id/item/:itemId')
@UseGuards(AuthGuard) @UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Update order item' }) @ApiOperation({ summary: 'Update order item' })
updateOrderAsAdmin(@Param('id') orderId: string, @Param(':itemId') itemId: string, updateOrderItemAsAdmin(@Param('id') orderId: string, @Param('itemId') itemId: string,
@Body() body: UpdateOrderItemDtoAsAdmin) { @Body() body: UpdateOrderItemDtoAsAdmin) {
return this.orderService.updateOrderItemAsAdmin(orderId, itemId, body); return this.orderService.updateOrderItemAsAdmin(orderId, +itemId, body);
} }
} }
+17 -7
View File
@@ -116,7 +116,7 @@ export class OrderService {
return orderItem return orderItem
} }
async updateOrderItemAsUser(userId: string, orderId: string, itemId: string, dto: UpdateOrderItemDtoAsUser) { async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) {
const order = await this.findOrderOrFail(orderId) const order = await this.findOrderOrFail(orderId)
@@ -133,9 +133,9 @@ export class OrderService {
return orderItem return orderItem
} }
async updateOrderItemAsAdmin(orderId: string, itemId: string, dto: UpdateOrderItemDtoAsAdmin) { async updateOrderItemAsAdmin(orderId: string, itemId: number, dto: UpdateOrderItemDtoAsAdmin) {
await this.findOrderOrFail(orderId) await this.findOrderItemOrFail(orderId, itemId)
const orderItem = await this.updateOrderItem(itemId, dto) const orderItem = await this.updateOrderItem(itemId, dto)
@@ -333,7 +333,9 @@ export class OrderService {
const admin = adminId ? await this.adminService.findOrFail(adminId) : undefined const admin = adminId ? await this.adminService.findOrFail(adminId) : undefined
const designer = designerId ? await this.adminService.findOrFail(designerId) : undefined const designer = designerId ? await this.adminService.findOrFail(designerId) : undefined
const products = await this.productService.findProductsByIds(items.map(item => item.productId)) const products = await this.productService.findProductsByIds(items.map(item => item.productId))
const productMap = new Map(
products.map(p => [Number(p.id), p])
);
return this.em.transactional(async (em) => { return this.em.transactional(async (em) => {
@@ -351,7 +353,7 @@ export class OrderService {
em.persist(order) em.persist(order)
for (const item of items) { for (const item of items) {
const product = products.find(p => Number(p.id) === Number(item.productId)) const product = productMap.get(Number(item.productId))
if (!product) { if (!product) {
throw new BadRequestException("Product not found!") throw new BadRequestException("Product not found!")
@@ -421,7 +423,7 @@ export class OrderService {
return order return order
} }
async updateOrderItem(itemId: string, dto: UpdateOrderItem) { async updateOrderItem(itemId: number, dto: UpdateOrderItem) {
const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto
const orderItem = await this.orderItemRepository.findOne({ const orderItem = await this.orderItemRepository.findOne({
@@ -505,12 +507,20 @@ export class OrderService {
async findOrderOrFail(orderId: string) { async findOrderOrFail(orderId: string) {
const order = await this.orderRepository.findOne({ id: orderId }, const order = await this.orderRepository.findOne({ id: orderId },
{ populate: ['items', 'items.product', 'payments'] }) { populate: ['items', 'items.product', 'payments', 'user'] })
if (!order) { if (!order) {
throw new BadRequestException('Order not found') throw new BadRequestException('Order not found')
} }
return order return order
} }
async findOrderItemOrFail(orderId: string, itemId: number) {
const orderItem = await this.orderItemRepository.findOne({ id: itemId, order: { id: orderId } },
{ populate: [] })
if (!orderItem) {
throw new BadRequestException('Order item not found')
}
return orderItem
}
} }