import { Controller, Get, Param, Post, Query } from '@nestjs/common'; import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; import { ApiResponse } from '../../common/dto/api-response.dto'; import { Roles } from '../auth/decorators/roles.decorator'; import { BrokerAccountsEnvelopeDto, BrokerOperationSyncEnvelopeDto, BrokerOperationsEnvelopeDto, BrokerPortfolioEnvelopeDto, } from './dto/broker-envelope.dto'; import { BrokerOperationQueryDto } from './dto/broker-operation-query.dto'; import { BrokerOperationSyncQueryDto } from './dto/broker-operation-sync-query.dto'; import { BrokerAccountsService } from './services/broker-accounts.service'; import { BrokerOperationSyncService } from './services/broker-operation-sync.service'; import { BrokerOperationsService } from './services/broker-operations.service'; import { BrokerPortfolioService } from './services/broker-portfolio.service'; @ApiTags('Broker') @ApiBearerAuth() @Roles('user') @Controller('broker') export class TBankController { constructor( private readonly brokerAccountsService: BrokerAccountsService, private readonly brokerPortfolioService: BrokerPortfolioService, private readonly brokerOperationsService: BrokerOperationsService, private readonly brokerOperationSyncService: BrokerOperationSyncService, ) {} @Get('accounts') @ApiOperation({ summary: 'Get open T-Bank brokerage and IIS accounts' }) @ApiOkResponse({ type: BrokerAccountsEnvelopeDto }) async getAccounts() { const result = await this.brokerAccountsService.findAll(); return new ApiResponse(result.data, result.meta.fromCache, result.meta.cachedAt); } @Get('accounts/:accountId/portfolio') @ApiOperation({ summary: 'Get T-Bank broker account portfolio with cash and positions' }) @ApiOkResponse({ type: BrokerPortfolioEnvelopeDto }) async getPortfolio(@Param('accountId') accountId: string) { const result = await this.brokerPortfolioService.getPortfolio(accountId); return new ApiResponse(result.data, result.meta.fromCache, result.meta.cachedAt); } @Get('accounts/:accountId/operations') @ApiOperation({ summary: 'Get paginated T-Bank broker account operations' }) @ApiOkResponse({ type: BrokerOperationsEnvelopeDto }) async getOperations( @Param('accountId') accountId: string, @Query() query: BrokerOperationQueryDto, ) { const result = await this.brokerOperationsService.getOperations(accountId, query); return new ApiResponse(result.data, result.meta.fromCache, result.meta.cachedAt); } @Post('accounts/:accountId/operations/sync') @ApiOperation({ summary: 'Synchronize T-Bank broker account operations into local history' }) @ApiOkResponse({ type: BrokerOperationSyncEnvelopeDto }) async syncOperations( @Param('accountId') accountId: string, @Query() query: BrokerOperationSyncQueryDto, ) { const result = await this.brokerOperationSyncService.syncAccount(accountId, query); return new ApiResponse(result); } }