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, BrokerAnalyticsEnvelopeDto, BrokerEventsEnvelopeDto, BrokerOperationSyncEnvelopeDto, BrokerOperationsEnvelopeDto, BrokerPortfolioEnvelopeDto, BrokerPositionsEnvelopeDto, } from './dto/broker-envelope.dto'; import { BrokerEventsQueryDto } from './dto/broker-events-query.dto'; import { BrokerPositionQueryDto } from './dto/broker-position-query.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 { BrokerEventsService } from './services/broker-events.service'; import { BrokerOperationSyncService } from './services/broker-operation-sync.service'; import { BrokerOperationsService } from './services/broker-operations.service'; import { BrokerAnalyticsService } from './services/broker-analytics.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 brokerEventsService: BrokerEventsService, private readonly brokerOperationsService: BrokerOperationsService, private readonly brokerOperationSyncService: BrokerOperationSyncService, private readonly brokerAnalyticsService: BrokerAnalyticsService, ) {} @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/positions') @ApiOperation({ summary: 'Get paginated T-Bank broker account positions' }) @ApiOkResponse({ type: BrokerPositionsEnvelopeDto }) async getPositions( @Param('accountId') accountId: string, @Query() query: BrokerPositionQueryDto, ) { const result = await this.brokerPortfolioService.getPositions( accountId, query.cursor, query.limit, query.type, ); 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); } @Get('accounts/:accountId/events') @ApiOperation({ summary: 'Get broker account calendar events and cashflow' }) @ApiOkResponse({ type: BrokerEventsEnvelopeDto }) async getEvents(@Param('accountId') accountId: string, @Query() query: BrokerEventsQueryDto) { const result = await this.brokerEventsService.getEvents(accountId, query); return new ApiResponse(result.data, result.meta.fromCache, result.meta.cachedAt); } @Get('accounts/:accountId/analytics') @ApiOperation({ summary: 'Get broker account profitability analytics' }) @ApiOkResponse({ type: BrokerAnalyticsEnvelopeDto }) async getAnalytics(@Param('accountId') accountId: string) { const result = await this.brokerAnalyticsService.getAnalytics(accountId); 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); } }