Skip to content

Commit

Permalink
Merge pull request #73 from boostcampwm-2024/feature/api/ordercancel-…
Browse files Browse the repository at this point in the history
…#51#52

[BE] 7.05/7.06 매수/매도 예약 취소 API 구현 #51,#52
  • Loading branch information
sieunie authored Nov 12, 2024
2 parents 6915b77 + 19973fd commit f0d5127
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions BE/src/stock/order/stock-order.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Body,
Controller,
Delete,
Param,
Post,
Req,
UseGuards,
Expand Down Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions BE/src/stock/order/stock-order.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f0d5127

Please sign in to comment.