backend logging

This commit is contained in:
jackiettran
2025-09-22 18:38:51 -04:00
parent 6199609a4d
commit 3e76769a3e
17 changed files with 1225 additions and 110 deletions

View File

@@ -4,6 +4,7 @@ const { Rental, Item, User } = require("../models"); // Import from models/index
const { authenticateToken } = require("../middleware/auth");
const FeeCalculator = require("../utils/feeCalculator");
const RefundService = require("../services/refundService");
const logger = require("../utils/logger");
const router = express.Router();
// Helper function to check and update review visibility
@@ -67,7 +68,12 @@ router.get("/my-rentals", authenticateToken, async (req, res) => {
res.json(rentals);
} catch (error) {
console.error("Error in my-rentals route:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error in my-rentals route", {
error: error.message,
stack: error.stack,
userId: req.user.id
});
res.status(500).json({ error: "Failed to fetch rentals" });
}
});
@@ -90,7 +96,12 @@ router.get("/my-listings", authenticateToken, async (req, res) => {
res.json(rentals);
} catch (error) {
console.error("Error in my-listings route:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error in my-listings route", {
error: error.message,
stack: error.stack,
userId: req.user.id
});
res.status(500).json({ error: "Failed to fetch listings" });
}
});
@@ -125,7 +136,13 @@ router.get("/:id", authenticateToken, async (req, res) => {
res.json(rental);
} catch (error) {
console.error("Error fetching rental:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error fetching rental", {
error: error.message,
stack: error.stack,
rentalId: req.params.id,
userId: req.user.id
});
res.status(500).json({ error: "Failed to fetch rental" });
}
});
@@ -355,7 +372,13 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
res.json(updatedRental);
return;
} catch (paymentError) {
console.error("Payment failed during approval:", paymentError);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Payment failed during approval", {
error: paymentError.message,
stack: paymentError.stack,
rentalId: req.params.id,
userId: req.user.id
});
// Keep rental as pending, but inform of payment failure
return res.status(400).json({
error: "Payment failed during approval",
@@ -538,7 +561,15 @@ router.post("/calculate-fees", authenticateToken, async (req, res) => {
display: displayFees,
});
} catch (error) {
console.error("Error calculating fees:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error calculating fees", {
error: error.message,
stack: error.stack,
userId: req.user.id,
startDate: req.query.startDate,
endDate: req.query.endDate,
itemId: req.query.itemId
});
res.status(500).json({ error: "Failed to calculate fees" });
}
});
@@ -566,7 +597,12 @@ router.get("/earnings/status", authenticateToken, async (req, res) => {
res.json(ownerRentals);
} catch (error) {
console.error("Error getting earnings status:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error getting earnings status", {
error: error.message,
stack: error.stack,
userId: req.user.id
});
res.status(500).json({ error: error.message });
}
});
@@ -580,7 +616,13 @@ router.get("/:id/refund-preview", authenticateToken, async (req, res) => {
);
res.json(preview);
} catch (error) {
console.error("Error getting refund preview:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error getting refund preview", {
error: error.message,
stack: error.stack,
rentalId: req.params.id,
userId: req.user.id
});
res.status(400).json({ error: error.message });
}
});
@@ -618,7 +660,13 @@ router.post("/:id/cancel", authenticateToken, async (req, res) => {
refund: result.refund,
});
} catch (error) {
console.error("Error cancelling rental:", error);
const reqLogger = logger.withRequestId(req.id);
reqLogger.error("Error cancelling rental", {
error: error.message,
stack: error.stack,
rentalId: req.params.id,
userId: req.user.id
});
res.status(400).json({ error: error.message });
}
});