started payouts

This commit is contained in:
jackiettran
2025-08-29 00:32:06 -04:00
parent 0f04182768
commit b52104c3fa
13 changed files with 578 additions and 252 deletions

View File

@@ -2,6 +2,7 @@ const express = require("express");
const { Op } = require("sequelize");
const { Rental, Item, User } = require("../models"); // Import from models/index.js to get models with associations
const { authenticateToken } = require("../middleware/auth");
const FeeCalculator = require("../utils/feeCalculator");
const router = express.Router();
// Helper function to check and update review visibility
@@ -139,7 +140,10 @@ router.post("/", authenticateToken, async (req, res) => {
const rentalDays = Math.ceil(
(new Date(endDate) - new Date(startDate)) / (1000 * 60 * 60 * 24)
);
const totalAmount = rentalDays * (item.pricePerDay || 0);
const baseRentalAmount = rentalDays * (item.pricePerDay || 0);
// Calculate fees using FeeCalculator
const fees = FeeCalculator.calculateRentalFees(baseRentalAmount);
const rental = await Rental.create({
itemId,
@@ -149,7 +153,11 @@ router.post("/", authenticateToken, async (req, res) => {
endDate,
startTime,
endTime,
totalAmount,
totalAmount: fees.totalChargedAmount,
baseRentalAmount: fees.baseRentalAmount,
platformFee: fees.platformFee,
processingFee: fees.processingFee,
payoutAmount: fees.payoutAmount,
deliveryMethod,
deliveryAddress,
notes,
@@ -376,4 +384,54 @@ router.post("/:id/review", authenticateToken, async (req, res) => {
}
});
// Calculate fees for rental pricing display
router.post("/calculate-fees", authenticateToken, async (req, res) => {
try {
const { baseAmount } = req.body;
if (!baseAmount || baseAmount <= 0) {
return res.status(400).json({ error: "Valid base amount is required" });
}
const fees = FeeCalculator.calculateRentalFees(baseAmount);
const displayFees = FeeCalculator.formatFeesForDisplay(fees);
res.json({
fees,
display: displayFees,
});
} catch (error) {
console.error("Error calculating fees:", error);
res.status(500).json({ error: error.message });
}
});
// Get payout status for owner's rentals
router.get("/payouts/status", authenticateToken, async (req, res) => {
try {
const ownerRentals = await Rental.findAll({
where: {
ownerId: req.user.id,
status: "completed",
},
attributes: [
"id",
"baseRentalAmount",
"platformFee",
"payoutAmount",
"payoutStatus",
"payoutProcessedAt",
"stripeTransferId",
],
include: [{ model: Item, as: "item", attributes: ["name"] }],
order: [["createdAt", "DESC"]],
});
res.json(ownerRentals);
} catch (error) {
console.error("Error getting payout status:", error);
res.status(500).json({ error: error.message });
}
});
module.exports = router;