Skip to content

Commit

Permalink
added route for View, Delete, Create Report
Browse files Browse the repository at this point in the history
  • Loading branch information
KovacevicAleksa committed Feb 17, 2025
1 parent f131803 commit 950d2d9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions backend/routes/reportRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import express from "express";
import { auth, adminAuth } from "../middleware/auth.js";
import mongoose from "mongoose"; // For ObjectId validation
import Report from "../models/report.js";

const router = express.Router();

// Route to get all reports (Admin only)
router.get("/report", adminAuth, async (req, res) => {
try {
const reports = await Report.find();
res.status(200).json(reports);
} catch (error) {
res.status(500).json({ error: "Failed to fetch reports" });
}
});

// Route to add a new report
router.post("/report", auth, async (req, res) => {
const { email, reportText, category } = req.body;

if (!email || !reportText || !category) {
return res.status(400).json({ error: "All fields are required" });
}

try {
const newReport = new Report({ email, reportText, category });
await newReport.save();
res.status(201).json({ message: "Report submitted successfully", newReport });
} catch (error) {
res.status(500).json({ error: "Failed to submit report" });
}
});

// Route to delete a specific report by ID (Admin only)
router.delete("/report/:id", adminAuth, async (req, res) => {
const { id } = req.params;

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).json({ error: "Invalid report ID" });
}

try {
const deletedReport = await Report.findByIdAndDelete(id);
if (!deletedReport) {
return res.status(404).json({ error: "Report not found" });
}
res.status(200).json({ message: "Report deleted successfully" });
} catch (error) {
res.status(500).json({ error: "Failed to delete report" });
}
});

export default router;
2 changes: 2 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import adminRoutes from "./routes/adminRoutes.js";
import healthCheckRoutes from "./routes/healthCheckRoutes.js";
import barcodeRoutes from "./routes/QRCodeRoutes.js";
import ticketRoutes from "./routes/ticketRoutes.js";
import reportRoutes from "./routes/reportRoutes.js"

// Import Swagger
import swaggerUi from "swagger-ui-express";
Expand Down Expand Up @@ -171,6 +172,7 @@ app.use("/", adminRoutes); //Admin routes (protected by adminAuth middleware)
app.use("/", healthCheckRoutes); //Health check routes
app.use("/", barcodeRoutes); //Barcode routes
app.use("/", ticketRoutes);//Ticket routes
app.use("/", reportRoutes);//Report routes


// New chat route using the initialized Socket.IO instance
Expand Down

0 comments on commit 950d2d9

Please # to comment.