date time validation and added ability to type in date

This commit is contained in:
jackiettran
2026-01-01 00:50:19 -05:00
parent f66dccdfa3
commit 3d0e553620
6 changed files with 711 additions and 410 deletions

View File

@@ -207,6 +207,26 @@ router.post("/", authenticateToken, requireVerifiedEmail, async (req, res) => {
rentalStartDateTime = new Date(startDateTime);
rentalEndDateTime = new Date(endDateTime);
// Validate date formats
if (isNaN(rentalStartDateTime.getTime())) {
return res.status(400).json({ error: "Invalid start date format" });
}
if (isNaN(rentalEndDateTime.getTime())) {
return res.status(400).json({ error: "Invalid end date format" });
}
// Validate start date is not in the past (with 5 minute grace period for form submission)
const now = new Date();
const fiveMinutesAgo = new Date(now.getTime() - 5 * 60 * 1000);
if (rentalStartDateTime < fiveMinutesAgo) {
return res.status(400).json({ error: "Start date cannot be in the past" });
}
// Validate end date/time is after start date/time
if (rentalEndDateTime <= rentalStartDateTime) {
return res.status(400).json({ error: "End date/time must be after start date/time" });
}
// Calculate rental cost using duration calculator
totalAmount = RentalDurationCalculator.calculateRentalCost(
rentalStartDateTime,
@@ -937,10 +957,25 @@ router.post("/cost-preview", authenticateToken, async (req, res) => {
const rentalStartDateTime = new Date(startDateTime);
const rentalEndDateTime = new Date(endDateTime);
// Validate date formats
if (isNaN(rentalStartDateTime.getTime())) {
return res.status(400).json({ error: "Invalid start date format" });
}
if (isNaN(rentalEndDateTime.getTime())) {
return res.status(400).json({ error: "Invalid end date format" });
}
// Validate start date is not in the past (with 5 minute grace period)
const now = new Date();
const fiveMinutesAgo = new Date(now.getTime() - 5 * 60 * 1000);
if (rentalStartDateTime < fiveMinutesAgo) {
return res.status(400).json({ error: "Start date cannot be in the past" });
}
// Validate date range
if (rentalEndDateTime <= rentalStartDateTime) {
return res.status(400).json({
error: "End must be after start",
error: "End date/time must be after start date/time",
});
}