From 186ef5251832650c939f7437fd6d740857801370 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 11 Dec 2024 14:55:26 +0100 Subject: [PATCH] feat: refs #7882 Osrm service --- back/methods/osrm-config/optimize.js | 15 +++++++++++++-- loopback/locale/es.json | 6 +++--- .../route/back/methods/route/optimizePriority.js | 13 ++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/back/methods/osrm-config/optimize.js b/back/methods/osrm-config/optimize.js index c03b70315..ae700d1cc 100644 --- a/back/methods/osrm-config/optimize.js +++ b/back/methods/osrm-config/optimize.js @@ -24,7 +24,7 @@ module.exports = Self => { } }); - Self.optimize = async(addressIds, firstAddressId) => { + Self.optimize = async(addressIds, firstAddressId, lastAddressId) => { const models = Self.app.models; try { const osrmConfig = await models.OsrmConfig.findOne(); @@ -53,13 +53,24 @@ module.exports = Self => { } } + if (lastAddressId) { + const firstAddress = await models.Address.findById(lastAddressId); + if (firstAddress.latitude && firstAddress.longitude) { + coords.push({ + addressId: firstAddress.id, + latitude: firstAddress.latitude.toFixed(6), + longitude: firstAddress.longitude.toFixed(6) + }); + } + } + if (!coords.length) throw new UserError('No address has coordinates'); const concatCoords = coords .map(coord => `${coord.longitude},${coord.latitude}`) .join(';'); const response = await axios.post(` - ${osrmConfig.url}/trip/v1/driving/${concatCoords}?source=first&roundtrip=true + ${osrmConfig.url}/trip/v1/driving/${concatCoords}?source=first&destination=last&roundtrip=true `); const tolerance = osrmConfig.tolerance; for (waypoint of response.data.waypoints) { diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 7e2253059..5f595e1ae 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -396,6 +396,6 @@ "Invalid options or too many coordinates": "Opciones invalidas o demasiadas coordenadas", "No address has coordinates": "Ninguna dirección tiene coordenadas", "An item type with the same code already exists": "Un tipo con el mismo código ya existe", - "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles" -} - + "Holidays to past days not available": "Las vacaciones a días pasados no están disponibles", + "All tickets have a route order": "Todos los tickets tienen orden de ruta" +} \ No newline at end of file diff --git a/modules/route/back/methods/route/optimizePriority.js b/modules/route/back/methods/route/optimizePriority.js index f84af50f8..c31913c66 100644 --- a/modules/route/back/methods/route/optimizePriority.js +++ b/modules/route/back/methods/route/optimizePriority.js @@ -71,13 +71,16 @@ module.exports = Self => { }, {}); const [mostFrequentZoneId] = Object.entries(zoneFrequency) .reduce((maxEntry, entry) => entry[1] > maxEntry[1] ? entry : maxEntry, [null, 0]); - const zone = await models.Zone.findById(mostFrequentZoneId, myOptions); - let firstAddress = zone.addressFk; - if (!firstAddress) firstAddress = (await models.ZoneConfig.findOne()).defaultAddressFk; + + // Obtenemos los address inicio y fin + const maxPosition = Math.max(...ticketAddress.map(g => g.priority)); + let firstAddress = (await models.Zone.findById(mostFrequentZoneId, myOptions))?.addressFk + || (await models.ZoneConfig.findOne())?.defaultAddressFk; + const lastAddress = firstAddress; + if (maxPosition) firstAddress = ticketAddress.find(g => g.priority === maxPosition)?.addressId; // Revisamos las coincidencias y actualizamos la prioridad en el array - const addressPositions = await models.OsrmConfig.optimize(addressIds, firstAddress, myOptions); - const maxPosition = Math.max(...ticketAddress.map(g => g.priority)); + const addressPositions = await models.OsrmConfig.optimize(addressIds, firstAddress, lastAddress, myOptions); await Promise.all(ticketAddress.map(async i => { const foundPosition = addressPositions.coords.find(item => item.addressId === i.addressId); if (foundPosition) i.priority = foundPosition.position + (maxPosition + 1);