Skip to content

Commit

Permalink
enhanced complaint sysytem task completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Prateek579 committed May 18, 2024
1 parent 3b8e441 commit c9fec37
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 258 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
39 changes: 24 additions & 15 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,6 +19,11 @@ 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 });

//checking NGO is APPROVED or NOT
if (ngo.approved == false) {
res.status(500).json({ messaage: "NGO is send for approval" });
}
try {
const dooner = await User.find(); // Assuming User is your Mongoose model for users

Expand Down Expand Up @@ -61,24 +66,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
125 changes: 66 additions & 59 deletions routers/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -119,7 +119,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -156,7 +156,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -193,7 +193,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -231,7 +231,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -267,7 +267,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand Down Expand Up @@ -303,7 +303,7 @@ 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();
const query1 = await problem.find({ answere: { $exists: false } });
res.render("Admin_Dashboard", {
name: admin.fullName,
email: admin.fullName,
Expand All @@ -321,71 +321,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", // Optional action button color
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
63 changes: 52 additions & 11 deletions routers/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,34 @@ router.get("/success", function (req, res) {
});

router.post("/", async function (req, res) {
const email = req.body.Email;
try {
const newQuery = new Query({
name: req.body.Fname,
email: req.body.Email,
subject: req.body.sub,
message: req.body.sms,
});
//finding the user EXIST or NOT in DATABASE
const userExist = await User.findOne({ email });
if (userExist) {
const newQuery = new Query({
name: req.body.Fname,
email: req.body.Email,
subject: req.body.sub,
message: req.body.sms,
user_id: userExist._id,
});

await newQuery.save();
console.log(newQuery);
} else {
const newQuery = new Query({
name: req.body.Fname,
email: req.body.Email,
subject: req.body.sub,
message: req.body.sms,
});

await newQuery.save();
await newQuery.save();
console.log(newQuery);
}

res.status(200).send("Successfully Received Message... Thank You!");
console.log(newQuery);
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
Expand All @@ -57,22 +73,23 @@ router.post("/", async function (req, res) {
// user login (seems to be something wrong here - user cant login even if he gives correct credentials)
router.post("/login", async function (req, res) {
const { username, password } = req.body;

console.log("user credentials", username, password);
try {
const foundUser = await User.findOne({ email: username });

console.log("user find in database", foundUser);
if (!foundUser) {
return res.render("loginError", { message: "User not found" });
}

const result = await bcrypt.compare(password, foundUser.password);

const userQuerys = await Query.find({ user_id: foundUser._id });
if (result) {
return res.render("UserDashBoard", {
fullName: foundUser.fullName,
email: foundUser.email,
phoneNo: foundUser.Mobile,
address: foundUser.address,
complain: userQuerys,
});
} else {
return res.render("loginError", { message: "Incorrect password" });
Expand Down Expand Up @@ -414,4 +431,28 @@ router.post(async (req, res) => {
}
});

//making a POST method for DELETING the COMPLAINt
router.post("/delete-query/:id/:email", async (req, res) => {
const compId = req.params.id;
const email = req.params.email;
try {
const dele = await Query.findByIdAndDelete(compId);

//rendering USER DASHBOARD
const foundUser = await User.findOne({ email });
const userQuerys = await Query.find({ user_id: foundUser._id });

return res.render("UserDashBoard", {
fullName: foundUser.fullName,
email: foundUser.email,
phoneNo: foundUser.Mobile,
address: foundUser.address,
complain: userQuerys,
});
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

module.exports = router;
Loading

0 comments on commit c9fec37

Please sign in to comment.