/** * GET /api/stats — compatibilité avec l'ancien front */ const express = require('express'); const router = express.Router(); const { getMarches } = require('../services/baserow'); const { selectVal, parseNum, isCloture, getDelaiRestant, niveauAlerte, DELAI_ATTENTION, } = require('../services/calc'); router.get('/', async (req, res) => { try { const rows = await getMarches(); const actifs = rows.filter(r => !isCloture(r)); const parStatut = {}; for (const r of rows) { const s = selectVal(r.observation) || 'Inconnu'; parStatut[s] = (parStatut[s] || 0) + 1; } const tauxList = actifs.map(r => parseNum(r.taux_phy || r.avt_phy)).filter(v => v > 0); const tauxMoyen = tauxList.length ? Math.round(tauxList.reduce((a, b) => a + b, 0) / tauxList.length * 10) / 10 : 0; const alertes = actifs .map(r => ({ ...r, _delai: getDelaiRestant(r) })) .filter(r => r._delai !== null && r._delai <= DELAI_ATTENTION) .map(r => ({ id: r.id, ref: r.id_marche || r.reference || '', entrepreneur: r.entrepreneur || '', projet: r.projet || '', region: r.region || '', delai_restant: r._delai, niveau: niveauAlerte(r._delai), })); res.json({ total: rows.length, actifs: actifs.length, clotures: rows.length - actifs.length, par_statut: parStatut, taux_avancement_moyen: tauxMoyen, alertes_delais: { count: alertes.length, critique: alertes.filter(a => a.niveau === 'critique').length, attention: alertes.filter(a => a.niveau === 'attention').length, items: alertes, }, }); } catch (err) { res.status(502).json({ error: 'Erreur Baserow', detail: err.message }); } }); module.exports = router;