feat(tbank): add getPositions() method to BrokerPortfolioService
This commit is contained in:
parent
05f1e792c5
commit
bb7fef8deb
@ -1,8 +1,8 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
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 type { BrokerPortfolio } from '../types/broker.types';
|
||||
import type { BrokerPortfolio, BrokerPositionsPage } from '../types/broker.types';
|
||||
import type {
|
||||
TBankInstrument,
|
||||
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(
|
||||
portfolio: TBankPortfolioResponse,
|
||||
): Promise<Map<string, Partial<TBankInstrument>>> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user