const { getAllEntries, removeAllRecords } = require('../../../core/core'); const { logError, logInfo } = require('../../../infrastructure/logger'); const { trackRequest } = require('../../../maintenance/metrics'); const { broadcast } = require('../websocket'); async function handleEntriesRoutes(req, res) { const urlPath = req.urlPath || new URL(req.url, `https://${req.headers.host}`).pathname; const method = req.method; if (method === 'GET' && urlPath === '/api/entries') { try { const entries = await getAllEntries(); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(entries)); } catch (err) { logError('Admin', `Failed to fetch entries: ${err.message}`); res.writeHead(500); res.end(JSON.stringify({ error: 'Failed to fetch entries' })); } return true; } if (method === 'POST' && urlPath === '/api/remove-all-records') { try { const result = await removeAllRecords(); trackRequest('/api/remove-all-records', true); broadcast({ type: 'update-database' }); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, message: `Removed ${result.removed} records from the network`, removed: result.removed, errors: result.errors })); logInfo('Admin', `Removed all records: ${result.removed} removed, ${result.errors} errors`); } catch (err) { trackRequest('/api/remove-all-records', false); logError('Admin', `Failed to remove all records: ${err.message}`); res.writeHead(500, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Failed to remove all records', message: err.message })); } return true; } return false; } module.exports = { handleEntriesRoutes };