diff --git a/backend/index.js b/backend/index.js index 71bde03..edaf7aa 100644 --- a/backend/index.js +++ b/backend/index.js @@ -126,18 +126,37 @@ app.put("/update-notification", authenticateUser, async (req, res) => { // ANNOUNCEMENT APIS // API to get announcements -app.get("/announcements", async (req, res) => { +app.post("/announcements", async (req, res) => { + // reqyest taken from the frontend connection + const { title, message } = req.body; + const users = await User.find({}, "email"); + const emailList = users.map((user) => user.email); + + // Set up Nodemailer transporter + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: process.env.EMAIL, + pass: process.env.PASS, + }, + }); + + const mailOptions = { + from: process.env.EMAIL, + to: emailList, + subject: title, + text: message, + }; + try { - const announcements = [ - { id: 1, message: "System maintenance on Sunday at 2 AM" }, - { id: 2, message: "New feature release next week" }, - ]; - res.status(200).send(announcements); + // Send email + await transporter.sendMail(mailOptions); + res + .status(200) + .json({ message: "Announcement posted and email sent successfully!" }); } catch (error) { - res.status(500).send({ - message: "Error occurred when fetching announcements.", - error: error.message, - }); + console.error("Error sending email:", error); + res.status(500).json({ message: "Error posting announcement" }); } }); diff --git a/backend/package-lock.json b/backend/package-lock.json index a66a124..7d9b65f 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -15,7 +15,11 @@ "express": "^4.19.2", "jsonwebtoken": "^9.0.2", "mongoose": "^8.4.0", +<<<<<<< HEAD + "nodemailer": "^6.9.13", +======= "nodemailer": "^6.9.14", +>>>>>>> 53b4556520ccfc77bbb053ad39f15f4f69ced03a "nodemon": "^3.1.1", "twilio": "^5.1.1" }, @@ -5521,9 +5525,15 @@ "dev": true }, "node_modules/nodemailer": { +<<<<<<< HEAD + "version": "6.9.13", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.13.tgz", + "integrity": "sha512-7o38Yogx6krdoBf3jCAqnIN4oSQFx+fMa0I7dK1D+me9kBxx12D+/33wSb+fhOCtIxvYJ+4x4IMEhmhCKfAiOA==", +======= "version": "6.9.14", "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.14.tgz", "integrity": "sha512-Dobp/ebDKBvz91sbtRKhcznLThrKxKt97GI2FAlAyy+fk19j73Uz3sBXolVtmcXjaorivqsbbbjDY+Jkt4/bQA==", +>>>>>>> 53b4556520ccfc77bbb053ad39f15f4f69ced03a "engines": { "node": ">=6.0.0" } diff --git a/backend/package.json b/backend/package.json index 3dd18f8..3b3e1b0 100644 --- a/backend/package.json +++ b/backend/package.json @@ -21,7 +21,11 @@ "express": "^4.19.2", "jsonwebtoken": "^9.0.2", "mongoose": "^8.4.0", +<<<<<<< HEAD + "nodemailer": "^6.9.13", +======= "nodemailer": "^6.9.14", +>>>>>>> 53b4556520ccfc77bbb053ad39f15f4f69ced03a "nodemon": "^3.1.1", "twilio": "^5.1.1" }, diff --git a/src/App.js b/src/App.js index 5f7d81e..11adcad 100644 --- a/src/App.js +++ b/src/App.js @@ -17,9 +17,10 @@ import AdminD from "./Dashboards/AdminD"; import Settings from "./pages/Settings"; import AccountSettings from "./settings/AccountSettings"; import NotFoundPage from "./pages/PNF"; +import Announcement from "./components/Announcement"; import ForgotPassword from "./pages/Forgotpassword.js"; import ResetPassword from "./pages/ResetPassword.js"; -import ProgressBar from './components/ProgressBar.js'; +import ProgressBar from "./components/ProgressBar.js"; const App = () => { return ( @@ -34,6 +35,10 @@ const App = () => { } /> }> } /> + } + /> } /> } /> } /> @@ -49,7 +54,6 @@ const App = () => { } /> } /> - > ); }; diff --git a/src/Dashboards/AdminD.js b/src/Dashboards/AdminD.js index 907e834..e93fffb 100644 --- a/src/Dashboards/AdminD.js +++ b/src/Dashboards/AdminD.js @@ -49,6 +49,10 @@ const AdminD = () => { navigate("/user/settings"); }; + const handleAnnounce = () => { + navigate("/admin/announcement"); + }; + const handleManageDatabase = () => { window.open( "mongodb+srv://usha15322:SmartSaver%402024@cluster0.nd5jrmm.mongodb.net/", @@ -186,6 +190,7 @@ const AdminD = () => { {announcements.map((announcement) => ( + {announcement.title} {announcement.message} ))} @@ -245,8 +250,16 @@ const AdminD = () => { - - + + Create Announcements + + Announncements + + + Manage Database { + const [announcement, setAnnouncement] = useState({ + title: "", + message: "", + }); + + const handleChange = (e) => { + const { name, value } = e.target; + setAnnouncement({ + ...announcement, + [name]: value, + }); + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + + try { + const response = await axios.post( + "http://localhost:6352/announcements", + announcement + ); + + if (response.status === 200) { + toast.success("Alert Raised successfully."); + setAnnouncement({ title: "", message: "" }); + } else { + toast.error("Error posting announcement"); + } + } catch (error) { + console.error("Error:", error); + alert("Error posting announcement"); + } + }; + + return ( + <> + + + + + + Create Announcements + + + Title + + + + + + Message + + + + + + Post Announcement + + + + + + > + ); +}; + +export default Announcement; diff --git a/src/components/Navbar.js b/src/components/Navbar.js index 886c481..0364560 100644 --- a/src/components/Navbar.js +++ b/src/components/Navbar.js @@ -4,7 +4,7 @@ import { Link } from "react-router-dom"; const Navbar = () => { const isLoggedIn = localStorage.getItem("isLoggedIn") === "true"; const currentUser = JSON.parse(localStorage.getItem("currentUser")); - const userRole = currentUser?.role; + const userRole = currentUser?.role; return ( @@ -32,16 +32,17 @@ const Navbar = () => { className="text-lg font-semibold hover:text-green-600" to="/admin/dashboard" > - ADMIN + ADMIN )} + {isLoggedIn && ( <> - USER + USER