emails for rental cancelation, rental declined, rental request confirmation, payout received
This commit is contained in:
@@ -296,12 +296,30 @@ router.post("/", authenticateToken, requireVerifiedEmail, async (req, res) => {
|
||||
// Log error but don't fail the request
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Failed to send rental request email", {
|
||||
error: emailError.message,
|
||||
error: emailError,
|
||||
rentalId: rental.id,
|
||||
ownerId: rentalWithDetails.ownerId,
|
||||
});
|
||||
}
|
||||
|
||||
// Send rental request confirmation to renter
|
||||
try {
|
||||
await emailService.sendRentalRequestConfirmationEmail(rentalWithDetails);
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.info("Rental request confirmation sent to renter", {
|
||||
rentalId: rental.id,
|
||||
renterId: rentalWithDetails.renterId,
|
||||
});
|
||||
} catch (emailError) {
|
||||
// Log error but don't fail the request
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Failed to send rental request confirmation email", {
|
||||
error: emailError,
|
||||
rentalId: rental.id,
|
||||
renterId: rentalWithDetails.renterId,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(201).json(rentalWithDetails);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: "Failed to create rental" });
|
||||
@@ -477,6 +495,105 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Decline rental request (owner only)
|
||||
router.put("/:id/decline", authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const { reason } = req.body;
|
||||
|
||||
// Validate that reason is provided
|
||||
if (!reason || reason.trim() === "") {
|
||||
return res.status(400).json({
|
||||
error: "A reason for declining is required",
|
||||
});
|
||||
}
|
||||
|
||||
const rental = await Rental.findByPk(req.params.id, {
|
||||
include: [
|
||||
{ model: Item, as: "item" },
|
||||
{
|
||||
model: User,
|
||||
as: "owner",
|
||||
attributes: ["id", "username", "firstName", "lastName"],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: "renter",
|
||||
attributes: ["id", "username", "firstName", "lastName", "email"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!rental) {
|
||||
return res.status(404).json({ error: "Rental not found" });
|
||||
}
|
||||
|
||||
// Only owner can decline
|
||||
if (rental.ownerId !== req.user.id) {
|
||||
return res
|
||||
.status(403)
|
||||
.json({ error: "Only the item owner can decline rental requests" });
|
||||
}
|
||||
|
||||
// Can only decline pending rentals
|
||||
if (rental.status !== "pending") {
|
||||
return res.status(400).json({
|
||||
error: "Can only decline pending rental requests",
|
||||
});
|
||||
}
|
||||
|
||||
// Update rental status to declined
|
||||
await rental.update({
|
||||
status: "declined",
|
||||
declineReason: reason,
|
||||
});
|
||||
|
||||
const updatedRental = await Rental.findByPk(rental.id, {
|
||||
include: [
|
||||
{ model: Item, as: "item" },
|
||||
{
|
||||
model: User,
|
||||
as: "owner",
|
||||
attributes: ["id", "username", "firstName", "lastName"],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: "renter",
|
||||
attributes: ["id", "username", "firstName", "lastName"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Send decline notification email to renter
|
||||
try {
|
||||
await emailService.sendRentalDeclinedEmail(updatedRental, reason);
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.info("Rental decline notification sent to renter", {
|
||||
rentalId: rental.id,
|
||||
renterId: updatedRental.renterId,
|
||||
});
|
||||
} catch (emailError) {
|
||||
// Log error but don't fail the request
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Failed to send rental decline email", {
|
||||
error: emailError,
|
||||
rentalId: rental.id,
|
||||
renterId: updatedRental.renterId,
|
||||
});
|
||||
}
|
||||
|
||||
res.json(updatedRental);
|
||||
} catch (error) {
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Error declining rental", {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
rentalId: req.params.id,
|
||||
userId: req.user.id,
|
||||
});
|
||||
res.status(500).json({ error: "Failed to decline rental" });
|
||||
}
|
||||
});
|
||||
|
||||
// Owner reviews renter
|
||||
router.post("/:id/review-renter", authenticateToken, async (req, res) => {
|
||||
try {
|
||||
@@ -738,10 +855,15 @@ router.post("/:id/cancel", authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const { reason } = req.body;
|
||||
|
||||
// Validate that reason is provided
|
||||
if (!reason || !reason.trim()) {
|
||||
return res.status(400).json({ error: "Cancellation reason is required" });
|
||||
}
|
||||
|
||||
const result = await RefundService.processCancellation(
|
||||
req.params.id,
|
||||
req.user.id,
|
||||
reason
|
||||
reason.trim()
|
||||
);
|
||||
|
||||
// Return the updated rental with refund information
|
||||
@@ -761,6 +883,27 @@ router.post("/:id/cancel", authenticateToken, async (req, res) => {
|
||||
],
|
||||
});
|
||||
|
||||
// Send cancellation notification emails
|
||||
try {
|
||||
await emailService.sendRentalCancellationEmails(
|
||||
updatedRental,
|
||||
result.refund
|
||||
);
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.info("Cancellation emails sent", {
|
||||
rentalId: updatedRental.id,
|
||||
cancelledBy: updatedRental.cancelledBy,
|
||||
});
|
||||
} catch (emailError) {
|
||||
// Log error but don't fail the request
|
||||
const reqLogger = logger.withRequestId(req.id);
|
||||
reqLogger.error("Failed to send cancellation emails", {
|
||||
error: emailError.message,
|
||||
rentalId: updatedRental.id,
|
||||
cancelledBy: updatedRental.cancelledBy,
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
rental: updatedRental,
|
||||
refund: result.refund,
|
||||
|
||||
Reference in New Issue
Block a user