Skip to content

fixed create vehicle api #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified server/.DS_Store
Binary file not shown.
50 changes: 24 additions & 26 deletions server/src/controllers/optimization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,35 @@ import pq from "js-priority-queue";
import { Vehicle } from "../types/vehicle";
import {
getSortedSTSFromLandfill,
getSortedVehicles,
getSortedVehiclesBySTS,
} from "../services/optmization";

const prisma = new PrismaClient();

const getSchedule = async () => {
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);
};
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
5 changes: 3 additions & 2 deletions server/src/services/optmization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
`;

Expand Down Expand Up @@ -103,7 +104,7 @@ const getDistance = async (origin: string, destination: string) => {
};

export {
getSortedVehicles,
getSortedVehiclesBySTS,
getSortedSTSFromLandfill,
getDuration,
getDistance,
Expand Down