pricing tiers
This commit is contained in:
@@ -3,6 +3,7 @@ const { Op } = require("sequelize");
|
||||
const { Rental, Item, User } = require("../models"); // Import from models/index.js to get models with associations
|
||||
const { authenticateToken, requireVerifiedEmail } = require("../middleware/auth");
|
||||
const FeeCalculator = require("../utils/feeCalculator");
|
||||
const RentalDurationCalculator = require("../utils/rentalDurationCalculator");
|
||||
const RefundService = require("../services/refundService");
|
||||
const LateReturnService = require("../services/lateReturnService");
|
||||
const DamageAssessmentService = require("../services/damageAssessmentService");
|
||||
@@ -201,19 +202,12 @@ router.post("/", authenticateToken, requireVerifiedEmail, async (req, res) => {
|
||||
rentalStartDateTime = new Date(startDateTime);
|
||||
rentalEndDateTime = new Date(endDateTime);
|
||||
|
||||
// Calculate rental duration
|
||||
const diffMs = rentalEndDateTime.getTime() - rentalStartDateTime.getTime();
|
||||
const diffHours = Math.ceil(diffMs / (1000 * 60 * 60));
|
||||
const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
|
||||
|
||||
// Calculate base amount based on duration
|
||||
if (item.pricePerHour && diffHours <= 24) {
|
||||
totalAmount = diffHours * Number(item.pricePerHour);
|
||||
} else if (item.pricePerDay) {
|
||||
totalAmount = diffDays * Number(item.pricePerDay);
|
||||
} else {
|
||||
totalAmount = 0;
|
||||
}
|
||||
// Calculate rental cost using duration calculator
|
||||
totalAmount = RentalDurationCalculator.calculateRentalCost(
|
||||
rentalStartDateTime,
|
||||
rentalEndDateTime,
|
||||
item
|
||||
);
|
||||
|
||||
// Check for overlapping rentals using datetime ranges
|
||||
const overlappingRental = await Rental.findOne({
|
||||
@@ -884,6 +878,60 @@ router.post("/calculate-fees", authenticateToken, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Preview rental cost calculation (no rental creation)
|
||||
router.post("/cost-preview", authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const { itemId, startDateTime, endDateTime } = req.body;
|
||||
|
||||
// Validate inputs
|
||||
if (!itemId || !startDateTime || !endDateTime) {
|
||||
return res.status(400).json({
|
||||
error: "itemId, startDateTime, and endDateTime are required",
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch item
|
||||
const item = await Item.findByPk(itemId);
|
||||
if (!item) {
|
||||
return res.status(404).json({ error: "Item not found" });
|
||||
}
|
||||
|
||||
// Parse datetimes
|
||||
const rentalStartDateTime = new Date(startDateTime);
|
||||
const rentalEndDateTime = new Date(endDateTime);
|
||||
|
||||
// Validate date range
|
||||
if (rentalEndDateTime <= rentalStartDateTime) {
|
||||
return res.status(400).json({
|
||||
error: "End date/time must be after start date/time",
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate rental cost using duration calculator
|
||||
const totalAmount = RentalDurationCalculator.calculateRentalCost(
|
||||
rentalStartDateTime,
|
||||
rentalEndDateTime,
|
||||
item
|
||||
);
|
||||
|
||||
// Calculate fees
|
||||
const fees = FeeCalculator.calculateRentalFees(totalAmount);
|
||||
|
||||
res.json({
|
||||
baseAmount: totalAmount,
|
||||
fees,
|
||||
});
|
||||
} catch (error) {
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Error calculating rental cost preview", {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
userId: req.user.id,
|
||||
});
|
||||
res.status(500).json({ error: "Failed to calculate rental cost preview" });
|
||||
}
|
||||
});
|
||||
|
||||
// Get earnings status for owner's rentals
|
||||
router.get("/earnings/status", authenticateToken, async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user