bulk-pdf for unique email addresses
This commit is contained in:
@@ -90,12 +90,38 @@ const CourseConsolidated = () => {
|
|||||||
{ key: "pwdPaperSettingTeachers", role: "PwD Paper Setter" },
|
{ key: "pwdPaperSettingTeachers", role: "PwD Paper Setter" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// ✅ NEW: Group appointments by Faculty ID
|
||||||
|
const facultyMap = {};
|
||||||
|
|
||||||
for (const { key, role } of roles) {
|
for (const { key, role } of roles) {
|
||||||
if (!courseData[key] || courseData[key].length === 0) continue; // Skip empty roles
|
if (!courseData[key] || courseData[key].length === 0) continue;
|
||||||
|
|
||||||
|
for (const teacher of courseData[key]) {
|
||||||
|
if (!facultyMap[teacher.facultyId]) {
|
||||||
|
facultyMap[teacher.facultyId] = {
|
||||||
|
facultyName: teacher.facultyName,
|
||||||
|
email: await fetchFacultyEmail(teacher.facultyId),
|
||||||
|
appointments: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
facultyMap[teacher.facultyId].appointments.push({
|
||||||
|
role,
|
||||||
|
teacher,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ NEW: For each Faculty, generate PDFs & send one email
|
||||||
|
for (const facultyId in facultyMap) {
|
||||||
|
const faculty = facultyMap[facultyId];
|
||||||
|
const pdfBlobs = [];
|
||||||
|
|
||||||
|
for (const appointment of faculty.appointments) {
|
||||||
|
const { role } = appointment;
|
||||||
|
|
||||||
const doc = new jsPDF();
|
const doc = new jsPDF();
|
||||||
|
|
||||||
// College Logo
|
// Add Logo and Title (your original PDF content)
|
||||||
const logoUrl = "/logo.png";
|
const logoUrl = "/logo.png";
|
||||||
const loadImage = async (url) => {
|
const loadImage = async (url) => {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
@@ -115,105 +141,62 @@ const CourseConsolidated = () => {
|
|||||||
console.error("Failed to load logo:", error);
|
console.error("Failed to load logo:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Title Section
|
|
||||||
doc.setFont("times", "normal");
|
doc.setFont("times", "normal");
|
||||||
doc.setTextColor(0, 0, 0);
|
doc.text(`Date: ${new Date().toLocaleDateString()}`, 150, 20);
|
||||||
doc.setFontSize(12);
|
|
||||||
doc.text("Date: " + new Date().toLocaleDateString(), 150, 20);
|
|
||||||
doc.setFontSize(14);
|
|
||||||
doc.text("CONFIDENTIAL", 10, 60);
|
doc.text("CONFIDENTIAL", 10, 60);
|
||||||
doc.setFontSize(10);
|
doc.text(`LETTER OF APPOINTMENT AS ${role.toUpperCase()}`, 105, 70, { align: "center" });
|
||||||
doc.text(`LETTER OF APPOINTMENT AS ${role.toUpperCase()}`, 105, 70, {
|
|
||||||
align: "center",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch Emails and Prepare Table Data
|
|
||||||
let table1Data = [];
|
|
||||||
let emails = [];
|
|
||||||
|
|
||||||
for (const teacher of courseData[key]) {
|
|
||||||
const email = await fetchFacultyEmail(teacher.facultyId);
|
|
||||||
emails.push(email);
|
|
||||||
table1Data.push([
|
|
||||||
teacher.facultyName,
|
|
||||||
"K. J. Somaiya School of Engineering",
|
|
||||||
role,
|
|
||||||
email,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate Table
|
|
||||||
autoTable(doc, {
|
autoTable(doc, {
|
||||||
head: [["Name", "Affiliation", "Appointment Role", "Email"]],
|
head: [["Name", "Affiliation", "Appointment Role", "Email"]],
|
||||||
body: table1Data,
|
body: [[faculty.facultyName, "K. J. Somaiya School of Engineering", role, faculty.email]],
|
||||||
startY: 80,
|
startY: 80,
|
||||||
theme: "grid",
|
theme: "grid",
|
||||||
headStyles: { fillColor: maroon, textColor: [255, 255, 255] },
|
headStyles: { fillColor: maroon, textColor: [255, 255, 255] },
|
||||||
styles: { textColor: [0, 0, 0] },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Content Table
|
autoTable(doc, {
|
||||||
const detailsTableData = [
|
body: [
|
||||||
["Programme:", courseData.courseName],
|
["Programme:", courseData.courseName],
|
||||||
["Exam Category:", "Regular Examination"],
|
["Exam Category:", "Regular Examination"],
|
||||||
["Exam Type:", courseData.examType],
|
["Exam Type:", courseData.examType],
|
||||||
["Exam Season:", "Second Half - Winter Examination 2023"],
|
["Exam Season:", "Second Half - Winter Examination 2023"],
|
||||||
["Number of Sets Required:", courseData.paperSettingTeachers.length],
|
|
||||||
["Year:", courseData.year],
|
["Year:", courseData.year],
|
||||||
["Semester:", courseData.semester],
|
["Semester:", courseData.semester],
|
||||||
["Course Name:", courseName],
|
["Course Name:", courseName],
|
||||||
["Course Code:", courseData.courseCode],
|
["Course Code:", courseData.courseCode],
|
||||||
];
|
],
|
||||||
|
|
||||||
autoTable(doc, {
|
|
||||||
body: detailsTableData,
|
|
||||||
startY: doc.previousAutoTable.finalY + 10,
|
startY: doc.previousAutoTable.finalY + 10,
|
||||||
theme: "grid",
|
theme: "grid",
|
||||||
styles: { textColor: [0, 0, 0] },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Footer
|
|
||||||
const footerY = doc.previousAutoTable.finalY + 10;
|
|
||||||
doc.setFontSize(12);
|
|
||||||
doc.text("Dr. S. K. Ukarande", 10, footerY);
|
|
||||||
doc.text("Principal", 10, footerY + 5);
|
|
||||||
doc.text("K. J. Somaiya School of Engineering", 10, footerY + 10);
|
|
||||||
|
|
||||||
const footerContactY = footerY + 20;
|
|
||||||
doc.setFontSize(10);
|
|
||||||
doc.text(
|
|
||||||
"Somaiya Vidyavihar, Vidyavihar (East), Mumbai-400 022, India",
|
|
||||||
10,
|
|
||||||
footerContactY
|
|
||||||
);
|
|
||||||
doc.text("Telephone: (91-22) 44444400, 44444404", 10, footerContactY + 5);
|
|
||||||
doc.text("Email: principal.tech@somaiya.edu", 10, footerContactY + 10);
|
|
||||||
doc.text("Web: www.somaiya.edu/kjsieit", 10, footerContactY + 15);
|
|
||||||
|
|
||||||
// Convert PDF to Blob
|
|
||||||
const pdfBlob = doc.output("blob");
|
const pdfBlob = doc.output("blob");
|
||||||
|
pdfBlobs.push({ blob: pdfBlob, filename: `${courseName}-${role}.pdf` });
|
||||||
|
}
|
||||||
|
|
||||||
// Send Email to Each Faculty
|
// ✅ NEW: Send ONE email with ALL PDFs to the Faculty
|
||||||
for (const email of emails) {
|
|
||||||
try {
|
try {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("pdfFile", pdfBlob, `${courseName}-${role}.pdf`);
|
|
||||||
formData.append("fileName", `${courseName}-${role}.pdf`);
|
pdfBlobs.forEach(({ blob, filename }) => {
|
||||||
formData.append("recipientEmail", email);
|
formData.append("pdfFiles", blob, filename); // note 'pdfFiles' is plural
|
||||||
formData.append("role", role);
|
});
|
||||||
|
|
||||||
|
formData.append("recipientEmail", faculty.email);
|
||||||
|
formData.append("facultyName", faculty.facultyName);
|
||||||
formData.append("courseName", courseName);
|
formData.append("courseName", courseName);
|
||||||
|
|
||||||
const emailResponse = await sendEmail(formData, "pdf");
|
const emailResponse = await sendEmail(formData, "bulk-pdf");
|
||||||
toast.success(`✅ Email sent successfully to ${email}`);
|
toast.success(`✅ Email sent successfully to ${faculty.email}`);
|
||||||
console.log("Response from server:", emailResponse);
|
console.log("Response:", emailResponse);
|
||||||
} catch (emailError) {
|
} catch (error) {
|
||||||
toast.error(`❌ Error sending email to ${email}:`, emailError.message);
|
toast.error(`❌ Error sending email to ${faculty.email}: ${error.message}`);
|
||||||
}
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Navbar />
|
<Navbar />
|
||||||
|
|||||||
@@ -4,19 +4,31 @@ const fs = require("fs");
|
|||||||
const multer = require("multer");
|
const multer = require("multer");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Setup multer for handling file uploads
|
// Multer setup remains intact
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
destination: function (req, file, cb) {
|
destination: function (req, file, cb) {
|
||||||
cb(null, "./"); // Save to the current directory (you can customize this)
|
cb(null, "./"); // Customize directory if needed
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
cb(null, file.originalname); // Use the original file name
|
cb(null, file.originalname);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const upload = multer({ storage });
|
const upload = multer({ storage });
|
||||||
|
|
||||||
// Route to handle email sending with file attachment
|
// Single transporter setup clearly moved to top (no duplication)
|
||||||
|
const transporter = nodemailer.createTransport({
|
||||||
|
service: "gmail",
|
||||||
|
auth: {
|
||||||
|
user: "swdc.ate@gmail.com",
|
||||||
|
pass: "umlc hbkr dpga iywd",
|
||||||
|
},
|
||||||
|
tls: { rejectUnauthorized: false },
|
||||||
|
connectionTimeout: 30000,
|
||||||
|
greetingTimeout: 30000,
|
||||||
|
socketTimeout: 30000,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Existing Excel route unchanged, except transporter removal
|
||||||
router.post("/excel", upload.single("file"), async (req, res) => {
|
router.post("/excel", upload.single("file"), async (req, res) => {
|
||||||
const { teacher, fileName, recipientEmail } = req.body;
|
const { teacher, fileName, recipientEmail } = req.body;
|
||||||
|
|
||||||
@@ -24,24 +36,8 @@ router.post("/excel", upload.single("file"), async (req, res) => {
|
|||||||
return res.status(400).json({ error: "Missing required fields or file" });
|
return res.status(400).json({ error: "Missing required fields or file" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure Nodemailer transporter
|
|
||||||
const transporter = nodemailer.createTransport({
|
|
||||||
service: "gmail",
|
|
||||||
auth: {
|
|
||||||
user: "swdc.ate@gmail.com", // Replace with your email
|
|
||||||
pass: "umlc hbkr dpga iywd", // Replace with your app-specific password or token
|
|
||||||
},
|
|
||||||
tls: {
|
|
||||||
rejectUnauthorized: false, // Ignore self-signed certificate errors
|
|
||||||
},
|
|
||||||
connectionTimeout: 15000,
|
|
||||||
greetingTimeout: 15000,
|
|
||||||
socketTimeout: 15000,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Email options
|
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
from: "SWDC Admin <swdc.ate@gmail.com>", // Replace with your email
|
from: "SWDC Admin <swdc.ate@gmail.com>",
|
||||||
to: recipientEmail,
|
to: recipientEmail,
|
||||||
subject: `Examination Appointments for ${teacher}`,
|
subject: `Examination Appointments for ${teacher}`,
|
||||||
text: `Dear Sir/Madam,
|
text: `Dear Sir/Madam,
|
||||||
@@ -50,27 +46,18 @@ Please find attached the Excel sheet regarding various appointments for the May-
|
|||||||
|
|
||||||
Note: Kindly download the Excel sheet to view the hidden columns referring to the Scheme and other information.
|
Note: Kindly download the Excel sheet to view the hidden columns referring to the Scheme and other information.
|
||||||
|
|
||||||
|
|
||||||
If you have any queries, please contact the H.O.D or the Examination In-charge.
|
If you have any queries, please contact the H.O.D or the Examination In-charge.
|
||||||
|
|
||||||
Your cooperation regarding the upcoming examination duties is highly solicited.`,
|
Your cooperation regarding the upcoming examination duties is highly solicited.`,
|
||||||
attachments: [
|
attachments: [{ filename: fileName, path: req.file.path }],
|
||||||
{
|
|
||||||
filename: fileName,
|
|
||||||
path: req.file.path, // Use the uploaded file's path
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send email
|
|
||||||
await transporter.sendMail(mailOptions);
|
await transporter.sendMail(mailOptions);
|
||||||
transporter.close();
|
transporter.close();
|
||||||
|
|
||||||
// Delete the temporary file after sending the email
|
// Delete the temporary file after sending the email
|
||||||
fs.unlinkSync(req.file.path);
|
fs.unlinkSync(req.file.path);
|
||||||
|
|
||||||
res.status(200).json({ message: "Email sent successfully" });
|
res.status(200).json({ message: "Email sent successfully" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error sending email:", error);
|
console.error("Error sending email:", error);
|
||||||
@@ -78,7 +65,7 @@ Your cooperation regarding the upcoming examination duties is highly solicited.`
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Route to send emails with PDF attachments
|
// Route to send emails with single PDF attachment remains intact
|
||||||
router.post("/pdf", upload.single("pdfFile"), async (req, res) => {
|
router.post("/pdf", upload.single("pdfFile"), async (req, res) => {
|
||||||
const { fileName, recipientEmail, role, courseName } = req.body;
|
const { fileName, recipientEmail, role, courseName } = req.body;
|
||||||
|
|
||||||
@@ -86,22 +73,6 @@ router.post("/pdf", upload.single("pdfFile"), async (req, res) => {
|
|||||||
return res.status(400).json({ error: "Missing required fields or file" });
|
return res.status(400).json({ error: "Missing required fields or file" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure Nodemailer transporter
|
|
||||||
const transporter = nodemailer.createTransport({
|
|
||||||
service: "gmail",
|
|
||||||
auth: {
|
|
||||||
user: "swdc.ate@gmail.com", // Replace with your email
|
|
||||||
pass: "umlc hbkr dpga iywd", // Replace with your app-specific password
|
|
||||||
},
|
|
||||||
tls: {
|
|
||||||
rejectUnauthorized: false,
|
|
||||||
},
|
|
||||||
connectionTimeout: 15000,
|
|
||||||
greetingTimeout: 15000,
|
|
||||||
socketTimeout: 15000,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Email options
|
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
from: "SWDC Admin <swdc.ate@gmail.com>",
|
from: "SWDC Admin <swdc.ate@gmail.com>",
|
||||||
to: recipientEmail,
|
to: recipientEmail,
|
||||||
@@ -115,22 +86,14 @@ If you have any queries, please contact the Examination In-charge.
|
|||||||
|
|
||||||
Best regards,
|
Best regards,
|
||||||
Examination Department`,
|
Examination Department`,
|
||||||
attachments: [
|
attachments: [{ filename: fileName, path: req.file.path }],
|
||||||
{
|
|
||||||
filename: fileName,
|
|
||||||
path: req.file.path, // Path to the uploaded PDF
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send email
|
|
||||||
await transporter.sendMail(mailOptions);
|
await transporter.sendMail(mailOptions);
|
||||||
transporter.close();
|
transporter.close();
|
||||||
|
|
||||||
// Delete the PDF file after sending
|
fs.unlinkSync(req.file.path);
|
||||||
// fs.unlinkSync(req.file.path);
|
|
||||||
|
|
||||||
res.status(200).json({ message: "Email sent successfully" });
|
res.status(200).json({ message: "Email sent successfully" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error sending email:", error);
|
console.error("Error sending email:", error);
|
||||||
@@ -138,4 +101,59 @@ Examination Department`,
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ✅ New route clearly added to handle multiple PDF attachments
|
||||||
|
// ✅ Correct and clean bulk-pdf route clearly stated
|
||||||
|
router.post("/bulk-pdf", upload.array("pdfFiles"), async (req, res) => {
|
||||||
|
const { recipientEmail, facultyName, courseName } = req.body;
|
||||||
|
|
||||||
|
if (!recipientEmail || !facultyName || !courseName || !req.files || req.files.length === 0) {
|
||||||
|
return res.status(400).json({ error: "Missing required fields or files" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const attachments = req.files.map((file) => ({
|
||||||
|
filename: file.originalname,
|
||||||
|
path: file.path,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const mailOptions = {
|
||||||
|
from: "SWDC Admin <swdc.ate@gmail.com>",
|
||||||
|
to: recipientEmail,
|
||||||
|
subject: `Appointment Letters - ${courseName}`,
|
||||||
|
text: `Dear ${facultyName},
|
||||||
|
|
||||||
|
Please find your appointment letters attached.
|
||||||
|
|
||||||
|
If you have any queries, please contact the Examination In-charge.
|
||||||
|
|
||||||
|
Best regards,
|
||||||
|
Examination Department`,
|
||||||
|
attachments,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await transporter.sendMail(mailOptions);
|
||||||
|
transporter.close();
|
||||||
|
|
||||||
|
// ✅ Ensure files are deleted after successful sending
|
||||||
|
attachments.forEach((attachment) => {
|
||||||
|
if (fs.existsSync(attachment.path)) {
|
||||||
|
fs.unlinkSync(attachment.path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({ message: "Bulk email sent successfully" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error sending bulk email (backend):", error);
|
||||||
|
|
||||||
|
attachments.forEach((attachment) => {
|
||||||
|
if (fs.existsSync(attachment.path)) {
|
||||||
|
fs.unlinkSync(attachment.path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(500).json({ error: "Failed to send bulk email", details: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user