removed cron job that made rentals active. Now whether or not the rental is active is determined on the fly
This commit is contained in:
@@ -14,8 +14,24 @@ const emailServices = require("../services/email");
|
||||
const logger = require("../utils/logger");
|
||||
const { validateS3Keys } = require("../utils/s3KeyValidator");
|
||||
const { IMAGE_LIMITS } = require("../config/imageLimits");
|
||||
const { isActive, getEffectiveStatus } = require("../utils/rentalStatus");
|
||||
const router = express.Router();
|
||||
|
||||
// Helper to add displayStatus to rental response
|
||||
const addDisplayStatus = (rental) => {
|
||||
if (!rental) return rental;
|
||||
const rentalJson = rental.toJSON ? rental.toJSON() : rental;
|
||||
return {
|
||||
...rentalJson,
|
||||
displayStatus: getEffectiveStatus(rentalJson),
|
||||
};
|
||||
};
|
||||
|
||||
// Helper to add displayStatus to array of rentals
|
||||
const addDisplayStatusToArray = (rentals) => {
|
||||
return rentals.map(addDisplayStatus);
|
||||
};
|
||||
|
||||
// Helper function to check and update review visibility
|
||||
const checkAndUpdateReviewVisibility = async (rental) => {
|
||||
const now = new Date();
|
||||
@@ -75,7 +91,7 @@ router.get("/renting", authenticateToken, async (req, res) => {
|
||||
order: [["createdAt", "DESC"]],
|
||||
});
|
||||
|
||||
res.json(rentals);
|
||||
res.json(addDisplayStatusToArray(rentals));
|
||||
} catch (error) {
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Error in renting route", {
|
||||
@@ -103,7 +119,7 @@ router.get("/owning", authenticateToken, async (req, res) => {
|
||||
order: [["createdAt", "DESC"]],
|
||||
});
|
||||
|
||||
res.json(rentals);
|
||||
res.json(addDisplayStatusToArray(rentals));
|
||||
} catch (error) {
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Error in owning route", {
|
||||
@@ -167,7 +183,7 @@ router.get("/:id", authenticateToken, async (req, res) => {
|
||||
.json({ error: "Unauthorized to view this rental" });
|
||||
}
|
||||
|
||||
res.json(rental);
|
||||
res.json(addDisplayStatus(rental));
|
||||
} catch (error) {
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Error fetching rental", {
|
||||
@@ -235,10 +251,11 @@ router.post("/", authenticateToken, requireVerifiedEmail, async (req, res) => {
|
||||
);
|
||||
|
||||
// Check for overlapping rentals using datetime ranges
|
||||
// Note: "active" rentals are stored as "confirmed" with startDateTime in the past
|
||||
const overlappingRental = await Rental.findOne({
|
||||
where: {
|
||||
itemId,
|
||||
status: { [Op.in]: ["confirmed", "active"] },
|
||||
status: "confirmed",
|
||||
[Op.or]: [
|
||||
{
|
||||
[Op.and]: [
|
||||
@@ -875,7 +892,7 @@ router.post("/:id/mark-completed", authenticateToken, async (req, res) => {
|
||||
.json({ error: "Only owners can mark rentals as completed" });
|
||||
}
|
||||
|
||||
if (rental.status !== "active") {
|
||||
if (!isActive(rental)) {
|
||||
return res.status(400).json({
|
||||
error: "Can only mark active rentals as completed",
|
||||
});
|
||||
@@ -1197,7 +1214,7 @@ router.post("/:id/mark-return", authenticateToken, async (req, res, next) => {
|
||||
.json({ error: "Only the item owner can mark return status" });
|
||||
}
|
||||
|
||||
if (rental.status !== "active") {
|
||||
if (!isActive(rental)) {
|
||||
return res.status(400).json({
|
||||
error: "Can only mark return status for active rentals",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user