fix(travel.js): fix Ulys API parsing issues and ID extraction

This commit is contained in:
ElPoyo
2026-06-05 14:11:06 +02:00
parent 69a65d83f2
commit adb0a2e7c9
+24 -9
View File
@@ -125,11 +125,18 @@ async function getUlysRate(vehicleCategory, passages) {
);
const data = res.data;
if (Array.isArray(data) && data.length > 0) {
// Système fermé : 1 réponse avec entranceToll + exitToll
if (data.length === 1 && data[0].price > 0) return data[0].price;
// Système ouvert (barrières individuelles) : sommer les prix
const total = data.reduce((sum, d) => sum + (d.price || 0), 0);
return total > 0 ? total : null;
if (passages.length === 2) {
// We expect a single closed system response
if (data.length === 1 && data[0].entranceToll && data[0].exitToll && data[0].price > 0) {
return data[0].price;
}
return null;
} else if (passages.length === 1) {
if (data.length === 1 && data[0].price > 0) {
return data[0].price;
}
return null;
}
}
return null;
} catch (e) {
@@ -144,16 +151,17 @@ async function calculateTollCost(encodedPolyline, vehicleCategory) {
try {
// 1. Demander à Ulys les gares sur le tracé
const legsData = await getUlysTollLegs(encodedPolyline);
const features = Array.isArray(legsData) ? legsData : (legsData && legsData.features ? legsData.features : []);
if (legsData && Array.isArray(legsData.features) && legsData.features.length > 0) {
if (features && features.length > 0) {
// Extraire les gares dans l'ordre du tracé
const tollGates = [];
for (const feature of legsData.features) {
const props = feature.properties || {};
for (const feature of features) {
const props = feature.properties || feature.Placemark || feature.placemark || {};
// La réponse Ulys peut utiliser différents noms de champs
// On cherche l'identifiant de la gare dans tous les champs connus
const id =
let id =
props.id_gare ||
props.idGare ||
props.id ||
@@ -161,6 +169,10 @@ async function calculateTollCost(encodedPolyline, vehicleCategory) {
props.gare_id ||
props.tollStationId;
if (!id && props.Code) {
id = props.Code.split('_')[0];
}
if (!id) continue;
const idStr = String(id);
if (idStr.length < 5) continue;
@@ -303,6 +315,9 @@ exports.googleMapsComputeRoute = async (req, res) => {
]);
const routes = [];
console.log("resToll.data.routes length:", resToll.data.routes ? resToll.data.routes.length : 0);
if (!resToll.data.routes) console.log("resToll.data:", JSON.stringify(resToll.data, null, 2));
// --- Route avec péage ---
if (resToll.data.routes && resToll.data.routes.length > 0) {