pricing tiers
This commit is contained in:
@@ -48,6 +48,12 @@ const Item = sequelize.define("Item", {
|
||||
pricePerDay: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
},
|
||||
pricePerWeek: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
},
|
||||
pricePerMonth: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
},
|
||||
replacementCost: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
|
||||
@@ -33,6 +33,12 @@ const ItemRequestResponse = sequelize.define('ItemRequestResponse', {
|
||||
offerPricePerDay: {
|
||||
type: DataTypes.DECIMAL(10, 2)
|
||||
},
|
||||
offerPricePerWeek: {
|
||||
type: DataTypes.DECIMAL(10, 2)
|
||||
},
|
||||
offerPricePerMonth: {
|
||||
type: DataTypes.DECIMAL(10, 2)
|
||||
},
|
||||
availableStartDate: {
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -25,33 +25,27 @@ class LateReturnService {
|
||||
|
||||
let lateFee = 0;
|
||||
let pricingType = "daily";
|
||||
const billableDays = Math.ceil(hoursLate / 24);
|
||||
|
||||
// Check if item has hourly or daily pricing
|
||||
if (rental.item?.pricePerHour && rental.item.pricePerHour > 0) {
|
||||
// Hourly pricing - charge per hour late
|
||||
lateFee = hoursLate * parseFloat(rental.item.pricePerHour);
|
||||
pricingType = "hourly";
|
||||
} else if (rental.item?.pricePerDay && rental.item.pricePerDay > 0) {
|
||||
// Daily pricing - charge per day late (rounded up)
|
||||
const billableDays = Math.ceil(hoursLate / 24);
|
||||
// Calculate late fees per day, deriving daily rate from available pricing tiers
|
||||
if (rental.item?.pricePerDay && rental.item.pricePerDay > 0) {
|
||||
// Daily pricing - charge per day late
|
||||
lateFee = billableDays * parseFloat(rental.item.pricePerDay);
|
||||
pricingType = "daily";
|
||||
} else if (rental.item?.pricePerWeek && rental.item.pricePerWeek > 0) {
|
||||
// Weekly pricing - derive daily rate and charge per day late
|
||||
const dailyRate = parseFloat(rental.item.pricePerWeek) / 7;
|
||||
lateFee = billableDays * dailyRate;
|
||||
} else if (rental.item?.pricePerMonth && rental.item.pricePerMonth > 0) {
|
||||
// Monthly pricing - derive daily rate and charge per day late
|
||||
const dailyRate = parseFloat(rental.item.pricePerMonth) / 30;
|
||||
lateFee = billableDays * dailyRate;
|
||||
} else if (rental.item?.pricePerHour && rental.item.pricePerHour > 0) {
|
||||
// Hourly pricing - derive daily rate and charge per day late
|
||||
const dailyRate = parseFloat(rental.item.pricePerHour) * 24;
|
||||
lateFee = billableDays * dailyRate;
|
||||
} else {
|
||||
// Free borrows: determine pricing type based on rental duration
|
||||
const rentalStart = new Date(rental.startDateTime);
|
||||
const rentalEnd = new Date(rental.endDateTime);
|
||||
const rentalDurationHours = (rentalEnd - rentalStart) / (1000 * 60 * 60);
|
||||
|
||||
if (rentalDurationHours <= 24) {
|
||||
// Hourly rental - charge $10 per hour late
|
||||
lateFee = hoursLate * 10.0;
|
||||
pricingType = "hourly";
|
||||
} else {
|
||||
// Daily rental - charge $10 per day late
|
||||
const billableDays = Math.ceil(hoursLate / 24);
|
||||
lateFee = billableDays * 10.0;
|
||||
pricingType = "daily";
|
||||
}
|
||||
// Free borrows - charge $10 per day late
|
||||
lateFee = billableDays * 10.0;
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
62
backend/utils/rentalDurationCalculator.js
Normal file
62
backend/utils/rentalDurationCalculator.js
Normal file
@@ -0,0 +1,62 @@
|
||||
class RentalDurationCalculator {
|
||||
/**
|
||||
* Calculate rental cost based on duration and item pricing tiers
|
||||
* @param {Date|string} startDateTime - Rental start date/time
|
||||
* @param {Date|string} endDateTime - Rental end date/time
|
||||
* @param {Object} item - Item object with pricing information
|
||||
* @returns {number} Total rental cost
|
||||
*/
|
||||
static calculateRentalCost(startDateTime, endDateTime, item) {
|
||||
const rentalStartDateTime = new Date(startDateTime);
|
||||
const 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));
|
||||
const diffWeeks = Math.ceil(diffDays / 7);
|
||||
|
||||
// Calculate difference in calendar months
|
||||
let diffMonths = (rentalEndDateTime.getFullYear() - rentalStartDateTime.getFullYear()) * 12;
|
||||
diffMonths += rentalEndDateTime.getMonth() - rentalStartDateTime.getMonth();
|
||||
|
||||
// Add 1 if we're past the start day/time in the end month
|
||||
if (rentalEndDateTime.getDate() > rentalStartDateTime.getDate()) {
|
||||
diffMonths += 1;
|
||||
} else if (rentalEndDateTime.getDate() === rentalStartDateTime.getDate() &&
|
||||
rentalEndDateTime.getTime() > rentalStartDateTime.getTime()) {
|
||||
diffMonths += 1;
|
||||
}
|
||||
|
||||
diffMonths = Math.max(1, diffMonths);
|
||||
|
||||
// Calculate base amount based on duration (tiered pricing)
|
||||
let totalAmount;
|
||||
|
||||
if (item.pricePerHour && diffHours <= 24) {
|
||||
// Use hourly rate for rentals <= 24 hours
|
||||
totalAmount = diffHours * Number(item.pricePerHour);
|
||||
} else if (diffDays <= 7 && item.pricePerDay) {
|
||||
// Use daily rate for rentals <= 7 days
|
||||
totalAmount = diffDays * Number(item.pricePerDay);
|
||||
} else if (diffMonths <= 1 && item.pricePerWeek) {
|
||||
// Use weekly rate for rentals <= 1 calendar month
|
||||
totalAmount = diffWeeks * Number(item.pricePerWeek);
|
||||
} else if (diffMonths > 1 && item.pricePerMonth) {
|
||||
// Use monthly rate for rentals > 1 calendar month
|
||||
totalAmount = diffMonths * Number(item.pricePerMonth);
|
||||
} else if (item.pricePerWeek) {
|
||||
// Fallback to weekly rate if monthly not available
|
||||
totalAmount = diffWeeks * Number(item.pricePerWeek);
|
||||
} else if (item.pricePerDay) {
|
||||
// Fallback to daily rate
|
||||
totalAmount = diffDays * Number(item.pricePerDay);
|
||||
} else {
|
||||
totalAmount = 0;
|
||||
}
|
||||
|
||||
return totalAmount;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RentalDurationCalculator;
|
||||
Reference in New Issue
Block a user