diff --git a/BE/src/stock/order/stock-order.controller.ts b/BE/src/stock/order/stock-order.controller.ts index e7696bb4..82d72083 100644 --- a/BE/src/stock/order/stock-order.controller.ts +++ b/BE/src/stock/order/stock-order.controller.ts @@ -1,6 +1,8 @@ import { Body, Controller, + Delete, + Param, Post, Req, UseGuards, @@ -57,4 +59,22 @@ export class StockOrderController { ) { await this.stockTradeService.sell(request.user.id, stockOrderRequest); } + + @Delete('/:order_id') + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @ApiOperation({ + summary: '주식 매도/매수 취소 API', + description: '주문 id로 미체결된 주문을 취소한다.', + }) + @ApiResponse({ + status: 200, + description: '주식 매도/매수 취소 성공', + }) + async cancel( + @Req() request: RequestInterface, + @Param('order_id') orderId: number, + ) { + await this.stockTradeService.cancel(request.user.id, orderId); + } } diff --git a/BE/src/stock/order/stock-order.service.ts b/BE/src/stock/order/stock-order.service.ts index a5c2cbba..3b5e7e07 100644 --- a/BE/src/stock/order/stock-order.service.ts +++ b/BE/src/stock/order/stock-order.service.ts @@ -1,3 +1,9 @@ +import { + ConflictException, + ForbiddenException, + Injectable, +} from '@nestjs/common'; +import { NotFoundError } from 'rxjs'; import { Injectable } from '@nestjs/common'; import { StockOrderRequestDto } from './dto/stock-order-request.dto'; import { StockOrderRepository } from './stock-order.repository'; @@ -33,4 +39,18 @@ export class StockOrderService { await this.stockOrderRepository.save(order); } + + async cancel(userId: number, orderId: number) { + const order = await this.stockOrderRepository.findOneBy({ id: orderId }); + + if (!order) throw new NotFoundError('주문을 찾을 수 없습니다.'); + + if (order.user_id !== userId) + throw new ForbiddenException('다른 사용자의 주문은 취소할 수 없습니다.'); + + if (order.status === StatusType.COMPLETE) + throw new ConflictException('이미 체결된 주문은 취소할 수 없습니다.'); + + await this.stockOrderRepository.remove(order); + } }