From 6f8012b4297fc99508cc5a430e7009e6974e08e7 Mon Sep 17 00:00:00 2001 From: Harshvardhan Gajabi Date: Fri, 24 Nov 2023 11:49:50 -0500 Subject: [PATCH] Implement the emailAdmin endpoint --- backend/controllers/admin.js | 50 +++++++++++++++++------------------- backend/routes/admin.js | 4 ++- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/backend/controllers/admin.js b/backend/controllers/admin.js index fba85c1..da95d9f 100644 --- a/backend/controllers/admin.js +++ b/backend/controllers/admin.js @@ -1,11 +1,6 @@ -const { v4: uuidv4 } = require('uuid'); const { LostItem, FoundItem, PotentialMatch } = require('../models/Item'); -const { s3 } = require('../utils/aws'); -const { errorHandler } = require('../utils/errorHandler'); -const Fuse = require('fuse.js'); -const { default: mongoose } = require('mongoose'); -const nodemailer = require('nodemailer'); -const xoauth2 = require('xoauth2'); +const User = require('../models/User'); +const { getTransporter } = require('../utils/otp'); module.exports.getAllLostItems = async (req, res, next) => { try { @@ -39,28 +34,29 @@ module.exports.getAllPotentialMatches = async (req, res, next) => { module.exports.emailAdmin = async (req, res, next) => { try { - const transporter = nodemailer.createTransport({ - host: 'smtp.gmail.com', - port: 465, - secure: true, - auth: { - user: process.env.EMAIL_ADDRESS, - pass: process.env.EMAIL_PASSWORD - } - }) - const mailOptions = { - from: `Findify <${process.env.EMAIL_ADDRESS}>`, - to: 'goutams.in@hotmail.com', - subject: `Findify - ${req.body.subject}`, - text: `${req.body.message} - Thanks, - ${req.user.email} - ` + // send the email to this admin (ramdomly chosen) + const lostAndFoundAdmin = await User.findOne({ isAdmin: true }); + if (!lostAndFoundAdmin) { + throw new Error('Lost and Found Admin not found'); } - return await transporter.sendMail(mailOptions, (err, info) => { - if (err) console.error(err); - else console.log('Email sent:', info.response); + + const transporter = getTransporter(); + + const mailOptions = { + from: `Findify <${process.env.EMAIL_SERVICE_USER}>`, + to: lostAndFoundAdmin.email, + subject: "Findify: New user inquiry received", + text: `You have a new inquiry from the user: ${req.user.email} + subject: ${req.body.subject} + body: ${req.body.body}`, + }; + + transporter.sendMail(mailOptions).catch((err) => { + console.log(err); + return res.status(500).json({ message: "Failed to email the admin" }); }); + + res.status(200).json({ message: "Admin emailed successfully" }); } catch (err) { console.error("Error sending email to admin:", err); res.status(500).json({ message: 'Error sending email to admin' }); diff --git a/backend/routes/admin.js b/backend/routes/admin.js index 10e76c6..c9dea07 100644 --- a/backend/routes/admin.js +++ b/backend/routes/admin.js @@ -1,7 +1,6 @@ const express = require('express'); const { checkRequiredAttributes, authenticate, adminAuthenticate } = require('../middlewares/user'); const AdminController = require('../controllers/admin'); -const multerUpload = require("../middlewares/multer"); const router = express.Router(); @@ -16,6 +15,7 @@ router.route('/allLostItems') } } ) + router.route('/allFoundItems') .get( adminAuthenticate, @@ -27,6 +27,7 @@ router.route('/allFoundItems') } } ) + router.route('/allPotentialMatches') .get( adminAuthenticate, @@ -42,6 +43,7 @@ router.route('/allPotentialMatches') router.route('/emailAdmin') .post( authenticate, + checkRequiredAttributes(["subject", "body"]), async (req, res) => { try { await AdminController.emailAdmin(req, res, errorHandler)