From 2298435c4c013ef9fee379d5783237c25510b4e7 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 11 Nov 2024 16:05:40 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=EC=A3=BC=EC=8B=9D=20?= =?UTF-8?q?=EB=A7=A4=EB=8F=84/=EB=A7=A4=EC=88=98=20=EC=B7=A8=EC=86=8C=20AP?= =?UTF-8?q?I=20=EA=B5=AC=ED=98=84=20#51=20#52?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/stock/order/stock-order.controller.ts | 20 +++++++++++++++++++ BE/src/stock/order/stock-order.service.ts | 21 +++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) 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..997de306 100644 --- a/BE/src/stock/order/stock-order.service.ts +++ b/BE/src/stock/order/stock-order.service.ts @@ -1,4 +1,9 @@ -import { Injectable } from '@nestjs/common'; +import { + ConflictException, + ForbiddenException, + Injectable, +} from '@nestjs/common'; +import { NotFoundError } from 'rxjs'; import { StockOrderRequestDto } from './dto/stock-order-request.dto'; import { StockOrderRepository } from './stock-order.repository'; import { TradeType } from './enum/trade-type'; @@ -33,4 +38,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); + } }