Files
2026-10-29 11:57:29 +01:00

100 lines
3.5 KiB
JavaScript

// Generated, do not edit. sha256:0ea473364503a4f8549c8bea4bc98c04b7236550cd8e91a8cd91945e7728d55a
import * as db from '../db.js';
import { CONFIG, ENUMS } from '../../shared/config.js';
import { nowUtcIso } from '../../shared/time.js';
const NIET_MELDEN = new Set(['updatedAt', 'lastStatus', 'lastCheckedAt', 'balanceSats', 'lastScannedAt']);
let herkomst = { origin: 'manual', originId: null };
export function withOrigin(origin, originId, fn) {
if (!ENUMS.CHANGE_ORIGIN.includes(origin)) throw new Error(`Unknown origin: ${origin}.`);
const vorige = herkomst;
herkomst = { origin, originId: originId ?? null };
try { return fn(); } finally { herkomst = vorige; }
}
export function currentOrigin() { return { ...herkomst }; }
const meedoet = (tabel) => CONFIG.DB_HISTORY.TRACKED.includes(tabel);
const leesRij = (tabel, id) => db.one(`SELECT * FROM ${tabel} WHERE id = ?`, [id]) || null;
function verschil(voor, na) {
if (!voor || !na) return null;
const uit = {};
for (const veld of Object.keys(voor)) {
if (NIET_MELDEN.has(veld)) continue;
if (voor[veld] !== na[veld]) uit[veld] = [voor[veld], na[veld]];
}
return Object.keys(uit).length ? uit : null;
}
function schrijf(tabel, rowId, operation, changes) {
db.run(`INSERT INTO ChangeLog (tableName, rowId, operation, origin, originId, changes, occurredAt)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[tabel, rowId, operation, herkomst.origin, herkomst.originId, JSON.stringify(changes), nowUtcIso()]);
}
export function trackUpdate(tabel, id, doe) {
if (!meedoet(tabel)) return doe();
const voor = leesRij(tabel, id);
const uit = doe();
const na = leesRij(tabel, id);
const changes = verschil(voor, na);
if (changes) {
db.run(`UPDATE ${tabel} SET updatedAt = ? WHERE id = ?`, [nowUtcIso(), id]);
schrijf(tabel, id, 'update', changes);
}
return uit;
}
export function trackUpdateField(tabel, id, veld, doe) {
if (!meedoet(tabel)) return doe();
const voor = leesRij(tabel, id);
const uit = doe();
const na = leesRij(tabel, id);
if (voor && na && voor[veld] !== na[veld]) schrijf(tabel, id, 'update', { [veld]: [voor[veld], na[veld]] });
return uit;
}
export function trackDelete(tabel, id, doe) {
if (!meedoet(tabel)) return doe();
const rij = leesRij(tabel, id);
const uit = doe();
if (rij) schrijf(tabel, id, 'delete', rij);
return uit;
}
export function trackUpdateMany(tabel, ids, doe) {
if (!meedoet(tabel) || !ids.length) return doe();
const voor = new Map(ids.map(id => [id, leesRij(tabel, id)]));
const uit = doe();
for (const id of ids) {
const changes = verschil(voor.get(id), leesRij(tabel, id));
if (!changes) continue;
db.run(`UPDATE ${tabel} SET updatedAt = ? WHERE id = ?`, [nowUtcIso(), id]);
schrijf(tabel, id, 'update', changes);
}
return uit;
}
export function trackUpdateWhere(tabel, waar, params, doe) {
if (!meedoet(tabel)) return doe();
const ids = db.all(`SELECT id FROM ${tabel} WHERE ${waar}`, params).map(r => r.id);
return trackUpdateMany(tabel, ids, doe);
}
export function trackDeleteWhere(tabel, waar, params, doe) {
if (!meedoet(tabel)) return doe();
const rijen = db.all(`SELECT * FROM ${tabel} WHERE ${waar}`, params);
const uit = doe();
for (const rij of rijen) schrijf(tabel, rij.id, 'delete', rij);
return uit;
}
export function rowHistory(tabel, rowId) {
return db.all(`SELECT id, operation, origin, originId, changes, occurredAt
FROM ChangeLog WHERE tableName = ? AND rowId = ? ORDER BY id DESC`, [tabel, rowId])
.map(r => ({ ...r, changes: JSON.parse(r.changes) }));
}