58 lines
2.2 KiB
JavaScript
58 lines
2.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { getPipeline } = require('../services/baserow');
|
|
|
|
const REGION_COLORS = {
|
|
Gabes:'#17A2B8', Gafsa:'#22C55E', Kebili:'#9333EA',
|
|
Medenine:'#0EA5E9', Sfax:'#002855', Tataouine:'#14B8A6', Tozeur:'#818CF8',
|
|
};
|
|
|
|
function phaseAO(r) {
|
|
const now = new Date();
|
|
const limit = r['date-limite'] ? new Date(r['date-limite']) : null;
|
|
const ouv = r['date-ouverture-adm-tech'] ? new Date(r['date-ouverture-adm-tech']) : null;
|
|
const clos = r['date-cloture-evaluation'] ? new Date(r['date-cloture-evaluation']) : null;
|
|
|
|
if (clos && now > clos) return { label:'Attribué', code:'attribue', color:'#6b7280' };
|
|
if (ouv && now > ouv) return { label:'Évaluation', code:'evaluation', color:'#8b5cf6' };
|
|
if (limit && now > limit) return { label:'Dépouillé', code:'depouille', color:'#f59e0b' };
|
|
if (limit) return { label:'Ouvert', code:'ouvert', color:'#10b981' };
|
|
return { label:'Préparation', code:'preparation', color:'#3b82f6' };
|
|
}
|
|
|
|
function joursAvantLimite(r) {
|
|
if (!r['date-limite']) return null;
|
|
const d = new Date(r['date-limite']);
|
|
if (isNaN(d.getTime())) return null;
|
|
return Math.ceil((d - new Date()) / 86400000);
|
|
}
|
|
|
|
function formatRegions(r) {
|
|
const v = r['Regions'] || r.regions || [];
|
|
if (!Array.isArray(v)) return [];
|
|
return v.map(x => ({
|
|
name: typeof x === 'object' ? (x.value || '') : x,
|
|
color: REGION_COLORS[typeof x === 'object' ? x.value : x] || '#888',
|
|
})).filter(x => x.name);
|
|
}
|
|
|
|
// GET /api/pipeline
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const rows = await getPipeline();
|
|
const enriched = rows.map(r => ({
|
|
...r,
|
|
_phase: phaseAO(r),
|
|
_jours_limite: joursAvantLimite(r),
|
|
_regions: formatRegions(r),
|
|
_estimation: parseFloat(r.Estimation || r.estimation || 0) || 0,
|
|
}));
|
|
const totalEstimation = enriched.reduce((s, r) => s + r._estimation, 0);
|
|
res.json({ count: enriched.length, total_estimation: totalEstimation, results: enriched });
|
|
} catch (err) {
|
|
res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|