feat(tbank): extract mapBrokerPosition, add mapBrokerPositionsPage, add name to operation
This commit is contained in:
parent
953c7c29a6
commit
05f1e792c5
@ -108,6 +108,7 @@ export function mapOperation(item: TBankOperationItem, accountId: string): Broke
|
|||||||
type,
|
type,
|
||||||
category: categorizeOperationType(type),
|
category: categorizeOperationType(type),
|
||||||
description: item.description || item.name || null,
|
description: item.description || item.name || null,
|
||||||
|
name: item.name ?? null,
|
||||||
state: item.state ?? null,
|
state: item.state ?? null,
|
||||||
instrumentUid: item.instrumentUid ?? null,
|
instrumentUid: item.instrumentUid ?? null,
|
||||||
figi: item.figi ?? null,
|
figi: item.figi ?? null,
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import type {
|
|||||||
BrokerMoney,
|
BrokerMoney,
|
||||||
BrokerPortfolio,
|
BrokerPortfolio,
|
||||||
BrokerPosition,
|
BrokerPosition,
|
||||||
|
BrokerPositionsPage,
|
||||||
} from '../types/broker.types';
|
} from '../types/broker.types';
|
||||||
import type {
|
import type {
|
||||||
TBankInstrument,
|
TBankInstrument,
|
||||||
@ -22,10 +23,25 @@ function isBrokerMoney(value: BrokerMoney | null): value is BrokerMoney {
|
|||||||
return value !== null;
|
return value !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mapBrokerPortfolio(input: MapBrokerPortfolioInput): BrokerPortfolio {
|
export function mapBrokerPosition(input: {
|
||||||
const mappedPositions = (input.portfolio.positions ?? []).map<BrokerPosition>((position) => {
|
position: {
|
||||||
const quantity = mapQuotationToNumber(position.quantity);
|
figi?: string;
|
||||||
const currentPrice = mapMoneyValue(position.currentPrice);
|
instrumentUid?: string;
|
||||||
|
positionUid?: string;
|
||||||
|
ticker?: string;
|
||||||
|
classCode?: string;
|
||||||
|
instrumentType?: string;
|
||||||
|
quantity?: { units?: string | number; nano?: number };
|
||||||
|
blockedLots?: { units?: string | number; nano?: number };
|
||||||
|
currentPrice?: { currency?: string; units?: string | number; nano?: number };
|
||||||
|
averagePositionPrice?: { currency?: string; units?: string | number; nano?: number };
|
||||||
|
expectedYield?: { units?: string | number; nano?: number };
|
||||||
|
dailyYield?: { currency?: string; units?: string | number; nano?: number };
|
||||||
|
};
|
||||||
|
instruments: Map<string, Partial<TBankInstrument>>;
|
||||||
|
}): BrokerPosition {
|
||||||
|
const quantity = mapQuotationToNumber(input.position.quantity);
|
||||||
|
const currentPrice = mapMoneyValue(input.position.currentPrice);
|
||||||
const currentValue =
|
const currentValue =
|
||||||
currentPrice && quantity !== null
|
currentPrice && quantity !== null
|
||||||
? {
|
? {
|
||||||
@ -36,28 +52,29 @@ export function mapBrokerPortfolio(input: MapBrokerPortfolioInput): BrokerPortfo
|
|||||||
}
|
}
|
||||||
: null;
|
: null;
|
||||||
const instrument =
|
const instrument =
|
||||||
(position.instrumentUid && input.instruments.get(position.instrumentUid)) ||
|
(input.position.instrumentUid && input.instruments.get(input.position.instrumentUid)) ||
|
||||||
(position.positionUid && input.instruments.get(position.positionUid)) ||
|
(input.position.positionUid && input.instruments.get(input.position.positionUid)) ||
|
||||||
undefined;
|
undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
figi: position.figi ?? null,
|
figi: input.position.figi ?? null,
|
||||||
instrumentUid: position.instrumentUid ?? null,
|
instrumentUid: input.position.instrumentUid ?? null,
|
||||||
positionUid: position.positionUid ?? null,
|
positionUid: input.position.positionUid ?? null,
|
||||||
ticker: position.ticker || instrument?.ticker || null,
|
ticker: input.position.ticker || instrument?.ticker || null,
|
||||||
classCode: position.classCode || instrument?.classCode || null,
|
classCode: input.position.classCode || instrument?.classCode || null,
|
||||||
instrumentType: position.instrumentType || instrument?.instrumentType || null,
|
instrumentType: input.position.instrumentType || instrument?.instrumentType || null,
|
||||||
name: instrument?.name ?? null,
|
name: instrument?.name ?? null,
|
||||||
quantity,
|
quantity,
|
||||||
blockedLots: mapQuotationToNumber(position.blockedLots),
|
blockedLots: mapQuotationToNumber(input.position.blockedLots),
|
||||||
currentPrice,
|
currentPrice,
|
||||||
currentValue,
|
currentValue,
|
||||||
averagePositionPrice: mapMoneyValue(position.averagePositionPrice),
|
averagePositionPrice: mapMoneyValue(input.position.averagePositionPrice),
|
||||||
expectedYieldPercent: mapQuotationToNumber(position.expectedYield),
|
expectedYieldPercent: mapQuotationToNumber(input.position.expectedYield),
|
||||||
dailyYield: mapMoneyValue(position.dailyYield),
|
dailyYield: mapMoneyValue(input.position.dailyYield),
|
||||||
};
|
};
|
||||||
});
|
}
|
||||||
|
|
||||||
|
export function mapBrokerPortfolio(input: MapBrokerPortfolioInput): BrokerPortfolio {
|
||||||
return {
|
return {
|
||||||
account: input.account,
|
account: input.account,
|
||||||
totals: {
|
totals: {
|
||||||
@ -78,7 +95,36 @@ export function mapBrokerPortfolio(input: MapBrokerPortfolioInput): BrokerPortfo
|
|||||||
},
|
},
|
||||||
cash: (input.positions.money ?? []).map(mapMoneyValue).filter(isBrokerMoney),
|
cash: (input.positions.money ?? []).map(mapMoneyValue).filter(isBrokerMoney),
|
||||||
blockedCash: (input.positions.blocked ?? []).map(mapMoneyValue).filter(isBrokerMoney),
|
blockedCash: (input.positions.blocked ?? []).map(mapMoneyValue).filter(isBrokerMoney),
|
||||||
positions: mappedPositions,
|
asOf: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mapBrokerPositionsPage(input: {
|
||||||
|
accountId: string;
|
||||||
|
portfolio: TBankPortfolioResponse;
|
||||||
|
instruments: Map<string, Partial<TBankInstrument>>;
|
||||||
|
cursor?: string;
|
||||||
|
limit: number;
|
||||||
|
}): BrokerPositionsPage {
|
||||||
|
const allPositions = (input.portfolio.positions ?? []).map((position) =>
|
||||||
|
mapBrokerPosition({ position, instruments: input.instruments }),
|
||||||
|
);
|
||||||
|
|
||||||
|
let startIndex = 0;
|
||||||
|
if (input.cursor) {
|
||||||
|
const found = allPositions.findIndex((p) => p.positionUid === input.cursor);
|
||||||
|
startIndex = found >= 0 ? found + 1 : allPositions.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageItems = allPositions.slice(startIndex, startIndex + input.limit);
|
||||||
|
const hasNext = startIndex + input.limit < allPositions.length;
|
||||||
|
const nextCursor = hasNext ? (pageItems[pageItems.length - 1]?.positionUid ?? null) : null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
accountId: input.accountId,
|
||||||
|
items: pageItems,
|
||||||
|
nextCursor,
|
||||||
|
hasNext,
|
||||||
asOf: new Date().toISOString(),
|
asOf: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user