Skip to content

Commit

Permalink
Implement the emailAdmin endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshGajabi committed Nov 24, 2023
1 parent 47e7b89 commit 6f8012b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
50 changes: 23 additions & 27 deletions backend/controllers/admin.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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: '[email protected]',
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' });
Expand Down
4 changes: 3 additions & 1 deletion backend/routes/admin.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -16,6 +15,7 @@ router.route('/allLostItems')
}
}
)

router.route('/allFoundItems')
.get(
adminAuthenticate,
Expand All @@ -27,6 +27,7 @@ router.route('/allFoundItems')
}
}
)

router.route('/allPotentialMatches')
.get(
adminAuthenticate,
Expand All @@ -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)
Expand Down

0 comments on commit 6f8012b

Please sign in to comment.