feat: RLA API v1.0.0 — API complète + exports + thème McKinsey
- 9 endpoints métier : synthese, alertes, en-service, en-cours, par-region, clotures, pilotage-proactif, matrice-risque - Exports PDF (tous rôles) / XLSX PPTX DOCX (superadmin) - services/calc.js : helpers normalisés partagés - services/export-pdf.js : PDF async PDFKit par vue - Thème McKinsey (#1C2B4B / bleu pétrole / gris sobre) - Boutons XLSX/DOCX front (superadmin uniquement) - BASEROW_API_URL → https://baserow.bolbol.tn/api/ - dotenv override: true Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* GET /api/synthese
|
||||
* Vue synthèse globale : KPIs, répartitions, alertes, projections
|
||||
*/
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { getMarches } = require('../services/baserow');
|
||||
const {
|
||||
parseNum, formatMontant, isCloture,
|
||||
getDelaiRestant, niveauAlerte, niveauAvancement,
|
||||
DELAI_CRITIQUE, DELAI_ATTENTION,
|
||||
} = require('../services/calc');
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const { region, nature, entrepreneur, projet } = req.query;
|
||||
const regionFilter = req.regionFilter; // set by filterByRegion middleware
|
||||
|
||||
let rows = await getMarches();
|
||||
|
||||
// Filtres
|
||||
if (regionFilter) rows = rows.filter(r => r.region === regionFilter);
|
||||
else if (region) rows = rows.filter(r => r.region === region);
|
||||
if (nature) rows = rows.filter(r => String(r.nature || '').toLowerCase().includes(nature.toLowerCase()));
|
||||
if (entrepreneur) rows = rows.filter(r => String(r.entrepreneur || '').toLowerCase().includes(entrepreneur.toLowerCase()));
|
||||
if (projet) rows = rows.filter(r => String(r.projet || '').toLowerCase().includes(projet.toLowerCase()));
|
||||
|
||||
const actifs = rows.filter(r => !isCloture(r));
|
||||
const clotures = rows.filter(r => isCloture(r));
|
||||
|
||||
// Montants
|
||||
const totalBudget = actifs.reduce((s, r) => s + parseNum(r.tot_marche ?? r.totmarche ?? r.montant), 0);
|
||||
const totalConsomme = actifs.reduce((s, r) => s + parseNum(r.consomme ?? r.montant_consomme ?? 0), 0);
|
||||
|
||||
// Avancement moyen physique
|
||||
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;
|
||||
|
||||
// Par statut
|
||||
const parStatut = {};
|
||||
for (const r of rows) {
|
||||
const s = String(r.statut || 'Inconnu');
|
||||
parStatut[s] = (parStatut[s] || 0) + 1;
|
||||
}
|
||||
|
||||
// Par région
|
||||
const parRegion = {};
|
||||
for (const r of actifs) {
|
||||
const reg = r.region || 'Inconnu';
|
||||
if (!parRegion[reg]) parRegion[reg] = { count: 0, taux_sum: 0, taux_count: 0 };
|
||||
parRegion[reg].count++;
|
||||
const t = parseNum(r.taux_phy ?? r.avt_phy);
|
||||
if (t > 0) { parRegion[reg].taux_sum += t; parRegion[reg].taux_count++; }
|
||||
}
|
||||
for (const reg of Object.keys(parRegion)) {
|
||||
const d = parRegion[reg];
|
||||
parRegion[reg].taux_moyen = d.taux_count ? Math.round(d.taux_sum / d.taux_count * 10) / 10 : 0;
|
||||
}
|
||||
|
||||
// Par nature (CAPEX/OPEX)
|
||||
const parNature = {};
|
||||
for (const r of actifs) {
|
||||
const n = r.nature || 'Non défini';
|
||||
parNature[n] = (parNature[n] || 0) + 1;
|
||||
}
|
||||
|
||||
// Alertes délais
|
||||
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.ref || r.reference || '',
|
||||
projet: r.projet || '',
|
||||
region: r.region || '',
|
||||
entrepreneur: r.entrepreneur || '',
|
||||
delai_restant: r._delai,
|
||||
niveau: niveauAlerte(r._delai),
|
||||
}))
|
||||
.sort((a, b) => a.delai_restant - b.delai_restant);
|
||||
|
||||
// Pilotage proactif (niveaux d'avancement)
|
||||
const pilotage = { normal: 0, sous_avancement: 0, depasse: 0 };
|
||||
for (const r of actifs) {
|
||||
const n = niveauAvancement(r.taux_phy ?? r.avt_phy, r.nature);
|
||||
if (n === 'normal') pilotage.normal++;
|
||||
else if (n === 'sous_avancement') pilotage.sous_avancement++;
|
||||
else pilotage.depasse++;
|
||||
}
|
||||
|
||||
res.json({
|
||||
total: rows.length,
|
||||
actifs: actifs.length,
|
||||
clotures: clotures.length,
|
||||
budget: {
|
||||
total: formatMontant(totalBudget),
|
||||
total_raw: totalBudget,
|
||||
consomme: formatMontant(totalConsomme),
|
||||
consomme_raw: totalConsomme,
|
||||
restant: formatMontant(totalBudget - totalConsomme),
|
||||
restant_raw: totalBudget - totalConsomme,
|
||||
},
|
||||
taux_avancement_moyen: tauxMoyen,
|
||||
par_statut: parStatut,
|
||||
par_region: parRegion,
|
||||
par_nature: parNature,
|
||||
alertes_delais: {
|
||||
count: alertes.length,
|
||||
critique: alertes.filter(a => a.niveau === 'critique').length,
|
||||
attention: alertes.filter(a => a.niveau === 'attention').length,
|
||||
items: alertes,
|
||||
},
|
||||
pilotage,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user