Skip to content

Commit

Permalink
Merge pull request #110 from Prateek579/add-complaint-sec
Browse files Browse the repository at this point in the history
enhanced complaint response system task completed (#95)
  • Loading branch information
Sahil1786 authored May 19, 2024
2 parents d5b048d + 224b309 commit 8bd2b29
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 268 deletions.
6 changes: 6 additions & 0 deletions model/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const querySchema = new mongoose.Schema(
subject: String,
message: String,
approved: { type: Boolean, default: false },
// adding models for storing ANSWERE ans USER_ID
answere: String,
user_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
},
{ timestamps: true }
);
Expand Down
68 changes: 41 additions & 27 deletions routers/NgoRoutes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require("express");
const router = new express.Router();
const fs = require("fs");
const path = require('path');
const Handlebars = require('handlebars');
const path = require("path");
const Handlebars = require("handlebars");

const bcrypt = require("bcrypt");
const saltRounds = 10;
Expand All @@ -19,19 +19,29 @@ router.post("/NGO-login", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
const ngo = await NGO.findOne({ username: username, password: password });

try {
const dooner = await User.find(); // Assuming User is your Mongoose model for users

res.render("NGO-Dashboard", {
fullName: ngo.NGOName,
email: ngo.username,
id: ngo.NGOID,
phoneNo: ngo.Mobile,
address: ngo.NGOLocation,
Donation: dooner,
Pickup: dooner,
complain: "",
});
if (ngo) {
//checking NGO is APPROVED or NOT
if (ngo.approved == false) {
res.status(500).json({ messaage: "NGO is send for approval" });
}

const dooner = await User.find(); // Assuming User is your Mongoose model for users

res.render("NGO-Dashboard", {
fullName: ngo.NGOName,
email: ngo.username,
id: ngo.NGOID,
phoneNo: ngo.Mobile,
address: ngo.NGOLocation,
Donation: dooner,
Pickup: dooner,
complain: "",
});
} else {
res.status(404).json({ message: "NGO is not registered" });
}
} catch (err) {
console.error(err);
res.status(500).send("An internal server error occurred.");
Expand Down Expand Up @@ -61,24 +71,28 @@ router.post("/NGO-Registarion", async (req, res) => {
// Save the new NGO to the database
await newNGO.save();

const templatePath = path.join(__dirname, '../views', 'Email.template.handlebars');
const templateContent = fs.readFileSync(templatePath, 'utf8');

// Compile the Handlebars template with the provided context data
const compiledHtml = Handlebars.compile(templateContent)({
user: {
_id: newNGO.NgoID, // Example ID
username: newNGO.NGOName, // Example username
email: newNGO.username, // Example email
fname: newNGO.NGOName // Example first name
}
});
const templatePath = path.join(
__dirname,
"../views",
"Email.template.handlebars"
);
const templateContent = fs.readFileSync(templatePath, "utf8");

// Compile the Handlebars template with the provided context data
const compiledHtml = Handlebars.compile(templateContent)({
user: {
_id: newNGO.NgoID, // Example ID
username: newNGO.NGOName, // Example username
email: newNGO.username, // Example email
fname: newNGO.NGOName, // Example first name
},
});
// Send an email to the admin for approval
const mailOptions = {
to: newNGO.username, // Admin's email address
subject: "New NGO Registration",
text: "A new NGO registration is pending approval. Login to the admin panel to review and approve.",
html: compiledHtml
html: compiledHtml,
// Include any necessary information in the email body
};
transporter.transporter.sendMail(mailOptions, function (error, info) {
Expand Down
132 changes: 73 additions & 59 deletions routers/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ router.post("/admin-login", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -119,7 +120,8 @@ router.post("/approve-ngo/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -156,7 +158,8 @@ router.post("/decline-ngo/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -193,7 +196,8 @@ router.post("/decline-donor/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -231,7 +235,8 @@ router.post("/accept-donor/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -267,7 +272,8 @@ router.post("/delete-complain/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -303,7 +309,8 @@ router.post("/accept-complain/:id/:userId", async (req, res) => {
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand All @@ -321,71 +328,78 @@ router.post("/accept-complain/:id/:userId", async (req, res) => {
});

//making a POST method for RESPONDE to email
router.post("/complains-response/:email/:userId", async (req, res) => {
router.post("/complains-response/:email/:userId/:id", async (req, res) => {
const email = req.params.email;
const answere = req.body.answere;
const userId = req.params.userId;
console.log(email, answere, userId);
const compId = req.params.id;

//sending email to the COMPLAIN email
try {
const transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.mail_id,
pass: process.env.pass_id,
},
});

let MailGenerator = new Mailgen({
theme: "default",
product: {
name: "PETARI",
link: "https://mailgen.js",
},
});

let response = {
body: {
name: "",
intro: "Welcome to PETARI! We're very excited to have you on board.",
action: {
instructions: answere,
button: {
color: "#22BC66", // Optional action button color
text: "Have a good day",
link: "",
//checking user EXIST or NOT
const userExist = await User.findOne({ email });
if (userExist) {
const complaint = await Query.findByIdAndUpdate(compId, {
answere,
});
} else {
//sending email to the COMPLAIN email
const transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.mail_id,
pass: process.env.pass_id,
},
});
let MailGenerator = new Mailgen({
theme: "default",
product: {
name: "PETARI",
link: "https://mailgen.js",
},
});
let response = {
body: {
name: "",
intro: "Welcome to PETARI! We're very excited to have you on board.",
action: {
instructions: answere,
button: {
color: "#22BC66",
text: "Have a good day",
link: "",
},
},
outro: "Thankyou for a part of PETARI",
},
outro: "Thankyou for a part of PETARI",
},
};

let mail = MailGenerator.generate(response);

let message = {
to: email,
subject: "Petari Support team",
html: mail,
};
};
let mail = MailGenerator.generate(response);
let message = {
to: email,
subject: "Petari Support team",
html: mail,
};
transporter
.sendMail(message)
.then(() => {
console.log("email is send successfully");
})
.catch((error) => {
console.log("Email is not send", error);
});

//DELETING complain after response because user does not exist
await Query.findByIdAndDelete(compId);
}

transporter
.sendMail(message)
.then(() => {
console.log("email is send successfully");
})
.catch((error) => {
console.log("Email is not send", error);
});
const admin = await Admin.findById(userId);
console.log("admin details in approve-ngo", admin);
const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

const query1 = await problem.find();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down
Loading

0 comments on commit 8bd2b29

Please sign in to comment.