Skip to content

Data entry interface #45

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 2 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
80 changes: 78 additions & 2 deletions server/src/controllers/bills.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -8,6 +8,8 @@ import {
getBills,
updateBill,
} from "../services/billServices";
import { getTripById } from "../services/tripServices";
import CustomError from "../services/CustomError";

const prisma = new PrismaClient();

Expand Down Expand Up @@ -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,
};
29 changes: 21 additions & 8 deletions server/src/controllers/trip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getStsVehicleEntryById,
updateStsVehicleEntry,
} from "../services/stsVehicle";
import { TripStatus } from "../types/tripStatus";

const prisma = new PrismaClient();

Expand Down Expand Up @@ -50,23 +51,28 @@ 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);
res.status(201).json(newTrip);
});

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({
Expand Down Expand Up @@ -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,
},
});

Expand Down
6 changes: 4 additions & 2 deletions server/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions server/src/routes/bills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;