File size: 5,001 Bytes
fcf8749 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
// GET /api/absorption/map-data - Get routes and virtual hubs for map visualization
const getMapData = async (req, res) => {
try {
const [routes, hubs] = await Promise.all([
prisma.optimizedRoute.findMany({
where: {
status: {
in: ['ACTIVE', 'PENDING', 'ALLOCATED']
}
},
include: {
deliveries: {
select: {
pickupLat: true,
pickupLng: true,
dropLat: true,
dropLng: true,
pickupLocation: true,
dropLocation: true
}
},
truck: {
select: {
licensePlate: true,
currentLat: true,
currentLng: true
}
}
},
take: 20
}),
prisma.virtualHub.findMany({
select: {
id: true,
name: true,
latitude: true,
longitude: true,
type: true,
radius: true
}
})
]);
// Format routes for map display
const formattedRoutes = routes.map(route => {
const firstDelivery = route.deliveries[0];
const lastDelivery = route.deliveries[route.deliveries.length - 1];
return {
id: route.id,
truckPlate: route.truck?.licensePlate || 'Unknown',
source: {
lat: firstDelivery?.pickupLat || route.truck?.currentLat,
lng: firstDelivery?.pickupLng || route.truck?.currentLng,
location: firstDelivery?.pickupLocation || 'Source'
},
destination: {
lat: lastDelivery?.dropLat,
lng: lastDelivery?.dropLng,
location: lastDelivery?.dropLocation || 'Destination'
},
polyline: route.routePolyline,
status: route.status
};
});
res.json({
routes: formattedRoutes,
hubs: hubs
});
} catch (error) {
console.error('Get map data error:', error);
res.status(500).json({ error: error.message });
}
};
// GET /api/absorption/active - Get active absorption requests
const getActiveAbsorptions = async (req, res) => {
try {
const absorptions = await prisma.absorptionOpportunity.findMany({
where: {
status: {
in: ['PENDING', 'ACCEPTED_BY_ROUTE1', 'ACCEPTED_BY_ROUTE2']
}
},
include: {
route1: {
include: {
truck: true,
driver: true
}
},
route2: {
include: {
truck: true,
driver: true
}
},
nearestHub: true
},
orderBy: {
proposedAt: 'desc'
}
});
const formattedAbsorptions = absorptions.map(abs => ({
id: abs.id,
route1: {
id: abs.route1.id,
truck: abs.route1.truck?.licensePlate,
driver: abs.route1.driver?.name
},
route2: {
id: abs.route2.id,
truck: abs.route2.truck?.licensePlate,
driver: abs.route2.driver?.name
},
hub: {
name: abs.nearestHub.name,
location: `${abs.nearestHub.latitude}, ${abs.nearestHub.longitude}`
},
overlapDistance: abs.overlapDistanceKm,
distanceSaved: abs.totalDistanceSaved,
carbonSaved: abs.potentialCarbonSaved,
meetTime: abs.estimatedMeetTime,
status: abs.status,
proposedAt: abs.proposedAt,
// Added fields for frontend compatibility
truck1: abs.route1.truck?.licensePlate,
truck2: abs.route2.truck?.licensePlate,
weight: abs.spaceRequiredWeight,
type: abs.route1.truck?.type || 'Standard', // Default to Standard if undefined
}));
res.json(formattedAbsorptions);
} catch (error) {
console.error('Get active absorptions error:', error);
res.status(500).json({ error: error.message });
}
};
module.exports = {
getMapData,
getActiveAbsorptions
}; |