Files
AtreidesAssetsFree/client/wealth/account-cards.js
T

54 lines
2.5 KiB
JavaScript
Raw Normal View History

2026-10-29 11:57:29 +01:00
// Generated, do not edit. sha256:771d7bfccd806caf4ec8847da01e643d018dcebd8d547afd6fc2a566be8ad622
import { ENUMS } from '../../shared/config.js';
import { byKindAndLabel } from '../ui/asset-label.js';
export function accountCards({ kaarten = [], holdings = [], accounts = [], toon = new Set() } = {}) {
const perAsset = new Map(kaarten.map(k => [k.asset.id, k]));
const lijnen = new Map();
const lijn = (accountId, k, saldo) => {
if (!lijnen.has(accountId)) lijnen.set(accountId, []);
lijnen.get(accountId).push({ k, saldo });
};
const opAccount = new Map();
for (const h of holdings) {
const k = perAsset.get(h.assetId);
if (!k || !h.saldo) continue;
lijn(h.accountId, k, h.saldo);
opAccount.set(h.assetId, (opAccount.get(h.assetId) || 0) + h.saldo);
}
for (const k of kaarten) {
const rest = (k.p.net || 0) - (opAccount.get(k.asset.id) || 0);
if (rest) lijn(null, k, rest);
}
for (const id of toon) if (!lijnen.has(id)) lijnen.set(id, []);
const naam = new Map(accounts.map(a => [a.id, a]));
const kaartVan = (accountId, rijen) => {
let value = null, unrealized = null;
const lines = rijen.map(({ k, saldo }) => {
const units = saldo / Math.pow(10, k.asset.decimals);
const v = k.m.fiatPrice != null ? units * k.m.fiatPrice : null;
if (v != null) value = (value ?? 0) + v;
const deel = ongerealiseerdVan(k, accountId, saldo);
if (deel != null) unrealized = (unrealized ?? 0) + deel;
return { asset: k.asset, saldo, value: v, origin: k.m.origin };
}).sort((a, b) => byKindAndLabel(a.asset, b.asset));
return {
accountId, account: naam.get(accountId) || null, value, unrealized, lines,
assetIds: lines.map(l => l.asset.id),
kinds: [...new Set(lines.map(l => l.asset.kind))],
};
};
const ongerealiseerdVan = (k, accountId, saldo) => {
const eigenDeel = k.m.beheerd?.find(d => d.accountId === accountId);
if (eigenDeel) return eigenDeel.unrealized;
const basis = k.m.eigen ?? { net: k.p.net, unrealized: k.m.unrealized };
return basis.unrealized != null && basis.net ? basis.unrealized * (saldo / basis.net) : null;
};
const soort = c => (c.accountId == null ? ENUMS.ACCOUNT_KIND.length + 1
: c.account ? ENUMS.ACCOUNT_KIND.indexOf(c.account.kind) : ENUMS.ACCOUNT_KIND.length);
return [...lijnen].map(([id, rijen]) => kaartVan(id, rijen))
.filter(c => c.lines.length || c.account)
.sort((a, b) => soort(a) - soort(b)
|| String(a.account?.name ?? '').localeCompare(String(b.account?.name ?? ''), 'nl'));
}