Files

93 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2026-10-29 11:57:29 +01:00
// Generated, do not edit. sha256:8017fede3aae4d6eb73088d3b73b43810357285ec3b8acd0f4f14e52251b7e6a
import * as db from '../db.js';
function tel(sql, params) {
const r = db.one(sql, params);
return r ? Number(r.n) : 0;
}
function idLijst(sql, kolom, params) {
return db.all(sql, params).map(r => r[kolom]);
}
export function accountStructure(accountId) {
const row = db.one(`SELECT * FROM Account WHERE id = ?`, [accountId]);
if (!row) return null;
const beideKanten = [accountId, accountId];
return {
row,
counts: {
events: tel(`SELECT COUNT(*) AS n FROM Event
WHERE fromAccountId = ? OR toAccountId = ?`, beideKanten),
assets: tel(`SELECT COUNT(DISTINCT assetId) AS n FROM Event
WHERE fromAccountId = ? OR toAccountId = ?`, beideKanten),
walletSources: tel(`SELECT COUNT(*) AS n FROM WalletSource WHERE accountId = ?`, [accountId]),
addresses: tel(`SELECT COUNT(*) AS n FROM WalletAddress WHERE accountId = ?`, [accountId]),
walletTransactions: tel(`SELECT COUNT(*) AS n FROM WalletTransaction WHERE accountId = ?`, [accountId]),
valuations: tel(`SELECT COUNT(*) AS n FROM Valuation WHERE accountId = ?`, [accountId]),
},
refs: {
assetIds: idLijst(`SELECT DISTINCT assetId FROM Event
WHERE fromAccountId = ? OR toAccountId = ?
ORDER BY assetId`, 'assetId', beideKanten),
},
};
}
export function assetStructure(assetId) {
const row = db.one(`SELECT * FROM Asset WHERE id = ?`, [assetId]);
if (!row) return null;
const REKENINGEN = `SELECT fromAccountId AS a FROM Event WHERE assetId = ? AND fromAccountId IS NOT NULL
UNION
SELECT toAccountId AS a FROM Event WHERE assetId = ? AND toAccountId IS NOT NULL`;
const beideKolommen = [assetId, assetId];
return {
row,
counts: {
events: tel(`SELECT COUNT(*) AS n FROM Event WHERE assetId = ?`, [assetId]),
accounts: tel(`SELECT COUNT(*) AS n FROM (${REKENINGEN})`, beideKolommen),
pricePoints: tel(`SELECT COUNT(*) AS n FROM PriceHistory WHERE assetId = ?`, [assetId]),
valuations: tel(`SELECT COUNT(*) AS n FROM Valuation WHERE assetId = ?`, [assetId]),
products: tel(`SELECT COUNT(*) AS n FROM MetalProduct WHERE assetId = ?`, [assetId]),
coverage: tel(`SELECT COUNT(*) AS n FROM ProviderCoverage
WHERE symbol = (SELECT symbol FROM Asset WHERE id = ?)`, [assetId]),
},
refs: {
accountIds: idLijst(`SELECT a FROM (${REKENINGEN}) ORDER BY a`, 'a', beideKolommen),
},
};
}
export function eventRefs({ accountId = null, assetId = null } = {}, limiet = 50) {
if (accountId == null && assetId == null) {
throw new Error('eventRefs needs an account or an asset; without a filter this is a bulk read.');
}
const n = Number.isInteger(limiet) && limiet > 0 ? limiet : 50;
const waar = [];
const params = [];
if (accountId != null) {
waar.push('(fromAccountId = ? OR toAccountId = ?)');
params.push(accountId, accountId);
}
if (assetId != null) {
waar.push('assetId = ?');
params.push(assetId);
}
return db.all(
`SELECT id, type, substr(occurredAt, 1, 7) AS month FROM Event
WHERE ${waar.join(' AND ')} ORDER BY occurredAt DESC, id DESC LIMIT ?`,
[...params, n],
).map(r => ({ id: r.id, type: r.type, month: r.month }));
}
export function labelStructure(labelId) {
const row = db.one(`SELECT * FROM Label WHERE id = ?`, [labelId]);
if (!row) return null;
return {
row,
counts: {
walletTransactions: tel(`SELECT COUNT(*) AS n FROM WalletTransaction WHERE labelId = ?`, [labelId]),
},
refs: {},
};
}