Files
Rasadyar_Inspection_Back/routes/inspectRoutes.js
2026-01-26 10:54:31 +03:30

226 lines
5.5 KiB
JavaScript

const express = require("express");
const router = express.Router();
const Inspect = require("../models/Inspect");
const { verifyToken } = require("../lib/jwtUtils");
router.use(verifyToken);
router.post("/inspections", async (req, res) => {
try {
const {
place_key,
province,
license_type,
document_number,
issuer,
economic_code,
registration_number,
ownership_type,
unit_type,
description,
infractions,
violation_amount,
plaintiff_damage,
inspectors,
user_id,
} = req.body;
const newInspection = new Inspect({
place_key,
province,
license_type,
document_number,
issuer,
economic_code,
registration_number,
ownership_type,
unit_type,
description,
infractions,
violation_amount,
plaintiff_damage,
inspectors,
user_id,
});
await newInspection.save();
res.status(201).json({
message: "inspection created successfully",
place: newInspection,
});
} catch (error) {
console.error("Error creating inspection:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/inspections/:place_key", async (req, res) => {
try {
const placeId = req.params.place_key;
const inspections = await Inspect.find({ place_key: placeId }).sort({
createdAt: -1,
});
if (!inspections || inspections.length === 0) {
return res
.status(404)
.json({ message: "Inspections not found for this place" });
}
res.status(200).json(inspections);
} catch (error) {
console.error("Error retrieving inspections:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/userinspections/:userInspects", async (req, res) => {
try {
const userInspects = req.params.userInspects;
const inspections = await Inspect.find({ user_id: userInspects }).sort({
createdAt: -1,
});
if (!inspections || inspections.length === 0) {
return res
.status(404)
.json({ message: "Inspections not found for this user" });
}
res.status(200).json(inspections);
} catch (error) {
console.error("Error retrieving inspections:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/inspects", async (req, res) => {
try {
const places = await Inspect.find().sort({ createdAt: -1 });
res.status(200).json(places);
} catch (error) {
console.error("Error retrieving inspects:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.get("/inspectkeys", async (req, res) => {
try {
const places = await Inspect.find()
.select("place_key infractions")
.sort({ createdAt: -1 });
const placesWithInfractions = places.map((place) => ({
place_key: place.place_key,
hasInfractions: place.infractions.length > 0,
}));
res.status(200).json(placesWithInfractions);
} catch (error) {
console.error("Error retrieving inspects:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.delete("/inspections/:inspectId", async (req, res) => {
try {
const inspectId = req.params.inspectId;
const deletedPlace = await Inspect.findByIdAndDelete(inspectId);
if (!deletedPlace) {
return res.status(404).json({ message: "Inspection not found" });
}
res.status(200).json({
message: "Inspection deleted successfully",
place: deletedPlace,
});
} catch (error) {
console.error("Error deleting inspection:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
router.delete(
"/inspections/:id/infractions/:infractionId",
async (req, res) => {
const inspectId = req.params.id;
const infractionId = req.params.infractionId;
try {
const inspect = await Inspect.findById(inspectId);
if (!inspect) {
return res.status(404).json({ message: "Place not found" });
}
inspect.infractions.pull({ _id: infractionId });
await inspect.save();
res.status(200).json({ message: "Infraction deleted successfully" });
} catch (error) {
console.error("Error deleting infraction:", error);
res.status(500).json({ message: "Internal Server Error" });
}
}
);
//routes
router.put("/inspections/:inspectionId", async (req, res) => {
try {
const inspectionId = req.params.inspectionId;
const {
place_key,
province,
license_type,
document_number,
issuer,
economic_code,
registration_number,
ownership_type,
unit_type,
description,
infractions,
violation_amount,
plaintiff_damage,
inspectors,
} = req.body;
const updatedPlace = await Inspect.findByIdAndUpdate(
inspectionId,
{
place_key,
province,
license_type,
document_number,
issuer,
economic_code,
registration_number,
ownership_type,
unit_type,
description,
infractions,
violation_amount,
plaintiff_damage,
inspectors,
},
{ new: true }
);
if (!updatedPlace) {
return res.status(404).json({ message: "Inspection not found" });
}
res.status(200).json({
message: "Inspection updated successfully",
place: updatedPlace,
});
} catch (error) {
console.error("Error updating Inspection:", error);
res.status(500).json({ message: "Internal Server Error" });
}
});
module.exports = router;