feat: aggiunta tracciatura delle modifiche effettuate

This commit is contained in:
2026-07-07 21:51:58 +02:00
parent 3f6d2cc3a8
commit bf8b4c387e
10 changed files with 914 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
/**
* activityDb.js
* Local SQLite activity log database.
* Opens (or creates) internal.db and provides logAttivita() for logging
* every significant action sent to OTRS.
*/
const Database = require('better-sqlite3');
const path = require('path');
const crypto = require('crypto');
const DB_PATH = path.join(__dirname, 'internal.db');
const db = new Database(DB_PATH);
// Ensure WAL mode for better concurrent access
db.pragma('journal_mode = WAL');
// Create table if it does not exist
db.exec(`
CREATE TABLE IF NOT EXISTS attivita (
id TEXT PRIMARY KEY,
agente_id INTEGER NOT NULL DEFAULT 0,
agente_nome TEXT NOT NULL DEFAULT '',
titolo_azione TEXT NOT NULL,
azione TEXT NOT NULL DEFAULT '{}',
esito TEXT NOT NULL DEFAULT 'successo',
creato_il DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
)
`);
/**
* Generate a UUID v7 (time-ordered).
*/
function uuidV7() {
const tsMs = BigInt(Date.now());
const tsMsHex = tsMs.toString(16).padStart(12, '0');
const rand = crypto.randomBytes(10).toString('hex');
const p1 = tsMsHex.slice(0, 8);
const p2 = tsMsHex.slice(8, 12);
const p3 = '7' + rand.slice(0, 3);
const p4 = ((parseInt(rand.slice(3, 4), 16) & 0x3) | 0x8).toString(16) + rand.slice(4, 7);
const p5 = rand.slice(7, 19);
return `${p1}-${p2}-${p3}-${p4}-${p5}`;
}
const insertStmt = db.prepare(`
INSERT INTO attivita (id, agente_id, agente_nome, titolo_azione, azione, esito)
VALUES (?, ?, ?, ?, ?, ?)
`);
/**
* Log an activity record.
* @param {Object} params
*/
function logAttivita({ agente_id = 0, agente_nome = '', titolo_azione, azione = {}, esito = 'successo' }) {
try {
const id = uuidV7();
const azioneStr = typeof azione === 'string' ? azione : JSON.stringify(azione, null, 2);
insertStmt.run(id, agente_id, agente_nome, titolo_azione, azioneStr, esito);
} catch (err) {
console.error('[activityDb] Failed to log activity:', err.message);
}
}
module.exports = { db, logAttivita };