-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathreport.js
More file actions
55 lines (49 loc) · 1.79 KB
/
report.js
File metadata and controls
55 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const Report = require('../models/reportModel');
const nodemailer = require("nodemailer");
const reportRegister = async (req, res) => {
// console.log(req.isAuthenticated());
if (req.isAuthenticated()) {
try{const { longitude, latitude, icon, desc, title } = req.body;
// console.log(req.user)
if(!longitude || !latitude ||!icon || !desc){
return res.status(400).json({msg: "Please fill all fields"})
}
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
auth: {
user: 'jamarcus.conn99@ethereal.email',
pass: 'QpNWpQGS1yfw5Jj6H6'
}
});
const info = await transporter.sendMail({
from: `"RoadSafety!" <${req.user._id}>`, // sender address
to: "municipal@gamil.com", // list of receivers
subject: "Reporting an Issue", // Subject line
text: `${desc}`, // plain text body
html: `<div>${desc}<div><br/><div>Image Link - <a href='${icon}'>Link</a></div>`, // html body
});
const newReport =await new Report({
longitude: longitude, latitude: latitude, icon: icon, description: desc, title: title,userId:req.user._id
})
await newReport.save();
res.status(200).json(newReport);
}
catch{
res.status(400).json("not created")
}
}
else {
res.status(403).json({ error: true, message: "Not Authorized" });
}
}
const getReport=async(req,res)=>{
try{
const reports = await Report.find();
res.status(200).json(reports);
}
catch(err){
res.status(500).json({ error: 'Internal server error' });
}
}
module.exports = { reportRegister, getReport};