Filter non-RUB data at ingest, collapse per-currency collections into single RUB Money, drop currency from TbankPortfolioValuePoint, remove NON_RUB_ACTIVITY and screener currency filter. BREAKING CHANGE: per-currency API fields replaced with single RUB Money; stock screener currency param removed.
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import type { Money } from '#src/common/money.types';
|
|
|
|
import {
|
|
add,
|
|
DECIMAL_RADIX,
|
|
decimalFromString,
|
|
formatDecimal,
|
|
} from '#src/common/decimal';
|
|
|
|
const ZERO_AMOUNT = 0n;
|
|
const INITIAL_UNVALUED_COUNT = 0;
|
|
const POSITION_INCREMENT = 1;
|
|
const ROUNDING_DIVISOR = 2n;
|
|
|
|
const SHARE_PERCENT_DECIMAL_PLACES = 6;
|
|
const PERCENT_SCALED_FACTOR = 100_000_000n;
|
|
|
|
const categoryTypes = ['STOCK', 'BOND', 'OTHER', 'CASH'] as const;
|
|
|
|
// Build from positions and cash, never from totals (which already include cash).
|
|
export function portfolioStructure(
|
|
positions: { currentValue: Money | null; type: string }[],
|
|
cashAmounts: Money[],
|
|
) {
|
|
const values = categoryTypes.map(() => decimalFromString('0'));
|
|
let hasNegativeValues = false;
|
|
const unvalued = new Map<string, number>();
|
|
const include = (type: string, money: Money) => {
|
|
const index = categoryTypes.findIndex((category) => category === type);
|
|
const value = decimalFromString(money.value);
|
|
// eslint-disable-next-line security/detect-object-injection -- The numeric index belongs to the fixed portfolio category array.
|
|
values[index] = add(values[index], value);
|
|
hasNegativeValues ||= value.value < ZERO_AMOUNT;
|
|
};
|
|
for (const position of positions) {
|
|
const type = ['BOND', 'STOCK'].includes(position.type)
|
|
? position.type
|
|
: 'OTHER';
|
|
if (position.currentValue) include(type, position.currentValue);
|
|
else unvalued.set(type, (unvalued.get(type) ?? INITIAL_UNVALUED_COUNT) + POSITION_INCREMENT);
|
|
}
|
|
for (const amount of cashAmounts) include('CASH', amount);
|
|
|
|
const total = values.reduce(add, decimalFromString('0'));
|
|
const canCalculateShares =
|
|
!unvalued.size && !hasNegativeValues && total.value > ZERO_AMOUNT;
|
|
const categories = values.map((value, index) => ({
|
|
// eslint-disable-next-line security/detect-object-injection -- The numeric index belongs to the fixed portfolio category array.
|
|
type: categoryTypes[index],
|
|
value: formatDecimal(value),
|
|
// Rounded to six decimal places using integer arithmetic. Only the
|
|
// resulting dimensionless percentage is passed to chart geometry.
|
|
sharePercent: canCalculateShares
|
|
? formatDecimal({
|
|
value:
|
|
(value.value *
|
|
DECIMAL_RADIX ** BigInt(total.scale - value.scale) *
|
|
PERCENT_SCALED_FACTOR +
|
|
total.value / ROUNDING_DIVISOR) /
|
|
total.value,
|
|
scale: SHARE_PERCENT_DECIMAL_PLACES,
|
|
})
|
|
: null,
|
|
}));
|
|
return {
|
|
totalValue: formatDecimal(total),
|
|
hasNegativeValues,
|
|
categories,
|
|
unvaluedCategories: [...unvalued].map(([type, count]) => ({ type, count })),
|
|
};
|
|
}
|