refactor!: support RUB only (WealthLedger/main#55) (#168)
This commit was merged in pull request #168.
This commit is contained in:
@@ -249,37 +249,23 @@ function toSection(
|
||||
|
||||
function toUnrealizedReturns(
|
||||
positions: Portfolio["positions"],
|
||||
): Money[] {
|
||||
const totals = new Map<string, {
|
||||
complete: boolean;
|
||||
values: Decimal[];
|
||||
}>();
|
||||
): Money | null {
|
||||
const values: Decimal[] = [];
|
||||
|
||||
for (const position of positions) {
|
||||
const currencies = position.absoluteReturn
|
||||
? [position.absoluteReturn.currency]
|
||||
: [...new Set([
|
||||
position.averagePurchasePrice?.currency,
|
||||
position.currentValue?.currency,
|
||||
].filter((currency): currency is string => !!currency))];
|
||||
if (!position.absoluteReturn) {
|
||||
// A position without any valuation has nothing to contribute.
|
||||
|
||||
for (const currency of currencies) {
|
||||
const total = totals.get(currency) ?? { complete: true, values: [] };
|
||||
if (!position.averagePurchasePrice && !position.currentValue)
|
||||
continue;
|
||||
|
||||
if (position.absoluteReturn) {
|
||||
total.values.push(decimalFromString(position.absoluteReturn.value));
|
||||
}
|
||||
else {
|
||||
total.complete = false;
|
||||
}
|
||||
|
||||
totals.set(currency, total);
|
||||
return null;
|
||||
}
|
||||
|
||||
values.push(decimalFromString(position.absoluteReturn.value));
|
||||
}
|
||||
|
||||
return [...totals].flatMap(([currency, total]) => total.complete
|
||||
? [{ currency, value: decimalToString(sumDecimals(total.values)) }]
|
||||
: []);
|
||||
return { currency: "RUB", value: decimalToString(sumDecimals(values)) };
|
||||
}
|
||||
|
||||
export {
|
||||
|
||||
Reference in New Issue
Block a user