forked from CSI-KJSCE/appointment_to_examiner
mailing-part 50%
This commit is contained in:
@@ -1,197 +1,7 @@
|
||||
// import React, { useState, useEffect } from "react";
|
||||
// import axios from "axios";
|
||||
|
||||
// const ConsolidatedTable = () => {
|
||||
// const [data, setData] = useState([]);
|
||||
// const [loading, setLoading] = useState(true);
|
||||
|
||||
// useEffect(() => {
|
||||
// const fetchData = async () => {
|
||||
// try {
|
||||
// const response = await axios.get("http://localhost:8080/api/table/consolidated-table");
|
||||
// setData(response.data);
|
||||
// setLoading(false);
|
||||
// } catch (error) {
|
||||
// console.error("Error fetching table data:", error);
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
|
||||
// fetchData();
|
||||
// }, []);
|
||||
|
||||
// if (loading) {
|
||||
// return <div>Loading...</div>;
|
||||
// }
|
||||
|
||||
// return (
|
||||
// <div>
|
||||
// <h1>Consolidated Table</h1>
|
||||
// <table border="1" style={{ width: "100%", textAlign: "left" }}>
|
||||
// <thead>
|
||||
// <tr>
|
||||
// <th>Semester</th>
|
||||
// <th>Course Code</th>
|
||||
// <th>Course Name</th>
|
||||
// <th>Exam Type</th>
|
||||
// <th>Year</th>
|
||||
// <th>Marks</th>
|
||||
// <th>Name</th>
|
||||
// <th>Affiliation/College</th>
|
||||
// <th>Highest Qualification</th>
|
||||
// <th>Career Experience</th>
|
||||
// <th>Oral/Practical</th>
|
||||
// <th>Assessment</th>
|
||||
// <th>Reassessment</th>
|
||||
// <th>Paper Setting</th>
|
||||
// <th>Moderation</th>
|
||||
// <th>PwD Paper Setting</th>
|
||||
// </tr>
|
||||
// </thead>
|
||||
// <tbody>
|
||||
// {data.map((row, index) => (
|
||||
// <tr key={index}>
|
||||
// <td>{row.semester}</td>
|
||||
// <td>{row.courseCode}</td>
|
||||
// <td>{row.courseName}</td>
|
||||
// <td>{row.examType}</td>
|
||||
// <td>{row.year}</td>
|
||||
// <td>{row.marks}</td>
|
||||
// <td>{row.Name}</td>
|
||||
// <td>{row.affiliation}</td>
|
||||
// <td>{row.qualification}</td>
|
||||
// <td>{row.experience}</td>
|
||||
// <td>{row.oralPractical}</td>
|
||||
// <td>{row.assessment}</td>
|
||||
// <td>{row.reassessment}</td>
|
||||
// <td>{row.paperSetting}</td>
|
||||
// <td>{row.moderation}</td>
|
||||
// <td>{row.pwdPaperSetting}</td>
|
||||
// </tr>
|
||||
// ))}
|
||||
// </tbody>
|
||||
// </table>
|
||||
// </div>
|
||||
// );
|
||||
// };
|
||||
|
||||
// export default ConsolidatedTable;
|
||||
|
||||
|
||||
|
||||
// import React, { useState, useEffect } from "react";
|
||||
// import axios from "axios";
|
||||
// import { CSVLink } from "react-csv";
|
||||
|
||||
// const ConsolidatedTable = () => {
|
||||
// const [data, setData] = useState([]);
|
||||
// const [loading, setLoading] = useState(true);
|
||||
|
||||
// useEffect(() => {
|
||||
// const fetchData = async () => {
|
||||
// try {
|
||||
// const response = await axios.get("http://localhost:8080/api/table/consolidated-table");
|
||||
// setData(response.data);
|
||||
// setLoading(false);
|
||||
// } catch (error) {
|
||||
// console.error("Error fetching table data:", error);
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
|
||||
// fetchData();
|
||||
// }, []);
|
||||
|
||||
// if (loading) {
|
||||
// return <div>Loading...</div>;
|
||||
// }
|
||||
|
||||
// // Extract unique faculty names
|
||||
// const uniqueTeachers = [...new Set(data.map((row) => row.Name))];
|
||||
|
||||
// return (
|
||||
// <div>
|
||||
// <h1>Faculty Tables with Download Option</h1>
|
||||
// {uniqueTeachers.map((teacher, index) => {
|
||||
// // Filter rows for the current teacher
|
||||
// const teacherData = data.filter((row) => row.Name === teacher);
|
||||
|
||||
// return (
|
||||
// <div key={index} style={{ marginBottom: "20px" }}>
|
||||
// <h2>{teacher}'s Table</h2>
|
||||
// <table border="1" style={{ width: "100%", textAlign: "left", marginBottom: "10px" }}>
|
||||
// <thead>
|
||||
// <tr>
|
||||
// <th>Semester</th>
|
||||
// <th>Course Code</th>
|
||||
// <th>Course Name</th>
|
||||
// <th>Exam Type</th>
|
||||
// <th>Year</th>
|
||||
// <th>Marks</th>
|
||||
// <th>Name</th>
|
||||
// <th>Affiliation/College</th>
|
||||
// <th>Highest Qualification</th>
|
||||
// <th>Career Experience</th>
|
||||
// <th>Oral/Practical</th>
|
||||
// <th>Assessment</th>
|
||||
// <th>Reassessment</th>
|
||||
// <th>Paper Setting</th>
|
||||
// <th>Moderation</th>
|
||||
// <th>PwD Paper Setting</th>
|
||||
// </tr>
|
||||
// </thead>
|
||||
// <tbody>
|
||||
// {teacherData.map((row, idx) => (
|
||||
// <tr key={idx}>
|
||||
// <td>{row.semester}</td>
|
||||
// <td>{row.courseCode}</td>
|
||||
// <td>{row.courseName}</td>
|
||||
// <td>{row.examType}</td>
|
||||
// <td>{row.year}</td>
|
||||
// <td>{row.marks}</td>
|
||||
// <td>{row.Name}</td>
|
||||
// <td>{row.affiliation}</td>
|
||||
// <td>{row.qualification}</td>
|
||||
// <td>{row.experience}</td>
|
||||
// <td>{row.oralPractical}</td>
|
||||
// <td>{row.assessment}</td>
|
||||
// <td>{row.reassessment}</td>
|
||||
// <td>{row.paperSetting}</td>
|
||||
// <td>{row.moderation}</td>
|
||||
// <td>{row.pwdPaperSetting}</td>
|
||||
// </tr>
|
||||
// ))}
|
||||
// </tbody>
|
||||
// </table>
|
||||
// {/* CSV Download Button */}
|
||||
// <CSVLink
|
||||
// data={teacherData}
|
||||
// filename={`${teacher.replace(/\s+/g, "_")}_table.csv`}
|
||||
// className="btn btn-primary"
|
||||
// style={{
|
||||
// padding: "10px 15px",
|
||||
// backgroundColor: "#007bff",
|
||||
// color: "white",
|
||||
// textDecoration: "none",
|
||||
// borderRadius: "5px",
|
||||
// }}
|
||||
// >
|
||||
// Download CSV
|
||||
// </CSVLink>
|
||||
// </div>
|
||||
// );
|
||||
// })}
|
||||
// </div>
|
||||
// );
|
||||
// };
|
||||
|
||||
// export default ConsolidatedTable;
|
||||
|
||||
|
||||
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import axios from "axios";
|
||||
import { CSVLink } from "react-csv";
|
||||
import { sendEmail } from "../api";
|
||||
|
||||
const ConsolidatedTable = () => {
|
||||
const [data, setData] = useState([]);
|
||||
@@ -201,7 +11,9 @@ const ConsolidatedTable = () => {
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const response = await axios.get("http://localhost:8080/api/table/consolidated-table");
|
||||
const response = await axios.get(
|
||||
"http://localhost:8080/api/table/consolidated-table"
|
||||
);
|
||||
setData(response.data);
|
||||
setLoading(false);
|
||||
} catch (error) {
|
||||
@@ -229,6 +41,46 @@ const ConsolidatedTable = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const handleSendEmail = async (teacher, teacherData) => {
|
||||
const csvHeaders = Object.keys(teacherData[0]);
|
||||
const csvRows = teacherData.map((row) =>
|
||||
csvHeaders
|
||||
.map(
|
||||
(header) => `"${row[header]?.toString().replace(/"/g, '""') || ""}"`
|
||||
)
|
||||
.join(",")
|
||||
);
|
||||
const csvContent = [csvHeaders.join(","), ...csvRows].join("\n");
|
||||
const fileName = `${teacher.replace(/\s+/g, "_")}_table.csv`;
|
||||
|
||||
try {
|
||||
const recipientEmail = prompt(`Enter recipient email for ${teacher}:`);
|
||||
if (!recipientEmail) {
|
||||
alert("Email is required!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create email data object
|
||||
const emailData = {
|
||||
teacher,
|
||||
csvData: csvContent,
|
||||
fileName,
|
||||
recipientEmail,
|
||||
};
|
||||
|
||||
// Call sendEmail from api.js
|
||||
const response = await sendEmail(emailData);
|
||||
|
||||
// Handle success
|
||||
alert(`Email sent successfully to ${recipientEmail}`);
|
||||
console.log("Response from server:", response);
|
||||
alert(`Email sent successfully to ${recipientEmail}`);
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
alert("Failed to send email.");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Faculty Tables with Download Options</h1>
|
||||
@@ -285,7 +137,14 @@ const ConsolidatedTable = () => {
|
||||
return (
|
||||
<div key={index} style={{ marginBottom: "20px" }}>
|
||||
<h2>{teacher}'s Table</h2>
|
||||
<table border="1" style={{ width: "100%", textAlign: "left", marginBottom: "10px" }}>
|
||||
<table
|
||||
border="1"
|
||||
style={{
|
||||
width: "100%",
|
||||
textAlign: "left",
|
||||
marginBottom: "10px",
|
||||
}}
|
||||
>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Semester</th>
|
||||
@@ -345,6 +204,20 @@ const ConsolidatedTable = () => {
|
||||
>
|
||||
Download {teacher}'s CSV
|
||||
</CSVLink>
|
||||
{/* Send Email Button */}
|
||||
<button
|
||||
onClick={() => handleSendEmail(teacher, teacherData)}
|
||||
className="btn btn-secondary"
|
||||
style={{
|
||||
padding: "10px 15px",
|
||||
backgroundColor: "#6c757d",
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
borderRadius: "5px",
|
||||
}}
|
||||
>
|
||||
Send {teacher}'s CSV via Email
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -137,3 +137,31 @@ export const updateCourseStatus = async (courseId) => {
|
||||
});
|
||||
};
|
||||
|
||||
// Send email
|
||||
export const sendEmail = async (emailData) => {
|
||||
console.log("Sending email with data:", emailData);
|
||||
|
||||
// Validate input
|
||||
// if (!emailData.to || !emailData.subject || !emailData.message) {
|
||||
// const errorMessage = "Missing required fields: to, subject, message";
|
||||
// console.error(errorMessage);
|
||||
// throw new Error(errorMessage);
|
||||
// }
|
||||
|
||||
try {
|
||||
const url = `${BASE_URL}/send-email`;
|
||||
const response = await fetchData(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(emailData), // Pass the email data to the server
|
||||
});
|
||||
|
||||
console.log("Email sent successfully:", response);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error.message);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
58
server/routes/emailRoutes.js
Normal file
58
server/routes/emailRoutes.js
Normal file
@@ -0,0 +1,58 @@
|
||||
// /routes/email.js
|
||||
const express = require("express");
|
||||
const nodemailer = require("nodemailer");
|
||||
const fs = require("fs");
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
const { teacher, csvData, fileName, recipientEmail } = req.body;
|
||||
|
||||
if (!teacher || !csvData || !fileName || !recipientEmail) {
|
||||
return res.status(400).json({ error: "Missing required fields" });
|
||||
}
|
||||
|
||||
// Save the CSV data to a temporary file
|
||||
const filePath = `./${fileName}`;
|
||||
fs.writeFileSync(filePath, csvData);
|
||||
|
||||
// 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, // Disable SSL verification
|
||||
// }
|
||||
});
|
||||
|
||||
// Email options
|
||||
const mailOptions = {
|
||||
from: "swdc.ate@gmail.com", // Replace with your email
|
||||
to: recipientEmail,
|
||||
subject: `CSV File for ${teacher}`,
|
||||
text: `Attached is the CSV file for ${teacher}.`,
|
||||
attachments: [
|
||||
{
|
||||
filename: fileName,
|
||||
path: filePath,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
try {
|
||||
// Send email
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
// Delete the temporary file after sending the email
|
||||
fs.unlinkSync(filePath);
|
||||
|
||||
res.status(200).json({ message: "Email sent successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
res.status(500).json({ error: "Failed to send email" });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -15,6 +15,7 @@ const facultyRoutes = require("./routes/facultyRoutes");
|
||||
const appointmentRoutes = require("./routes/appointmentRoutes");
|
||||
const optionsRoutes = require("./routes/optionsRoutes");
|
||||
const consolidatedRoutes = require("./routes/consolidatedRoutes");
|
||||
const emailRoutes = require("./routes/emailRoutes");
|
||||
const Course = require("./models/Course");
|
||||
|
||||
// MongoDB Connection
|
||||
@@ -56,6 +57,7 @@ app.use("/api/faculty", facultyRoutes);
|
||||
app.use("/api/appointments", appointmentRoutes);
|
||||
app.use("/api/options", optionsRoutes);
|
||||
app.use("/api/data", consolidatedRoutes); // Moved after `app` initialization
|
||||
app.use("/api/send-email", emailRoutes);
|
||||
|
||||
// Google OAuth Routes
|
||||
app.get(
|
||||
|
||||
Reference in New Issue
Block a user