Skip to content
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
8 changes: 0 additions & 8 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ server.use("/notification", notificationRoutes);
server.use("/violation", violationRoutes);
server.use("/violator", violatorRoutes);

// server.get("/testing", async (req: Request, res: Response) => {
// try {
// res.status(200).json({ title: "Testing Complete", message: "WOWZIES" });
// } catch (error) {
// res.sendStatus(500);
// }
// });

// For PORT
const PORT = Number(process.env.PORT) || 3000;

Expand Down
6 changes: 1 addition & 5 deletions backend/src/middlewares/verifyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dotenv.config();

const verifyToken = async (req: Request, res: Response, next: NextFunction) => {
const token = req.headers["authorization"]?.split(" ")[1];
// console.log(token)
if (!token) {
res
.status(401)
Expand All @@ -19,14 +18,11 @@ const verifyToken = async (req: Request, res: Response, next: NextFunction) => {
token,
process.env.ACCESS_TOKEN_SECRET!
) as JwtPayload;
// console.log(payload.userId)
req.user = payload.userId;
console.log(`UserId: ${req.user}`);
} catch (error) {
console.log(error);
res
.status(403)
.json({ title: "Token Invalid", message: "Access has expired." });
.json({ title: "Token Invalid", message: error });
return;
}
next();
Expand Down
14 changes: 6 additions & 8 deletions backend/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ router.post("/login", validateAuth, async (req: Request, res: Response) => {
// console.log(req.cookies.jwt);
res.status(200).json({ accessToken, isAdmin: user.is_admin, id: user.id });
} catch (error) {
res.sendStatus(500);
console.log(error);
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});



router.get("/refresh", async (req: Request, res: Response) => {
try {
const cookies = req.cookies;
Expand Down Expand Up @@ -153,8 +151,8 @@ router.get("/refresh", async (req: Request, res: Response) => {
return;
}
} catch (error) {
res.sendStatus(500).json({ title: "Unknown Error", message: error });
console.log(error);
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

Expand Down Expand Up @@ -201,8 +199,8 @@ router.get("/logout", async (req: Request, res: Response) => {
message: "Thank you for visiting, feel free to use our services again.",
});
} catch (error) {
res.sendStatus(500);
console.log(error);
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});
export default router;
87 changes: 41 additions & 46 deletions backend/src/routes/cars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ router.post("/check-license", async (req: Request, res: Response) => {

res.status(200).json(driverFound);
} catch (error) {
console.log(error);
res.sendStatus(500);
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

Expand Down Expand Up @@ -86,8 +86,7 @@ router.post("/add", async (req: Request, res: Response) => {
});
} catch (error) {
const errorMessage = (error as Error).message;
console.error("Error:", errorMessage);
res.status(500).json({ title: "Error", message: errorMessage });
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

Expand Down Expand Up @@ -124,61 +123,56 @@ router.get("/get", async (req: Request, res: Response) => {

res.status(200).json(cars);
} catch (error: unknown) {
if (error instanceof Error) {
console.error("Error fetching cars:", error.message);
res.status(500).json({ title: "Unknown Error", message: error.message });
return;
} else {
console.error("Unknown error:", error);
res.status(500).json({
title: "Unknown Error",
message: "An unexpected error occurred",
});
return;
}
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

router.patch("/update", async (req: Request, res: Response) => {
const { id, ...updates } = req.body;
try {
const { id, ...updates } = req.body;

if (!id) {
res.status(400).json({
title: "Validation Error",
message: "License plate is required to update the record.",
});
return;
}
if (!id) {
res.status(400).json({
title: "Validation Error",
message: "License plate is required to update the record.",
});
return;
}

const fields = Object.keys(updates);
const values = Object.values(updates);
const fields = Object.keys(updates);
const values = Object.values(updates);

const setClause = fields
.map((field, index) => `${field} = $${index + 1}`)
.join(", ");
const setClause = fields
.map((field, index) => `${field} = $${index + 1}`)
.join(", ");

const query = `UPDATE cars SET ${setClause}
const query = `UPDATE cars SET ${setClause}
WHERE id = $${fields.length + 1}
RETURNING *`;

const result = await pool.query(query, [...values, id]);
const result = await pool.query(query, [...values, id]);

if (result.rowCount === 0) {
res.status(404).json({
title: "Not Found",
message: "Cars with the specified ID does not exist.",
});
return;
}
if (result.rowCount === 0) {
res.status(404).json({
title: "Not Found",
message: "Cars with the specified ID does not exist.",
});
return;
}

const updateCar = result.rows[0];
console.log("Car updated successfully:", updateCar);
const updateCar = result.rows[0];
console.log("Car updated successfully:", updateCar);

res.status(200).json({
title: "Car Updated!",
message: `Car has been updated successfully.`,
driver: updateCar,
});
res.status(200).json({
title: "Car Updated!",
message: `Car has been updated successfully.`,
driver: updateCar,
});
} catch (error) {
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

router.delete("/delete", async (req: Request, res: Response) => {
Expand All @@ -197,7 +191,8 @@ router.delete("/delete", async (req: Request, res: Response) => {
res.status(200).json({ message: "Car Added Successfully" });
console.log("Driver deleted successfully:", car);
} catch (error) {
res.status(500).json({ message: error });
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

Expand Down
98 changes: 38 additions & 60 deletions backend/src/routes/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,8 @@ router.post("/add", validateDriver, async (req: Request, res: Response) => {

return;
} catch (error) {
if (error instanceof Error) {
console.error("Error occurred:", error.message);
res.status(500).json({ title: "Server Error", message: error.message });
} else {
console.error("Unexpected error occurred:", error);
res.status(500).json({
title: "Server Error",
message: "An unexpected error occurred.",
});
}
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

Expand Down Expand Up @@ -129,7 +121,6 @@ router.get("/get/:driverId", async (req: Request, res: Response) => {
res.status(200).json({ ...foundDriver, violations, cars });
} catch (error) {
const errorMessage = (error as Error).message;
console.error("Error fetching driver:", errorMessage);
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});
Expand All @@ -149,20 +140,6 @@ router.patch("/update", async (req: Request, res: Response) => {
license_expiration_date,
} = req.body;

console.log("THIS IS THE DRIVER!");
console.log(
id,
email,
first_name,
last_name,
middle_name,
date_of_birth,
sex,
driver_type,
license_number,
license_expiration_date
);

if (
![
id,
Expand Down Expand Up @@ -210,54 +187,55 @@ router.patch("/update", async (req: Request, res: Response) => {
message: `Successfully updated Driver: ${first_name} ${last_name}.`,
});
} catch (error) {
console.error("Error updating driver:", error);
res.status(500).json({
title: "Server Error",
message: "An error occurred while updating the driver.",
});
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}
});

router.delete("/delete", async (req: Request, res: Response) => {
console.log("Request body:", req.body); // Log the incoming request body
try {
const { id } = req.body;

const { id } = req.body;
if (!id) {
console.error("No ID provided in the request body");
res.status(400).json({
title: "Validation Error",
message: "Driver ID is required to delete a record.",
});
return;
}

if (!id) {
console.error("No ID provided in the request body");
res.status(400).json({
title: "Validation Error",
message: "Driver ID is required to delete a record.",
});
return;
}
await pool.query(`DELETE FROM cars WHERE driver_id = $1 RETURNING *`, [id]);

await pool.query(`DELETE FROM cars WHERE driver_id = $1 RETURNING *`, [id]);
await pool.query(`DELETE FROM violations WHERE id = $1 RETURNING *`, [id]);

await pool.query(`DELETE FROM violations WHERE id = $1 RETURNING *`, [id]);
const resultDriver = await pool.query(
`DELETE FROM drivers WHERE id = $1 RETURNING *`,
[id]
);

const resultDriver = await pool.query(
`DELETE FROM drivers WHERE id = $1 RETURNING *`,
[id]
);
if (resultDriver.rowCount === 0) {
console.error("Driver not found in the database");
res.status(404).json({
title: "Not Found",
message: "Driver with the specified ID does not exist.",
});
return;
}

const deletedDriver = resultDriver.rows[0];
console.log("Driver deleted successfully:", deletedDriver);

if (resultDriver.rowCount === 0) {
console.error("Driver not found in the database");
res.status(404).json({
title: "Not Found",
message: "Driver with the specified ID does not exist.",
res.status(200).json({
title: "Driver Deleted",
message: `Driver ${deletedDriver.last_name}, ${deletedDriver.first_name} has been removed.`,
driver: deletedDriver,
});
return;
} catch (error) {
const errorMessage = (error as Error).message;
res.status(500).json({ title: "Unknown Error", message: errorMessage });
}

const deletedDriver = resultDriver.rows[0];
console.log("Driver deleted successfully:", deletedDriver);

res.status(200).json({
title: "Driver Deleted",
message: `Driver ${deletedDriver.last_name}, ${deletedDriver.first_name} has been removed.`,
driver: deletedDriver,
});
});

export default router;
13 changes: 7 additions & 6 deletions backend/src/routes/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ router.get("/get-by-user", async (req: Req, res: Response) => {
res.json(notifications);
}
} catch (err) {
console.error("ERROR", err);
console.log(err);
res.status(500).json({
title: "Server Error",
message: "An unexpected error occurred while retrieving notifications",
Expand All @@ -36,11 +36,12 @@ router.get("/get-by-user", async (req: Req, res: Response) => {

// Create a new notification
router.post("/add", async (req: Req, res: Response) => {
const { licenseNumber, message } = req.body;
try {
const { driver_id, title, message } = req.body;

const { rows: drivers } = await pool.query(
"SELECT * FROM drivers WHERE license_number = $1",
[licenseNumber]
"SELECT * FROM drivers WHERE id = $1",
[driver_id]
);

if (drivers.length === 0) {
Expand All @@ -54,8 +55,8 @@ router.post("/add", async (req: Req, res: Response) => {
const driver = await drivers[0];

const { rows: notifications } = await pool.query(
"INSERT INTO notifications (user_id, message) VALUES ($1, $2)",
[driver.id, message]
"INSERT INTO notifications (driver_id, title, message) VALUES ($1, $2, $3) RETURNING *",
[driver.id, title, message]
);

if (notifications.length === 0) {
Expand Down
Loading
Loading