Files
p2ns/includes/admin/routes/entries.js
T
2025-12-17 20:05:50 -05:00

58 lines
2.0 KiB
JavaScript

const { getAllEntries, removeAllRecords } = require('../../core/core');
const { logError, logInfo } = require('../../infrastructure/logger');
const { trackRequest } = require('../../maintenance/metrics');
// Get broadcast function if available
let broadcast;
try {
broadcast = require('../admin-backend/websocket').broadcast;
} catch (e) {
// Fallback if websocket module not available
broadcast = () => {};
}
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 };