From 851aef901b95814d20af62780f54acbbd8ac37e9 Mon Sep 17 00:00:00 2001 From: shawon-majid Date: Thu, 28 Mar 2024 00:32:00 +0600 Subject: [PATCH 1/2] calculated actual trip duration --- server/src/controllers/trip.ts | 29 +++++++++++++++++++++-------- server/src/prisma/schema.prisma | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/src/controllers/trip.ts b/server/src/controllers/trip.ts index ab24ec9..309494e 100644 --- a/server/src/controllers/trip.ts +++ b/server/src/controllers/trip.ts @@ -10,6 +10,7 @@ import { getStsVehicleEntryById, updateStsVehicleEntry, } from "../services/stsVehicle"; +import { TripStatus } from "../types/tripStatus"; const prisma = new PrismaClient(); @@ -50,8 +51,9 @@ const createTrip = errorWrapper(async (req: Request, res: Response) => { weightOfWaste, distance, estimatedDuration, + tripStartTime: exitTime, estimatedFuelCost: new Prisma.Decimal(estimatedCost), - tripStatus: "PENDING", + tripStatus: TripStatus.PENDING, }; const newTrip = await addTrip(trip as Trip); @@ -59,14 +61,18 @@ const createTrip = errorWrapper(async (req: Request, res: Response) => { }); const getListOfTrips = errorWrapper(async (req: Request, res: Response) => { - const { tripStatus } = req.query; + const { tripStatus, landfillId } = req.query; let where: Prisma.TripWhereInput | undefined = undefined; - if (tripStatus) { - where = { - tripStatus: tripStatus as string, - }; + if (tripStatus || landfillId) { + where = {}; + if (tripStatus) { + where.tripStatus = tripStatus as string; + } + if (landfillId) { + where.landfillId = landfillId as string; + } } const trips = await prisma.trip.findMany({ @@ -95,16 +101,23 @@ const completeTrip = errorWrapper(async (req: Request, res: Response) => { const shortage = Number(trip.weightOfWaste) - weightOfWaste; - // calculate actual duration + const tripStartTime = new Date(trip.tripStartTime as Date); + + const entryTimeConverted = new Date(entryTime); + + let duration = + (entryTimeConverted.getTime() - tripStartTime.getTime()) / (1000 * 60); const completedTrip = await prisma.trip.update({ where: { id: tripId, }, data: { - tripStatus: "COMPLETED", + tripStatus: TripStatus.DELIVERED, weightOfWaste, shortage, + tripEndTime: entryTime, + actualDuration: duration, }, }); diff --git a/server/src/prisma/schema.prisma b/server/src/prisma/schema.prisma index d0bacc3..c8f1268 100644 --- a/server/src/prisma/schema.prisma +++ b/server/src/prisma/schema.prisma @@ -178,6 +178,8 @@ model Trip { vehicle Vehicle @relation(fields: [vehicleId], references: [id]) vehicleId String + tripStartTime DateTime? + tripEndTime DateTime? weightOfWaste Decimal? shortage Decimal? estimatedFuelCost Decimal? From ee5e547559f896990b887b8cb4701d744c71383f Mon Sep 17 00:00:00 2001 From: shawon-majid Date: Thu, 28 Mar 2024 01:29:21 +0600 Subject: [PATCH 2/2] bill generation api done --- server/src/controllers/bills.ts | 80 ++++++++++++++++++++++++++++++++- server/src/prisma/schema.prisma | 4 +- server/src/routes/bills.ts | 4 ++ 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/server/src/controllers/bills.ts b/server/src/controllers/bills.ts index cf8489b..a44e9c7 100644 --- a/server/src/controllers/bills.ts +++ b/server/src/controllers/bills.ts @@ -1,4 +1,4 @@ -import { Bill, PrismaClient } from "@prisma/client"; +import { Bill, Prisma, PrismaClient } from "@prisma/client"; import { Request, Response } from "express"; import errorWrapper from "../middlewares/errorWrapper"; import { @@ -8,6 +8,8 @@ import { getBills, updateBill, } from "../services/billServices"; +import { getTripById } from "../services/tripServices"; +import CustomError from "../services/CustomError"; const prisma = new PrismaClient(); @@ -61,4 +63,78 @@ const removeBill = errorWrapper( { statusCode: 500, message: "Couldn't delete bill" } ); -export { createBill, fetchBills, fetchBill, editBill, removeBill }; +const createBillFromTrip = errorWrapper( + async (req: Request, res: Response) => { + const { tripId, allocatedFuelCost } = req.body; + + const trip = await getTripById(tripId); + + if (!trip) { + throw new CustomError("No such trip found", 404); + } + + const bill = await prisma.bill.create({ + data: { + vehicleId: trip.vehicleId, + stsId: trip.stsId, + landfillId: trip.landfillId, + tripId: trip.id, + weightOfWaste: trip.weightOfWaste, + allocatedFuelCost, + }, + include: { + trip: true, + vehicle: true, + sts: true, + landfill: true, + }, + }); + + res.status(201).json(bill); + }, + { statusCode: 500, message: "Couldn't create bill" } +); + +const getListOfBills = errorWrapper(async (req: Request, res: Response) => { + const { landfillId, day } = req.query; + + let where: Prisma.BillWhereInput | undefined = undefined; + + if (landfillId || day) { + where = {}; + if (landfillId) { + where.landfillId = landfillId as string; + } + if (day) { + const days = parseInt(day as string, 10); + + const thresholdDate = new Date(); + thresholdDate.setDate(thresholdDate.getDate() - days); + + where.createdAt = { + gte: thresholdDate.toISOString(), + }; + } + } + + const bills = await prisma.bill.findMany({ + where, + include: { + trip: true, + vehicle: true, + sts: true, + landfill: true, + }, + }); + res.json(bills); +}); + +export { + createBill, + fetchBills, + fetchBill, + editBill, + removeBill, + createBillFromTrip, + getListOfBills, +}; diff --git a/server/src/prisma/schema.prisma b/server/src/prisma/schema.prisma index c8f1268..bba237d 100644 --- a/server/src/prisma/schema.prisma +++ b/server/src/prisma/schema.prisma @@ -159,8 +159,8 @@ model Bill { landfill Landfill @relation(fields: [landfillId], references: [id]) landfillId String - Trip Trip? @relation(fields: [TripId], references: [id]) - TripId String? + trip Trip? @relation(fields: [tripId], references: [id]) + tripId String? weightOfWaste Decimal? allocatedFuelCost Decimal? diff --git a/server/src/routes/bills.ts b/server/src/routes/bills.ts index 4b632c8..1789275 100644 --- a/server/src/routes/bills.ts +++ b/server/src/routes/bills.ts @@ -3,16 +3,20 @@ const router = express.Router(); import { createBill, + createBillFromTrip, editBill, fetchBill, fetchBills, + getListOfBills, removeBill, } from "../controllers/bills"; router.route("/").get(fetchBills); +router.route("/search").get(getListOfBills); router.route("/create").post(createBill); router.route("/:billId").get(fetchBill); router.route("/:billId").put(editBill); router.route("/:billId").delete(removeBill); +router.route("/create-from-trip/").post(createBillFromTrip); export default router;