auto email

This commit is contained in:
amNobodyyy
2025-01-24 01:11:21 +05:30
parent 2841638ba5
commit add7192ccf
2 changed files with 115 additions and 105 deletions

View File

@@ -33,7 +33,7 @@ const ConsolidatedTable = () => {
const createExcelFile = (teacherData, teacherName) => { const createExcelFile = (teacherData, teacherName) => {
const workbook = XLSX.utils.book_new(); const workbook = XLSX.utils.book_new();
// Define header information // Define header information
const headerInfo = [ const headerInfo = [
["Somaiya Vidyavihar University"], ["Somaiya Vidyavihar University"],
@@ -45,7 +45,7 @@ const ConsolidatedTable = () => {
["Department - Computer Engineering"], ["Department - Computer Engineering"],
[], [],
]; ];
const tableHeaders = [ const tableHeaders = [
[ [
"Sr No", "Sr No",
@@ -55,9 +55,7 @@ const ConsolidatedTable = () => {
"Exam Type", "Exam Type",
"Year", "Year",
"Marks", "Marks",
"Surname", "Name",
"First Name",
"Middle Name",
"Affiliation/College", "Affiliation/College",
"Highest Qualification", "Highest Qualification",
"Career Experience", "Career Experience",
@@ -69,7 +67,7 @@ const ConsolidatedTable = () => {
"PwD Paper Setting", "PwD Paper Setting",
], ],
]; ];
const dataRows = teacherData.map((row, index) => [ const dataRows = teacherData.map((row, index) => [
index + 1, index + 1,
row.semester, row.semester,
@@ -78,9 +76,7 @@ const ConsolidatedTable = () => {
row.examType, row.examType,
row.year, row.year,
row.marks, row.marks,
row.surname, row.Name,
row.firstName,
row.middleName,
row.affiliation, row.affiliation,
row.qualification, row.qualification,
row.experience, row.experience,
@@ -91,10 +87,10 @@ const ConsolidatedTable = () => {
row.moderation, row.moderation,
row.pwdPaperSetting, row.pwdPaperSetting,
]); ]);
const sheetData = [...headerInfo, ...tableHeaders, ...dataRows]; const sheetData = [...headerInfo, ...tableHeaders, ...dataRows];
const worksheet = XLSX.utils.aoa_to_sheet(sheetData); const worksheet = XLSX.utils.aoa_to_sheet(sheetData);
// Add merged cells // Add merged cells
worksheet["!merges"] = [ worksheet["!merges"] = [
{ s: { r: 0, c: 0 }, e: { r: 0, c: 18 } }, { s: { r: 0, c: 0 }, e: { r: 0, c: 18 } },
@@ -103,22 +99,27 @@ const ConsolidatedTable = () => {
{ s: { r: 3, c: 0 }, e: { r: 3, c: 18 } }, { s: { r: 3, c: 0 }, e: { r: 3, c: 18 } },
{ s: { r: 4, c: 0 }, e: { r: 4, c: 18 } }, { s: { r: 4, c: 0 }, e: { r: 4, c: 18 } },
]; ];
// Define styles // Define styles
const boldStyle = { const boldStyle = {
font: { bold: true, name: "Times New Roman", sz: 14 }, font: { bold: true, name: "Times New Roman", sz: 14 },
alignment: { horizontal: "center", vertical: "center" }, alignment: { horizontal: "center", vertical: "center" },
}; };
const redStyle = { const redStyle = {
font: { bold: true, color: { rgb: "FF0000" }, name: "Times New Roman", sz: 14 }, font: {
bold: true,
color: { rgb: "FF0000" },
name: "Times New Roman",
sz: 14,
},
alignment: { horizontal: "center", vertical: "center" }, alignment: { horizontal: "center", vertical: "center" },
}; };
const normalStyle = { const normalStyle = {
font: { name: "Times New Roman", sz: 12 }, font: { name: "Times New Roman", sz: 12 },
}; };
// Apply styles to headers // Apply styles to headers
const headerRanges = [ const headerRanges = [
{ row: 0, style: boldStyle }, { row: 0, style: boldStyle },
@@ -127,7 +128,7 @@ const ConsolidatedTable = () => {
{ row: 3, style: boldStyle }, { row: 3, style: boldStyle },
{ row: 4, style: redStyle }, { row: 4, style: redStyle },
]; ];
headerRanges.forEach(({ row, style }) => { headerRanges.forEach(({ row, style }) => {
for (let col = 0; col <= 18; col++) { for (let col = 0; col <= 18; col++) {
const cellAddress = XLSX.utils.encode_cell({ r: row, c: col }); const cellAddress = XLSX.utils.encode_cell({ r: row, c: col });
@@ -135,7 +136,7 @@ const ConsolidatedTable = () => {
worksheet[cellAddress].s = style; worksheet[cellAddress].s = style;
} }
}); });
// Set column widths for better readability // Set column widths for better readability
worksheet["!cols"] = [ worksheet["!cols"] = [
{ wch: 10 }, // Sr No { wch: 10 }, // Sr No
@@ -158,14 +159,14 @@ const ConsolidatedTable = () => {
{ wch: 15 }, // Moderation { wch: 15 }, // Moderation
{ wch: 15 }, // PwD Paper Setting { wch: 15 }, // PwD Paper Setting
]; ];
// Apply normal font style to all cells // Apply normal font style to all cells
Object.keys(worksheet).forEach((key) => { Object.keys(worksheet).forEach((key) => {
if (worksheet[key] && key[0] !== "!") { if (worksheet[key] && key[0] !== "!") {
worksheet[key].s = worksheet[key].s || normalStyle; worksheet[key].s = worksheet[key].s || normalStyle;
} }
}); });
// Add worksheet to workbook and save file // Add worksheet to workbook and save file
XLSX.utils.book_append_sheet(workbook, worksheet, teacherName); XLSX.utils.book_append_sheet(workbook, worksheet, teacherName);
XLSX.writeFile(workbook, `${teacherName.replace(/\s+/g, "_")}_Table.xlsx`); XLSX.writeFile(workbook, `${teacherName.replace(/\s+/g, "_")}_Table.xlsx`);
@@ -179,100 +180,108 @@ const ConsolidatedTable = () => {
}; };
const handleSendEmail = async (teacher, teacherData) => { const handleSendEmail = async (teacher, teacherData) => {
const workbook = XLSX.utils.book_new(); const facultyId = teacherData[0].facultyId; // This assumes all rows for a teacher have the same facultyId
const headerInfo = [
["Somaiya Vidyavihar University"],
["K. J. SOMAIYA COLLEGE OF ENGINEERING"],
[
"Appointment of Internal Examiners for Paper Setting / OR/PR/Assessment/Reassessment",
],
["Class: B Tech/M Tech/Honour/Minor"],
["Department - Computer Engineering"],
[],
];
const tableHeaders = [
[
"Sr No",
"Semester",
"Course Code",
"Course Name",
"Exam Type",
"Year",
"Marks",
"Surname",
"First Name",
"Middle Name",
"Affiliation/College",
"Highest Qualification",
"Career Experience",
"Oral/Practical",
"Assessment",
"Reassessment",
"Paper Setting",
"Moderation",
"PwD Paper Setting",
],
];
const dataRows = teacherData.map((row, index) => [
index + 1,
row.semester,
row.courseCode,
row.courseName,
row.examType,
row.year,
row.marks,
row.surname,
row.firstName,
row.middleName,
row.affiliation,
row.qualification,
row.experience,
row.oralPractical,
row.assessment,
row.reassessment,
row.paperSetting,
row.moderation,
row.pwdPaperSetting,
]);
const sheetData = [...headerInfo, ...tableHeaders, ...dataRows];
const worksheet = XLSX.utils.aoa_to_sheet(sheetData);
XLSX.utils.book_append_sheet(workbook, worksheet, teacher);
const fileName = `${teacher.replace(/\s+/g, "_")}_table.xlsx`;
const excelBlob = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
const file = new File([excelBlob], fileName, {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const formData = new FormData();
formData.append("teacher", teacher);
formData.append("fileName", fileName);
const recipientEmail = prompt(`Enter recipient email for ${teacher}:`);
formData.append("recipientEmail", recipientEmail);
formData.append("file", file);
try { try {
const response = await sendEmail(formData); // Fetch email from the backend
alert(`Email sent successfully to ${recipientEmail}`); const response = await axios.get(
console.log("Response from server:", response); `http://localhost:8080/api/faculty/email/${facultyId}`
);
const facultyEmail = response.data.email;
const workbook = XLSX.utils.book_new();
const headerInfo = [
["Somaiya Vidyavihar University"],
["K. J. SOMAIYA COLLEGE OF ENGINEERING"],
[
"Appointment of Internal Examiners for Paper Setting / OR/PR/Assessment/Reassessment",
],
["Class: B Tech/M Tech/Honour/Minor"],
["Department - Computer Engineering"],
[],
];
const tableHeaders = [
[
"Sr No",
"Semester",
"Course Code",
"Course Name",
"Exam Type",
"Year",
"Marks",
"Name",
"Affiliation/College",
"Highest Qualification",
"Career Experience",
"Oral/Practical",
"Assessment",
"Reassessment",
"Paper Setting",
"Moderation",
"PwD Paper Setting",
],
];
const dataRows = teacherData.map((row, index) => [
index + 1,
row.semester,
row.courseCode,
row.courseName,
row.examType,
row.year,
row.marks,
row.Name,
row.affiliation,
row.qualification,
row.experience,
row.oralPractical,
row.assessment,
row.reassessment,
row.paperSetting,
row.moderation,
row.pwdPaperSetting,
]);
const sheetData = [...headerInfo, ...tableHeaders, ...dataRows];
const worksheet = XLSX.utils.aoa_to_sheet(sheetData);
XLSX.utils.book_append_sheet(workbook, worksheet, teacher);
const fileName = `${teacher.replace(/\s+/g, "_")}_table.xlsx`;
const excelBlob = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
const file = new File([excelBlob], fileName, {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const formData = new FormData();
formData.append("teacher", teacher);
formData.append("fileName", fileName);
formData.append("recipientEmail", facultyEmail);
formData.append("file", file);
try {
const response = await sendEmail(formData);
alert(`Email sent successfully to ${facultyEmail}`);
console.log("Response from server:", response);
} catch (error) {
console.error("Error sending email:", error);
alert("Failed to send email.");
}
} catch (error) { } catch (error) {
console.error("Error sending email:", error); console.error("Error sending email:", error);
alert("Failed to send email."); alert("Failed to send email.");
} }
}; };
return ( return (
<div> <div>
<h1 style={{ textAlign: "center" }}>Faculty Tables with Download Options</h1> <h1 style={{ textAlign: "center" }}>
Faculty Tables with Download Options
</h1>
<div style={{ marginBottom: "20px", textAlign: "center" }}> <div style={{ marginBottom: "20px", textAlign: "center" }}>
<button <button

View File

@@ -63,6 +63,7 @@ router.get("/consolidated-table", async (req, res) => {
paperSetting: data.tasks.has("paperSetting") ? "✔" : "", paperSetting: data.tasks.has("paperSetting") ? "✔" : "",
moderation: data.tasks.has("moderation") ? "✔" : "", moderation: data.tasks.has("moderation") ? "✔" : "",
pwdPaperSetting: data.tasks.has("pwdPaperSetter") ? "✔" : "", pwdPaperSetting: data.tasks.has("pwdPaperSetter") ? "✔" : "",
key: `${data.facultyId}`
})); }));
res.status(200).json(tableData); res.status(200).json(tableData);