68 lines
3.0 KiB
JavaScript
68 lines
3.0 KiB
JavaScript
import { createClient } from '@supabase/supabase-js';
|
|
import { buildMovements, executeAllocationAlgorithm } from '../src/utils/algorithmeQuais.js';
|
|
|
|
const supabaseUrl = process.env.REACT_APP_SUPABASE_URL || process.env.SUPABASE_URL;
|
|
const supabaseKey = process.env.REACT_APP_SUPABASE_ANON_KEY || process.env.SUPABASE_ANON_KEY;
|
|
// Note: un service_role key est préférable en production pour un cron
|
|
const supabase = createClient(supabaseUrl, supabaseKey);
|
|
|
|
export default async function handler(req, res) {
|
|
if (req.headers.authorization !== `Bearer ${process.env.CRON_SECRET}`) {
|
|
return res.status(401).end('Unauthorized');
|
|
}
|
|
|
|
try {
|
|
const today = new Date().toISOString().split('T')[0];
|
|
|
|
// 1. Fetch gares
|
|
const { data: gares } = await supabase.from('gares').select('*');
|
|
if (!gares) return res.status(200).json({ message: "Aucune gare" });
|
|
|
|
// 2. Fetch horaires du jour
|
|
const { data: horaires } = await supabase.from('horaires').select(`
|
|
*,
|
|
materiel_roulant ( id, nom ),
|
|
ligne_id
|
|
`).contains('jours_circulation', `[{"date": "${today}", "type": "jour"}]`);
|
|
|
|
// 3. Fetch perturbations
|
|
const { data: perturbations } = await supabase.from('perturbations').select('*').contains('jours_perturbation', `[{"date": "${today}", "type": "jour"}]`);
|
|
|
|
// 4. Fetch règles
|
|
const { data: regles } = await supabase.from('regles_quais').select('*');
|
|
|
|
for (const gare of gares) {
|
|
const reglesGare = (regles || []).filter(r => r.gare_id === gare.id);
|
|
const quaisLibres = (gare.gare_quais || []).filter(q => q.nom !== 'CG');
|
|
|
|
const movements = buildMovements(horaires || [], gare, perturbations || []);
|
|
|
|
const { blockUpdates, perturbationsToCreate } = executeAllocationAlgorithm(movements, quaisLibres, reglesGare);
|
|
|
|
// Update DB
|
|
for (const update of blockUpdates) {
|
|
const { data: currentHoraire } = await supabase.from('horaires').select('gares_desservies').eq('id', update.horaireId).single();
|
|
if (currentHoraire && currentHoraire.gares_desservies) {
|
|
const newDesservies = currentHoraire.gares_desservies.map(arret => {
|
|
if (String(arret.gare_id) === String(gare.id) || arret.gare_nom === gare.nom) {
|
|
return { ...arret, quai_reel: update.payload.depart || update.payload.arrivee || update.payload[arret.ordre] || arret.quai_reel };
|
|
}
|
|
return arret;
|
|
});
|
|
await supabase.from('horaires').update({ gares_desservies: newDesservies }).eq('id', update.horaireId);
|
|
}
|
|
}
|
|
|
|
for (const pert of perturbationsToCreate) {
|
|
const exists = perturbations.find(p => p.horaire_id === pert.horaire_id && p.cause === pert.cause);
|
|
if (!exists) await supabase.from('perturbations').insert([pert]);
|
|
}
|
|
}
|
|
|
|
return res.status(200).json({ success: true, message: "Attribution de nuit terminée." });
|
|
} catch (err) {
|
|
console.error(err);
|
|
return res.status(500).json({ error: err.message });
|
|
}
|
|
}
|