-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #890 from haseebzaki-07/new_branch_7
Add email rental confirmation
- Loading branch information
Showing
5 changed files
with
226 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
const nodemailer = require('nodemailer'); | ||
|
||
const sendOrderConfirmationEmail = async (userDetails, cartItems, totalPrice, trackingID) => { | ||
// Create a transporter using Gmail's SMTP server | ||
const transporter = nodemailer.createTransport({ | ||
service: 'gmail', // Use your email service provider (e.g., 'gmail', 'smtp.mailtrap.io', etc.) | ||
auth: { | ||
user: process.env.EMAIL_USER, // Replace with your Gmail email address | ||
pass: process.env.EMAIL_PASS, // Replace with your Gmail password or App Password | ||
}, | ||
}); | ||
|
||
// Define the email details | ||
const mailOptions = { | ||
from: '[email protected]', // Sender's email | ||
to: userDetails.email, // Recipient's email | ||
subject: 'Order Confirmation', | ||
html: ` | ||
<h1>Order Confirmation</h1> | ||
<p>Thank you for your order, ${userDetails.name}!</p> | ||
<h3>Your Order Details</h3> | ||
<ul> | ||
${cartItems | ||
.map( | ||
(item) => ` | ||
<li> | ||
<strong>${item.name}</strong> - ${item.quantity} x ₹${item.price} = ₹${item.quantity * item.price} | ||
</li>` | ||
) | ||
.join('')} | ||
</ul> | ||
<h3>Total Price: ₹${totalPrice}</h3> | ||
<p>Your tracking ID: <strong>${trackingID}</strong></p> | ||
<p>Your order will be processed shortly. Thank you!</p> | ||
`, | ||
}; | ||
|
||
// Send the email | ||
try { | ||
await transporter.sendMail(mailOptions); | ||
console.log('Confirmation email sent successfully.'); | ||
} catch (error) { | ||
console.error('Error sending email:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
exports.OrderConfirmation = async (req, res) => { | ||
try { | ||
const { userDetails, cartItems, totalPrice } = req.body; | ||
|
||
// Generate a random tracking ID | ||
const trackingID = 'TRK' + Math.random().toString(36).substr(2, 9).toUpperCase(); | ||
|
||
// Send the confirmation email | ||
await sendOrderConfirmationEmail(userDetails, cartItems, totalPrice, trackingID); | ||
|
||
res.status(200).json({ | ||
message: 'Order confirmed! A confirmation email has been sent.', | ||
trackingID, | ||
}); | ||
} catch (error) { | ||
console.error('Error sending email:', error); | ||
res.status(500).json({ message: 'Failed to send confirmation email.' }); | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// routes/productRoutes.js | ||
|
||
const express = require('express'); | ||
const { OrderConfirmation } = require('../../controllers/rent/RentOrderController'); | ||
const router = express.Router(); | ||
|
||
|
||
// Create a new product | ||
router.post('/rent-product', OrderConfirmation); | ||
|
||
|
||
|
||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters