refactor(routes): Phase 3.6-10 - standardize 5 utility routes
- credentials.js (2 deps: credentialManager, asyncHandler) - backups.js (2 deps: backupManager, asyncHandler) - license.js (2 deps: licenseManager, asyncHandler) - errorlogs.js (3 deps: ERROR_LOG_FILE, auditLogger, asyncHandler) - themes.js (0 deps! Standalone route) Total: 9 routes refactored so far
This commit is contained in:
@@ -3,17 +3,26 @@ const fs = require('fs');
|
||||
const fsp = require('fs').promises;
|
||||
const { exists } = require('../fs-helpers');
|
||||
const { paginate, parsePaginationParams } = require('../pagination');
|
||||
const { success } = require('../response-helpers');
|
||||
|
||||
module.exports = function(ctx) {
|
||||
/**
|
||||
* Error logs routes factory
|
||||
* @param {Object} deps - Explicit dependencies
|
||||
* @param {string} deps.ERROR_LOG_FILE - Path to error log file
|
||||
* @param {Object} deps.auditLogger - Audit logger instance
|
||||
* @param {Function} deps.asyncHandler - Async route handler wrapper
|
||||
* @returns {express.Router}
|
||||
*/
|
||||
module.exports = function({ ERROR_LOG_FILE, auditLogger, asyncHandler }) {
|
||||
const router = express.Router();
|
||||
|
||||
// Get error logs
|
||||
router.get('/error-logs', ctx.asyncHandler(async (req, res) => {
|
||||
if (!await exists(ctx.ERROR_LOG_FILE)) {
|
||||
return res.json({ success: true, logs: [] });
|
||||
router.get('/error-logs', asyncHandler(async (req, res) => {
|
||||
if (!await exists(ERROR_LOG_FILE)) {
|
||||
return success(res, { logs: [] });
|
||||
}
|
||||
|
||||
const logContent = await fsp.readFile(ctx.ERROR_LOG_FILE, 'utf8');
|
||||
const logContent = await fsp.readFile(ERROR_LOG_FILE, 'utf8');
|
||||
const logEntries = logContent.split('='.repeat(80)).filter(entry => entry.trim());
|
||||
|
||||
const logs = logEntries.map(entry => {
|
||||
@@ -31,37 +40,37 @@ module.exports = function(ctx) {
|
||||
return null;
|
||||
}).filter(Boolean);
|
||||
|
||||
res.json({ success: true, logs: logs.slice(-50).reverse() });
|
||||
success(res, { logs: logs.slice(-50).reverse() });
|
||||
}, 'error-logs-get'));
|
||||
|
||||
// Clear error logs
|
||||
router.delete('/error-logs', ctx.asyncHandler(async (req, res) => {
|
||||
if (await exists(ctx.ERROR_LOG_FILE)) {
|
||||
await fsp.writeFile(ctx.ERROR_LOG_FILE, '');
|
||||
router.delete('/error-logs', asyncHandler(async (req, res) => {
|
||||
if (await exists(ERROR_LOG_FILE)) {
|
||||
await fsp.writeFile(ERROR_LOG_FILE, '');
|
||||
}
|
||||
res.json({ success: true, message: 'Error logs cleared' });
|
||||
success(res, { message: 'Error logs cleared' });
|
||||
}, 'error-logs-clear'));
|
||||
|
||||
// Audit log
|
||||
router.get('/audit-logs', ctx.asyncHandler(async (req, res) => {
|
||||
router.get('/audit-logs', asyncHandler(async (req, res) => {
|
||||
const paginationParams = parsePaginationParams(req.query);
|
||||
const action = req.query.action || '';
|
||||
if (paginationParams) {
|
||||
// When paginating, fetch all matching entries and let pagination slice
|
||||
const entries = await ctx.auditLogger.query({ limit: Number.MAX_SAFE_INTEGER, offset: 0, action });
|
||||
const entries = await auditLogger.query({ limit: Number.MAX_SAFE_INTEGER, offset: 0, action });
|
||||
const result = paginate(entries, paginationParams);
|
||||
res.json({ success: true, entries: result.data, pagination: result.pagination });
|
||||
success(res, { entries: result.data, pagination: result.pagination });
|
||||
} else {
|
||||
const limit = parseInt(req.query.limit) || 50;
|
||||
const offset = parseInt(req.query.offset) || 0;
|
||||
const entries = await ctx.auditLogger.query({ limit, offset, action });
|
||||
res.json({ success: true, entries });
|
||||
const entries = await auditLogger.query({ limit, offset, action });
|
||||
success(res, { entries });
|
||||
}
|
||||
}, 'audit-log'));
|
||||
|
||||
router.delete('/audit-logs', ctx.asyncHandler(async (req, res) => {
|
||||
await ctx.auditLogger.clear();
|
||||
res.json({ success: true, message: 'Audit log cleared' });
|
||||
router.delete('/audit-logs', asyncHandler(async (req, res) => {
|
||||
await auditLogger.clear();
|
||||
success(res, { message: 'Audit log cleared' });
|
||||
}, 'audit-log-clear'));
|
||||
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user