Spaces:
Running
Running
Upload reports.js
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657const express = require('express');const router = express.Router();const path = require('path');const fs = require('fs');const { authenticateToken } = require('../middleware/authenticateToken');
const REPORTS_DIR = path.join(__dirname, '..', 'data', 'reports');
function appendReport(entry) { if (!fs.existsSync(REPORTS_DIR)) fs.mkdirSync(REPORTS_DIR, { recursive: true }); const date = new Date().toISOString().slice(0, 10); const file = path.join(REPORTS_DIR, `${date}.jsonl`); fs.appendFileSync(file, JSON.stringify(entry) + '\n');}
const CATEGORIES = ['bug', 'account', 'suggestion'];
router.post('/api/reports', authenticateToken, async (req, res) => { try { const category = String(req.body.category || 'bug'); const description = String(req.body.description || '').trim(); const contact = String(req.body.contact || '').trim();
if (!CATEGORIES.includes(category)) { return res.status(400).json({ error: 'Invalid category' }); } if (!description || description.length < 5 || description.length > 4000) { return res.status(400).json({ error: 'Description must be between 5 and 4000 characters' }); } if (contact.length > 500) { return res.status(400).json({ error: 'Contact is too long' }); }
let meta = null; try { meta = { appVersion: String(req.body.appVersion || '').slice(0, 50), userAgent: String(req.headers['user-agent'] || '').slice(0, 500), timestamp: new Date().toISOString(), }; } catch { meta = null; }
appendReport({ username: req.user.username, category, description, contact: contact || null, meta, });
res.json({ success: true }); } catch { res.status(500).json({ error: 'Server error' }); }});
module.exports = router;routes/reports.jsAdded+57