All files / src/routes admin-log-config.js

88.88% Statements 32/36
91.66% Branches 11/12
100% Functions 3/3
88.23% Lines 30/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                1x 1x 1x 1x   1x                           1x                 1x 3x 3x 2x 2x   1x 1x         1x 2x   1x 1x 1x               1x 4x   3x 3x 3x 15x 2x       3x 2x     1x 1x             1x  
/**
 * Log configuration endpoints — public + admin.
 *
 * GET /log-config              → Public: read log config (mobile clients)
 * GET /admin/log-config        → Admin: read log config
 * PATCH /admin/log-config      → Admin: update log config
 */
 
const router = require('express').Router();
const { db } = require('../utils/firebase');
const { requireAdmin } = require('../middleware/auth');
const log = require('../utils/log');
 
const DEFAULT_CONFIG = {
  retentionHours: 48,
  levelPerSource: {
    'express-api': 'INFO',
    android: 'INFO',
    ios: 'INFO',
    'admin-panel': 'INFO',
    'landing-page': 'WARN',
  },
  excludedRoutes: [],
  hardCapDaily: 15000,
  batchSettings: { intervalSeconds: 30, wifiOnly: false },
};
 
const ALLOWED_FIELDS = [
  'retentionHours',
  'levelPerSource',
  'excludedRoutes',
  'hardCapDaily',
  'batchSettings',
];
 
// GET /log-config — Public (no admin guard), for mobile clients
router.get('/log-config', async (req, res) => {
  try {
    const doc = await db.doc('logConfig/settings').get();
    res.set('Cache-Control', 'public, max-age=300');
    res.json(doc.exists ? doc.data() : DEFAULT_CONFIG);
  } catch (err) {
    log.error('admin-log-config', 'Error reading log config', { error: err.message });
    res.json(DEFAULT_CONFIG);
  }
});
 
// GET /admin/log-config — Admin only
router.get('/admin/log-config', async (req, res) => {
  if (requireAdmin(req, res)) return;
 
  try {
    const doc = await db.doc('logConfig/settings').get();
    res.json(doc.exists ? doc.data() : DEFAULT_CONFIG);
  } catch (err) {
    log.error('admin-log-config', 'Error reading log config', { error: err.message });
    res.json(DEFAULT_CONFIG);
  }
});
 
// PATCH /admin/log-config — Admin only, update settings
router.patch('/admin/log-config', async (req, res) => {
  if (requireAdmin(req, res)) return;
 
  try {
    const updates = {};
    for (const field of ALLOWED_FIELDS) {
      if (req.body[field] !== undefined) {
        updates[field] = req.body[field];
      }
    }
 
    if (Object.keys(updates).length === 0) {
      return res.status(400).json({ error: 'No valid fields provided' });
    }
 
    await db.doc('logConfig/settings').set(updates, { merge: true });
    res.json({ success: true });
  } catch (err) {
    log.error('admin-log-config', 'Error updating log config', { error: err.message });
    res.status(500).json({ error: 'Internal server error' });
  }
});
 
module.exports = router;