Skip to content

full user panel fetch #39

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 3 commits into from
Mar 27, 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
67 changes: 65 additions & 2 deletions server/src/controllers/trip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from "express";
import errorWrapper from "../middlewares/errorWrapper";
import { addTrip } from "../services/tripServices";
import { addTrip, getTripById } from "../services/tripServices";
import { Prisma, Trip } from "@prisma/client";
import { PrismaClient } from "@prisma/client";
import CustomError from "../services/CustomError";
Expand All @@ -23,6 +23,16 @@ const createTrip = errorWrapper(async (req: Request, res: Response) => {
throw new CustomError("No such Vehicle entry found", 404);
}

await prisma.sTSVehicleEntry.update({
where: {
id: stsVehicleId,
},
data: {
exitTime,
weightOfWaste,
},
});

const vehicle = stsVehicleInfo.vehicle;
const sts = stsVehicleInfo.sts;

Expand All @@ -48,4 +58,57 @@ const createTrip = errorWrapper(async (req: Request, res: Response) => {
res.status(201).json(newTrip);
});

export { createTrip };
const getListOfTrips = errorWrapper(async (req: Request, res: Response) => {
const { tripStatus } = req.query;

let where: Prisma.TripWhereInput | undefined = undefined;

if (tripStatus) {
where = {
tripStatus: tripStatus as string,
};
}

const trips = await prisma.trip.findMany({
where,
});
res.json(trips);
});

const completeTrip = errorWrapper(async (req: Request, res: Response) => {
const { tripId, landfillId, vehicleId, weightOfWaste, entryTime } = req.body;

prisma.landfillVehicleEntry.create({
data: {
landfillId,
vehicleId,
weightOfWaste,
entryTime,
},
});

const trip = await getTripById(tripId);

if (!trip) {
throw new CustomError("No such trip found", 404);
}

const shortage = Number(trip.weightOfWaste) - weightOfWaste;

// calculate actual duration

const completedTrip = await prisma.trip.update({
where: {
id: tripId,
},
data: {
tripStatus: "COMPLETED",
weightOfWaste,
shortage,
},
});

res.json(completedTrip);
});

export { createTrip, getListOfTrips, completeTrip };
28 changes: 28 additions & 0 deletions server/src/controllers/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Prisma } from "./../../node_modules/.prisma/client/index.d";
import { PrismaClient, Vehicle } from "@prisma/client";
import { Request, Response } from "express";
import errorWrapper from "../middlewares/errorWrapper";
Expand Down Expand Up @@ -63,10 +64,37 @@ const removeVehicle = errorWrapper(
{ statusCode: 500, message: "Couldn't delete vehicle" }
);

const getVehiclesOnQuery = errorWrapper(async (req: Request, res: Response) => {
const { landFillId, vehicleType, vehicleNumber } = req.query;

let where: Prisma.VehicleWhereInput | undefined = undefined;
if (landFillId || vehicleType || vehicleNumber) {
where = {};
if (landFillId) {
where.landFillId = landFillId as string;
}

if (vehicleType) {
where.vehicleType = vehicleType as string;
}

if (vehicleNumber) {
where.vehicleNumber = vehicleNumber as string;
}
}

const vehicles = await prisma.vehicle.findMany({
where,
});

res.json(vehicles);
});

export {
createVehicle,
fetchAllVehicles,
fetchVehicleById,
editVehicle,
removeVehicle,
getVehiclesOnQuery,
};
1 change: 1 addition & 0 deletions server/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ model Trip {
vehicleId String

weightOfWaste Decimal?
shortage Decimal?
estimatedFuelCost Decimal?

distance Decimal?
Expand Down
9 changes: 9 additions & 0 deletions server/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ async function main() {
console.log(newStsVehicleEntry);
}

console.log("Seeding trips...");

for (const trip of tripData) {
const newTrip = await prisma.trip.create({
data: trip,
});
console.log(newTrip);
}

console.log("Seeding completed!");
}

Expand Down
4 changes: 3 additions & 1 deletion server/src/routes/trip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import express from "express";
import { createTrip } from "../controllers/trip";
import { completeTrip, createTrip, getListOfTrips } from "../controllers/trip";
const router = express.Router();

router.route("/create").post(createTrip);
router.route("/search").get(getListOfTrips);
router.route("/complete").post(completeTrip);

export default router;
2 changes: 2 additions & 0 deletions server/src/routes/vehicles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
editVehicle,
fetchAllVehicles,
fetchVehicleById,
getVehiclesOnQuery,
removeVehicle,
} from "../controllers/vehicle";

const router = express.Router();

router.route("/create").post(createVehicle);
router.route("/search").get(getVehiclesOnQuery);
router.route("/").get(fetchAllVehicles);
router.route("/:vehicleId").get(fetchVehicleById);
router.route("/:vehicleId").put(editVehicle);
Expand Down