feat: refs #7882 Osrm service
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Guillermo Bonet 2024-12-11 14:55:26 +01:00
parent bc072208d7
commit 186ef52518
3 changed files with 24 additions and 10 deletions

View File

@ -24,7 +24,7 @@ module.exports = Self => {
} }
}); });
Self.optimize = async(addressIds, firstAddressId) => { Self.optimize = async(addressIds, firstAddressId, lastAddressId) => {
const models = Self.app.models; const models = Self.app.models;
try { try {
const osrmConfig = await models.OsrmConfig.findOne(); 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'); if (!coords.length) throw new UserError('No address has coordinates');
const concatCoords = coords const concatCoords = coords
.map(coord => `${coord.longitude},${coord.latitude}`) .map(coord => `${coord.longitude},${coord.latitude}`)
.join(';'); .join(';');
const response = await axios.post(` 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; const tolerance = osrmConfig.tolerance;
for (waypoint of response.data.waypoints) { for (waypoint of response.data.waypoints) {

View File

@ -396,6 +396,6 @@
"Invalid options or too many coordinates": "Opciones invalidas o demasiadas coordenadas", "Invalid options or too many coordinates": "Opciones invalidas o demasiadas coordenadas",
"No address has coordinates": "Ninguna dirección tiene 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", "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"
}

View File

@ -71,13 +71,16 @@ module.exports = Self => {
}, {}); }, {});
const [mostFrequentZoneId] = Object.entries(zoneFrequency) const [mostFrequentZoneId] = Object.entries(zoneFrequency)
.reduce((maxEntry, entry) => entry[1] > maxEntry[1] ? entry : maxEntry, [null, 0]); .reduce((maxEntry, entry) => entry[1] > maxEntry[1] ? entry : maxEntry, [null, 0]);
const zone = await models.Zone.findById(mostFrequentZoneId, myOptions);
let firstAddress = zone.addressFk; // Obtenemos los address inicio y fin
if (!firstAddress) firstAddress = (await models.ZoneConfig.findOne()).defaultAddressFk; 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 // Revisamos las coincidencias y actualizamos la prioridad en el array
const addressPositions = await models.OsrmConfig.optimize(addressIds, firstAddress, myOptions); const addressPositions = await models.OsrmConfig.optimize(addressIds, firstAddress, lastAddress, myOptions);
const maxPosition = Math.max(...ticketAddress.map(g => g.priority));
await Promise.all(ticketAddress.map(async i => { await Promise.all(ticketAddress.map(async i => {
const foundPosition = addressPositions.coords.find(item => item.addressId === i.addressId); const foundPosition = addressPositions.coords.find(item => item.addressId === i.addressId);
if (foundPosition) i.priority = foundPosition.position + (maxPosition + 1); if (foundPosition) i.priority = foundPosition.position + (maxPosition + 1);