forked from CSI-KJSCE/BOS-React-
final changes
This commit is contained in:
78
backend/controllers/departmentController.js
Normal file
78
backend/controllers/departmentController.js
Normal file
@@ -0,0 +1,78 @@
|
||||
const DepartmentEmail = require("../models/DepartmentEmail");
|
||||
const Program = require("../models/Program");
|
||||
|
||||
exports.getAllDepartmentEmails = async (req, res) => {
|
||||
try {
|
||||
const departmentEmails = await DepartmentEmail.find();
|
||||
res.json(departmentEmails);
|
||||
} catch (error) {
|
||||
console.error("Error fetching department emails:", error);
|
||||
res.status(500).json({ error: "Error fetching department emails" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.getDepartmentsByProgram = async (req, res) => {
|
||||
const program = await Program.findOne({ name: req.params.program });
|
||||
res.json(program ? program.departments : []);
|
||||
};
|
||||
|
||||
exports.getEmailsByDepartment = async (req, res) => {
|
||||
const departmentEmails = await DepartmentEmail.findOne({ department: req.params.department });
|
||||
res.json(departmentEmails ? { emails: departmentEmails.emails } : { emails: [] });
|
||||
};
|
||||
|
||||
exports.updateDepartmentEmails = async (req, res) => {
|
||||
const { department } = req.params;
|
||||
const { emails } = req.body;
|
||||
|
||||
if (!emails || !Array.isArray(emails)) {
|
||||
return res.status(400).json({ error: "Emails array is required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const updatedDepartment = await DepartmentEmail.findOneAndUpdate(
|
||||
{ department },
|
||||
{ emails },
|
||||
{ new: true, upsert: true }
|
||||
);
|
||||
res.json(updatedDepartment);
|
||||
} catch (error) {
|
||||
console.error("Error updating department emails:", error);
|
||||
res.status(500).json({ error: "Error updating department emails" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.createDepartment = async (req, res) => {
|
||||
const { department, emails, program } = req.body;
|
||||
|
||||
if (!department || !emails || !Array.isArray(emails) || !program) {
|
||||
return res.status(400).json({ error: "Department, program, and emails array are required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const newDepartment = new DepartmentEmail({ department, emails });
|
||||
await newDepartment.save();
|
||||
|
||||
await Program.findOneAndUpdate(
|
||||
{ name: program },
|
||||
{ $addToSet: { departments: department } },
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
res.status(201).json(newDepartment);
|
||||
} catch (error) {
|
||||
console.error("Error creating department:", error);
|
||||
res.status(500).json({ error: "Error creating department" });
|
||||
}
|
||||
};
|
||||
|
||||
exports.deleteDepartment = async (req, res) => {
|
||||
try {
|
||||
const { department } = req.params;
|
||||
await DepartmentEmail.findOneAndDelete({ department });
|
||||
res.json({ message: "Department deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting department:", error);
|
||||
res.status(500).json({ error: "Error deleting department" });
|
||||
}
|
||||
};
|
||||
136
backend/controllers/emailController.js
Normal file
136
backend/controllers/emailController.js
Normal file
@@ -0,0 +1,136 @@
|
||||
const nodemailer = require("nodemailer");
|
||||
const moment = require("moment");
|
||||
const Meeting = require("../models/Meeting");
|
||||
const DepartmentEmail = require("../models/DepartmentEmail");
|
||||
|
||||
const sendEmail = async (req, res) => {
|
||||
try {
|
||||
const { program, department, subject, body, agenda, date, startTime, endTime, recipients } = req.body;
|
||||
const files = req.files;
|
||||
|
||||
console.log({ program, department, subject, body, agenda, date, startTime, endTime, recipients, files });
|
||||
|
||||
if (!program || !department || !subject || !body || !date || !startTime || !endTime || !recipients) {
|
||||
return res.status(400).json({ error: "All fields are required!" });
|
||||
}
|
||||
|
||||
const departmentEmails = await DepartmentEmail.findOne({ department });
|
||||
if (!departmentEmails) {
|
||||
return res.status(404).json({ error: "Department emails not found" });
|
||||
}
|
||||
|
||||
const allRecipients = [...new Set([...departmentEmails.emails, ...recipients])];
|
||||
|
||||
const conflict = await Meeting.findOne({
|
||||
date,
|
||||
startTime: { $lt: endTime },
|
||||
endTime: { $gt: startTime },
|
||||
});
|
||||
|
||||
const formattedDate = moment(date, "YYYY-MM-DD").format("dddd, MMMM Do YYYY");
|
||||
const formattedStartTime = moment(startTime, "HH:mm").format("hh:mm A");
|
||||
const formattedEndTime = moment(endTime, "HH:mm").format("hh:mm A");
|
||||
|
||||
const formattedStart = moment(`${date} ${startTime}`, "YYYY-MM-DD HH:mm").utc().format("YYYYMMDDTHHmmss[Z]");
|
||||
const formattedEnd = moment(`${date} ${endTime}`, "YYYY-MM-DD HH:mm").utc().format("YYYYMMDDTHHmmss[Z]");
|
||||
|
||||
const zoomLink = "https://us05web.zoom.us/j/2655616202?pwd=S3d6RjVPNnhmQ3AzTlZsRC9GYmNHQT09";
|
||||
const calendarLink = `https://calendar.google.com/calendar/render?action=TEMPLATE&text=${encodeURIComponent(
|
||||
subject
|
||||
)}&details=${encodeURIComponent(`${body}\nJoin Zoom Meeting: ${zoomLink}`)}&dates=${formattedStart}/${formattedEnd}`;
|
||||
|
||||
const formattedAgenda = Array.isArray(agenda) && agenda.length > 0
|
||||
? agenda.map((item, index) => `<li>${index + 1}. ${item}</li>`).join("")
|
||||
: "<li>No agenda provided</li>";
|
||||
|
||||
const newMeeting = new Meeting({
|
||||
program,
|
||||
department,
|
||||
subject,
|
||||
body,
|
||||
agenda,
|
||||
date,
|
||||
startTime,
|
||||
endTime,
|
||||
recipients,
|
||||
attachments: files.map((file) => ({ filename: file.filename, path: file.path })),
|
||||
});
|
||||
await newMeeting.save();
|
||||
|
||||
let conflictDetails = null;
|
||||
|
||||
if (conflict) {
|
||||
conflictDetails = {
|
||||
department: conflict.department,
|
||||
date: conflict.date,
|
||||
startTime: conflict.startTime,
|
||||
endTime: conflict.endTime,
|
||||
recipients: conflict.recipients,
|
||||
subject: conflict.subject,
|
||||
agenda: conflict.agenda || "No agenda provided",
|
||||
};
|
||||
console.log("Conflict detected! Details:", conflictDetails);
|
||||
}
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: "gmail",
|
||||
auth: {
|
||||
user: process.env.EMAIL,
|
||||
pass: process.env.EMAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
const mailOptions = {
|
||||
from: process.env.EMAIL,
|
||||
to: recipients,
|
||||
subject,
|
||||
html: `
|
||||
<p><strong>Respected Board Members,</strong></p>
|
||||
<p><strong>Greetings from Somaiya Vidyavihar University !!</strong></p>
|
||||
<p><strong>We are glad to invite you to the Board of Studies meeting: </strong></p>
|
||||
<p>${body}</p>
|
||||
<p><strong>Program:</strong> ${program}</p>
|
||||
<p><strong>Department:</strong> ${department}</p>
|
||||
<p><strong>Date:</strong> ${formattedDate}</p>
|
||||
<p><strong>Time:</strong> ${formattedStartTime} - ${formattedEndTime}</p>
|
||||
<p><strong>Zoom Meeting Details:</strong></p>
|
||||
<p><strong>Meeting ID: </strong>265 561 6202</p>
|
||||
<p><strong>Meeting Password: </strong>123456</p>
|
||||
<p><strong>Zoom Meeting Link: </strong><a href="${zoomLink}" target="_blank">Join the Meeting</a></p>
|
||||
<p><strong>Agenda:</strong></p>
|
||||
<ul>${formattedAgenda}</ul>
|
||||
<p><a href="${calendarLink}">📅 Click here to add this event to Google Calendar</a></p>
|
||||
${
|
||||
conflictDetails
|
||||
? `<p><strong>Conflict detected!</strong></p>
|
||||
<p><strong>Conflicting Meeting Details:</strong></p>
|
||||
<ul>
|
||||
<li><strong>Department:</strong> ${conflictDetails.department}</li>
|
||||
<li><strong>Date:</strong> ${conflictDetails.date}</li>
|
||||
<li><strong>Time:</strong> ${conflictDetails.startTime} - ${conflictDetails.endTime}</li>
|
||||
<li><strong>Subject:</strong> ${conflictDetails.subject}</li>
|
||||
<li><strong>Agenda:</strong> ${conflictDetails.agenda}</li>
|
||||
</ul>`
|
||||
: ""
|
||||
}
|
||||
`,
|
||||
attachments: files.map((file) => ({
|
||||
filename: file.filename,
|
||||
path: file.path,
|
||||
})),
|
||||
};
|
||||
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
res.status(200).json({
|
||||
message: conflictDetails ? "Conflict detected! Email sent successfully." : "Email sent successfully!",
|
||||
conflictDetails,
|
||||
calendarLink,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
res.status(500).json({ error: "Error sending email" });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { sendEmail };
|
||||
18
backend/controllers/meetingController.js
Normal file
18
backend/controllers/meetingController.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const Meeting = require("../models/Meeting");
|
||||
|
||||
exports.getMeetings = async (req, res) => {
|
||||
try {
|
||||
const meetings = await Meeting.find();
|
||||
const formattedMeetings = meetings.map((meeting) => {
|
||||
const formattedAgenda = meeting.agenda
|
||||
? meeting.agenda.map((item, index) => `<li>${index + 1}. ${item}</li>`).join("")
|
||||
: "<li>No agenda provided</li>";
|
||||
return { ...meeting.toObject(), formattedAgenda };
|
||||
});
|
||||
|
||||
res.json(formattedMeetings);
|
||||
} catch (error) {
|
||||
console.error("Error fetching meetings:", error);
|
||||
res.status(500).json({ error: "Error fetching meetings" });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user