moex-vibe/apps/backend/src/modules/tbank/tbank.controller.ts

101 lines
4.1 KiB
TypeScript

import { Controller, Get, Param, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
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() {
return this.brokerAccountsService.findAll();
}
@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) {
return this.brokerPortfolioService.getPortfolio(accountId);
}
@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,
) {
return this.brokerPortfolioService.getPositions(
accountId,
query.cursor,
query.limit,
query.type,
);
}
@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,
) {
return this.brokerOperationsService.getOperations(accountId, query);
}
@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) {
return this.brokerEventsService.getEvents(accountId, query);
}
@Get('accounts/:accountId/analytics')
@ApiOperation({ summary: 'Get broker account profitability analytics' })
@ApiOkResponse({ type: BrokerAnalyticsEnvelopeDto })
async getAnalytics(@Param('accountId') accountId: string) {
return this.brokerAnalyticsService.getAnalytics(accountId);
}
@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,
) {
return this.brokerOperationSyncService.syncAccount(accountId, query);
}
}