-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47e7b89
commit 6f8012b
Showing
2 changed files
with
26 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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' }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters