const { createClient } = require('@supabase/supabase-js'); require('dotenv').config({ path: '/Users/mathisgrillot/IdeaProjects/afficheurs-ferrovia/.env' }); const supabase = createClient(process.env.REACT_APP_SUPABASE_URL, process.env.REACT_APP_SUPABASE_ANON_KEY); const normalizeName = (s) => { if (!s) return ''; try { const t = String(s).normalize('NFD').replace(/\p{M}/gu, '').toLowerCase().trim(); return t.replace(/[-_]+/g, ' ').replace(/\s+/g, ' '); } catch (e) { return String(s).toLowerCase().trim(); } }; const parseTimeHM = (v) => { if (!v) return null; const m = String(v).match(/(\d{1,2}):(\d{2})/); if (m) return parseInt(m[1], 10)*60 + parseInt(m[2], 10); return null; }; async function run() { const gareDisplay = "Ouges"; const flexNeedle = `%${String(gareDisplay).replace(/[-_+\s]+/g, '%')}%`; const { data: gdata } = await supabase.from('gares').select('id,type').ilike('nom', flexNeedle).limit(1); const stationType = gdata && gdata.length ? gdata[0].type.toLowerCase() : null; console.log("stationType:", stationType); const thresholdForStationType = (typeGare) => { const tG = String(typeGare || '').trim().toLowerCase(); if (tG === 'interurbaine' || tG.includes('interurb')) return 360; if (tG === 'ville' || tG.includes('ville')) return 30; return 30; }; const threshold = thresholdForStationType(stationType); console.log("threshold:", threshold); const { data: allData } = await supabase.from('horaires').select('*').limit(200); const horaires = allData.filter(h => JSON.stringify(h).includes("Ouges")); const h = horaires[0]; const stops = h.gares_desservies; const targetStationName = gareDisplay; const stopForStation = (stops || []).find(s => { const nm = s.gare_nom || s.nom || s.name || s.stop_name || ''; const ns = normalizeName(nm || ''); const target = normalizeName(targetStationName || ''); return (targetStationName && (ns === target || ns.includes(target) || target.includes(ns))); }) || null; h._displayPlatform = (stopForStation && stopForStation.quai_reel) || h.quai_reel || ''; console.log("_displayPlatform:", h._displayPlatform); const platform = (h._displayPlatform || '').toString().trim(); let showPlatform = Boolean(platform); console.log("Boolean(platform):", showPlatform); if (showPlatform) { let diffMinutes = null; h._depHM = parseTimeHM(h.heure_depart); const sched = h._depHM; console.log("sched:", sched); const currentTime = new Date(); if (sched !== null) { const baseDate = new Date(); baseDate.setHours(Math.floor(sched / 60), sched % 60, 0, 0); const nowHM = currentTime.getHours() * 60 + currentTime.getMinutes(); if (sched < 240 && nowHM > 1200) baseDate.setDate(baseDate.getDate() + 1); else if (sched > 1200 && nowHM < 240) baseDate.setDate(baseDate.getDate() - 1); diffMinutes = (baseDate.getTime() - currentTime.getTime()) / 60000; } console.log("diffMinutes:", diffMinutes); if (diffMinutes !== null) { showPlatform = (diffMinutes <= threshold); } else { showPlatform = false; } } console.log("FINAL showPlatform:", showPlatform); } run();