codex/broker-operations-ui #18

Merged
ksv741 merged 22 commits from codex/broker-operations-ui into main 2026-06-18 06:34:55 +03:00
Showing only changes of commit bb7fef8deb - Show all commits

View File

@ -1,8 +1,8 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, NotFoundException } from '@nestjs/common';
import { CacheService } from '../../cache/cache.service'; import { CacheService } from '../../cache/cache.service';
import { mapBrokerPortfolio } from '../mappers/portfolio.mapper'; import { mapBrokerPortfolio, mapBrokerPositionsPage } from '../mappers/portfolio.mapper';
import { TBANK_CACHE_KEYS } from '../tbank.config'; import { TBANK_CACHE_KEYS } from '../tbank.config';
import type { BrokerPortfolio } from '../types/broker.types'; import type { BrokerPortfolio, BrokerPositionsPage } from '../types/broker.types';
import type { import type {
TBankInstrument, TBankInstrument,
TBankPortfolioResponse, TBankPortfolioResponse,
@ -62,6 +62,49 @@ export class BrokerPortfolioService {
}; };
} }
async getPositions(
accountId: string,
cursor?: string,
limit = 10,
): Promise<{
data: BrokerPositionsPage;
meta: { fromCache: boolean; cachedAt: string | null };
}> {
const account = await this.accountsService.findById(accountId);
if (!account) throw new NotFoundException('Broker account not found');
const result = await this.cacheService.getOrFetch(
TBANK_CACHE_KEYS.positions,
[accountId],
async () => {
const operationsClient = this.tbankClient.getServiceClient('OperationsService') as any;
const portfolio = await this.tbankClient.callUnary<
{ accountId: string; currency: string },
TBankPortfolioResponse
>('OperationsService/GetPortfolio', operationsClient.getPortfolio.bind(operationsClient), {
accountId,
currency: 'RUB',
});
const instrumentMap = await this.buildInstrumentMap(portfolio);
return mapBrokerPositionsPage({
accountId,
portfolio,
instruments: instrumentMap,
cursor,
limit,
});
},
'tbankPositionsTtl',
);
return {
data: result.data,
meta: { fromCache: result.fromCache, cachedAt: result.cachedAt },
};
}
private async buildInstrumentMap( private async buildInstrumentMap(
portfolio: TBankPortfolioResponse, portfolio: TBankPortfolioResponse,
): Promise<Map<string, Partial<TBankInstrument>>> { ): Promise<Map<string, Partial<TBankInstrument>>> {