refactor!: support RUB only (WealthLedger/main#55)
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.
This commit is contained in:
@@ -20,71 +20,52 @@ 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 }[],
|
||||
cash: Money[],
|
||||
cashAmounts: Money[],
|
||||
) {
|
||||
const currencies = new Map<
|
||||
string,
|
||||
{
|
||||
hasNegativeValues: boolean;
|
||||
values: ReturnType<typeof decimalFromString>[];
|
||||
}
|
||||
>();
|
||||
const values = categoryTypes.map(() => decimalFromString('0'));
|
||||
let hasNegativeValues = false;
|
||||
const unvalued = new Map<string, number>();
|
||||
const include = (type: string, money: Money) => {
|
||||
const group = currencies.get(money.currency) ?? {
|
||||
values: categoryTypes.map(() => decimalFromString('0')),
|
||||
hasNegativeValues: false,
|
||||
};
|
||||
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.
|
||||
group.values[index] = add(group.values[index], value);
|
||||
group.hasNegativeValues ||= value.value < ZERO_AMOUNT;
|
||||
currencies.set(money.currency, group);
|
||||
values[index] = add(values[index], value);
|
||||
hasNegativeValues ||= value.value < ZERO_AMOUNT;
|
||||
};
|
||||
for (const position of positions) {
|
||||
// CURRENCY positions mirror cash, which is counted once as CASH below.
|
||||
if (position.type === 'CURRENCY') continue;
|
||||
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 cash) include('CASH', amount);
|
||||
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 })),
|
||||
currencies: [...currencies]
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([currency, group]) => {
|
||||
const total = group.values.reduce(add, decimalFromString('0'));
|
||||
const canCalculateShares =
|
||||
!unvalued.size && !group.hasNegativeValues && total.value > ZERO_AMOUNT;
|
||||
const categories = group.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 {
|
||||
currency,
|
||||
totalValue: formatDecimal(total),
|
||||
hasNegativeValues: group.hasNegativeValues,
|
||||
categories,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user