From 6c542caa1ab5a4cc7ad521ea458c650b335e16f3 Mon Sep 17 00:00:00 2001 From: shawon-majid Date: Sat, 30 Mar 2024 22:55:01 +0600 Subject: [PATCH] fixed create vehicle api --- server/.DS_Store | Bin 6148 -> 6148 bytes server/src/controllers/optimization.ts | 50 ++++++++++++------------- server/src/controllers/vehicle.ts | 2 +- server/src/services/optmization.ts | 5 ++- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/server/.DS_Store b/server/.DS_Store index 53d3a9df4240bc9c0a8385b01ed55e589a2c856b..655ce36bb29cfbb51bba78a5329c46c62a83a6ee 100644 GIT binary patch delta 86 zcmZoMXffEZm5F)cOvTAPOfn+5`7SO=Ir&Kp3=ACBr!T%dbo;0yR3Zhd1b;z>VQ_MO RZUImS11H~y&CbjRMF3!59_Ro7 delta 86 zcmZoMXffEZm5I4}_o~S~Ofn+5`7SO=Ir&Kp3=ACdPb}|o+&t { - const vehicles: Vehicle[] = await getSortedVehicles(); - - // console.log(vehicles); - - const stsList = await getSortedSTSFromLandfill( - "c4028362-6c17-4cf0-9b0e-ae20acfa2fbd" - ); - - const queue = new pq({ - comparator: (a: Vehicle, b: Vehicle) => { - if (a.busyTime && b.busyTime) { - return a.busyTime - b.busyTime; - } - return 0; // Return a default value of 0 when a.busyTime or b.busyTime is undefined - }, - }); - // enter all vehicles in the queue, with the priority being the emergency factor - - vehicles.forEach((vehicle: Vehicle) => { - queue.queue(vehicle); - }); - - for (let i = 0; i < queue.length; i++) { - console.log(queue.dequeue()); - } + const stsList = await prisma.sTS.findMany({}); + + const vehicles: Vehicle[] = await getSortedVehiclesBySTS(stsList[0].id); + + console.log(vehicles); + + // const queue = new pq({ + // comparator: (a: Vehicle, b: Vehicle) => { + // if (a.busyTime && b.busyTime) { + // return a.busyTime - b.busyTime; + // } + // return 0; // Return a default value of 0 when a.busyTime or b.busyTime is undefined + // }, + // }); + // // enter all vehicles in the queue, with the priority being the emergency factor + + // vehicles.forEach((vehicle: Vehicle) => { + // queue.queue(vehicle); + // }); + + // for (let i = 0; i < queue.length; i++) { + // console.log(queue.dequeue()); + // } // console.log(stsList); }; diff --git a/server/src/controllers/vehicle.ts b/server/src/controllers/vehicle.ts index 2357eea..a1dea64 100644 --- a/server/src/controllers/vehicle.ts +++ b/server/src/controllers/vehicle.ts @@ -18,7 +18,7 @@ const createVehicle = errorWrapper( async (req: Request, res: Response) => { const vehicleInfo: Vehicle = req.body; - const stsId = vehicleInfo.stsId; + const stsId = vehicleInfo.stsId || ""; const sts = await prisma.sTS.findUnique({ where: { diff --git a/server/src/services/optmization.ts b/server/src/services/optmization.ts index 77366fd..10c5c31 100644 --- a/server/src/services/optmization.ts +++ b/server/src/services/optmization.ts @@ -13,9 +13,10 @@ const apiKey = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY || ""; const prisma = new PrismaClient(); -const getSortedVehicles = async () => { +const getSortedVehiclesBySTS = async (stsId: string) => { const vehicles: Vehicle[] = await prisma.$queryRaw` SELECT * FROM "Vehicle" + WHERE "stsId" = ${stsId} ORDER BY ("loadedFuelCostPerKm" / "capacity") DESC `; @@ -103,7 +104,7 @@ const getDistance = async (origin: string, destination: string) => { }; export { - getSortedVehicles, + getSortedVehiclesBySTS, getSortedSTSFromLandfill, getDuration, getDistance,