Files
ferrovia-regional/fix_details.js
T
2026-09-24 16:29:30 +02:00

78 lines
3.6 KiB
JavaScript

const fs = require('fs');
let code = fs.readFileSync('src/components/TrainDetailsClient.tsx', 'utf8');
const newPerturbationLogic = `
// Fetch perturbations
const parts = currentDateDisplay.split('/');
const targetIsoDate = parts.length === 3 ? \`\${parts[2]}-\${parts[1]}-\${parts[0]}\` : new Date().toISOString().split('T')[0];
const { data: pertData } = await supabase.from('perturbations').select('*').eq('actif', true);
let currentDelay = 0;
let pArrets: any[] = [];
if (pertData) {
const matchingPert = pertData.find((p: any) => {
const matchTrain = p.horaire_id === h.id || p.horaire_id === h.numero_train;
if (!matchTrain) return false;
const pStart = (p.date_debut || '').slice(0, 10);
const pEnd = (p.date_fin || '').slice(0, 10);
if (pStart && pEnd && pStart <= targetIsoDate && targetIsoDate <= pEnd) return true;
if (p.jours_perturbation && Array.isArray(p.jours_perturbation)) {
return p.jours_perturbation.some((jp: any) => {
const d = (jp.date || jp.dateDebut || '').slice(0, 10);
return d === targetIsoDate;
});
}
return !pStart && !pEnd && (!p.jours_perturbation || p.jours_perturbation.length === 0);
});
if (matchingPert) {
pArrets = Array.isArray(matchingPert.perturbations_arrets) ? matchingPert.perturbations_arrets : [];
// Find max delay in perturbations_arrets
let maxArretsDelay = 0;
pArrets.forEach(pa => {
if (pa.retard_cumule > maxArretsDelay) maxArretsDelay = pa.retard_cumule;
if (pa.retard_entrant > maxArretsDelay) maxArretsDelay = pa.retard_entrant;
});
currentDelay = maxArretsDelay > 0 ? maxArretsDelay : (matchingPert.duree_retard_minutes || 0);
setPerturbationInfo({
type: matchingPert.type_perturbation || 'Retard',
delayMinutes: currentDelay,
message: matchingPert.cause_details || matchingPert.notes || '',
});
setDelayMinutes(currentDelay);
}
}
`;
const newResolveLogic = `
const resolved: StopItem[] = parsedStops.map((s, idx) => {
const prev = s.heure_depart || s.heure_arrivee || '12:00';
let est = prev;
// Get delay specifically for this stop
const stopPert = pArrets.find(pa => pa.gare_nom === s.gare_nom || pa.gare_id === s.gare_id);
const stopDelay = stopPert && stopPert.retard_cumule !== undefined ? stopPert.retard_cumule : currentDelay;
if (stopDelay > 0) {
const [hh, mm] = prev.split(':').map(Number);
const totalMin = (hh * 60 + mm + stopDelay) % 1440;
const newH = String(Math.floor(totalMin / 60)).padStart(2, '0');
const newM = String(totalMin % 60).padStart(2, '0');
est = \`\${newH}:\${newM}\`;
}
`;
// Replace fetch perturbation logic
code = code.replace(/\/\/ Fetch perturbations[\s\S]*?(?=\/\/ Fetch materiel roulant)/, newPerturbationLogic);
// Replace mapping logic
code = code.replace(/const resolved: StopItem\[\] = parsedStops\.map\(\(s, idx\) => \{[\s\S]*?if \(currentDelay > 0\) \{[\s\S]*?est = `\$\{newH\}:\$\{newM\}`;\s*\}/, newResolveLogic);
fs.writeFileSync('src/components/TrainDetailsClient.tsx', code);