Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Food Donation Tracking in Petari #172

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ const userSchema = new mongoose.Schema({
},
],
approved: { type: Boolean, default: false },
acceptedBy: { type: String },
status: {
type: String,
enum: ["packed", "accepted", "onRoad", "delivered"],
default: "packed",
},
destinaion: { type: String },
// googleId: String,
// profile: String,
resetTokenExpiration: Date,
resetToken:String
resetToken: String,
});

const User = new mongoose.model("User", userSchema);
Expand Down
14 changes: 7 additions & 7 deletions routers/NgoRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ router.post("/NGO-Registarion", async (req, res) => {
html: compiledHtml,
// Include any necessary information in the email body
};
transporter.transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
// transporter.transporter.sendMail(mailOptions, function (error, info) {
// if (error) {
// console.log(error);
// } else {
// console.log("Email sent: " + info.response);
// }
// });

console.log("NGO registration request sent for approval");
// res
Expand Down
97 changes: 92 additions & 5 deletions routers/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,24 @@ router.post("/login", async function (req, res) {
const result = await bcrypt.compare(password, foundUser.password);

const userQuerys = await Query.find({ user_id: foundUser._id });

const donationInfo = {
food: foundUser.foodInventory,
city: foundUser.city,
flat: foundUser.flatNo,
destination: foundUser.destinaion,
acceptedBy: foundUser.acceptedBy,
status: foundUser.status,
};

if (result) {
return res.render("UserDashBoard", {
fullName: foundUser.fullName,
email: foundUser.email,
phoneNo: foundUser.Mobile,
address: foundUser.address,
complain: userQuerys,
donationInfo: donationInfo,
});
} else {
return res.render("loginError", { message: "Incorrect password" });
Expand Down Expand Up @@ -190,9 +201,13 @@ router.post("/approve-donation/:email/:ngoEmail", async (req, res) => {
// Find the user by their email
let user = await User.findOne({ email: req.params.email });

const ngo1 = await NGO.findOne({ username: ngoEmail });

// If the user exists, approved their donation
if (user) {
if (user && ngo1) {
user.approved = true;
user.acceptedBy = ngo1.NGOName;
user.destinaion = ngo1.NgoLocation;

// Save the updated user document
await user.save();
Expand Down Expand Up @@ -255,7 +270,7 @@ router.post("/User_singUp", async function (req, res) {
});

// creating a message for USER
const message = `Thank you, ${(fullName).toUpperCase()}, for connecting with the PETARI organization.`
const message = `Thank you, ${fullName.toUpperCase()}, for connecting with the PETARI organization.`;

await newUser.save().then((user) => {
let mailOptions = {
Expand Down Expand Up @@ -319,7 +334,7 @@ router.post("/User_singUp", async function (req, res) {
});

router.route("/forgot-password-user").get(async (req, res) => {
res.render("forget-password",{role:"user"});
res.render("forget-password", { role: "user" });
});

//send Email for the reset password
Expand Down Expand Up @@ -406,7 +421,7 @@ router.route("/reset-password-user").get(async (req, res) => {
// Process the decoded token (e.g., extract information from it)
console.log(decodedToken);
// Continue with the reset-password logic
res.render("set_password", { email, token,role:"user" });
res.render("set_password", { email, token, role: "user" });
} catch (error) {
// Handle JWT verification errors
console.error("JWT verification error:", error.message);
Expand Down Expand Up @@ -454,7 +469,7 @@ router.route("/reset-password-user").post(async (req, res) => {
email: user.email,
phoneNo: user.Mobile,
address: user.address,
complain:userQuerys
complain: userQuerys,
});

// Redirect to login page or any other desired page
Expand Down Expand Up @@ -494,4 +509,76 @@ router.post("/delete-query/:id/:email", async (req, res) => {
}
});

// updating the donation status
router.post("/donation-status/:email", async (req, res) => {
const email = req.params.email;

try {
const user = await User.findOne({ email });
if (user) {
const currentIndex = user.status ? user.status : "packed";
const statuses = ["packed", "accepted", "onRoad", "delivered"];
const currentIndexInArray = statuses.indexOf(currentIndex);
const nextIndexInArray = currentIndexInArray + 1;

// Ensure next index is within the array bounds
if (nextIndexInArray < statuses.length) {
const nextStatus = statuses[nextIndexInArray];
if (req.body.roadType === nextStatus) {
user.status = req.body.roadType;
await user.save();

if (
req.body.roadType === "packed" ||
req.body.roadType === "onRoad"
) {
const donationInfo = {
food: user.foodInventory,
city: user.city,
flat: user.flatNo,
destination: user.destinaion,
acceptedBy: user.acceptedBy,
status: user.status,
};
const userQuerys = await Query.find({ user_id: user._id });
return res.render("UserDashBoard", {
fullName: user.fullName,
email: user.email,
phoneNo: user.Mobile,
address: user.address,
complain: userQuerys,
donationInfo: donationInfo,
});
} else {
const dooner = await User.find();
const ngo = await NGO.findOne({ NGOName: user.acceptedBy });

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(400).json({ message: "Invalid status update" });
}
} else {
res.status(400).json({
message: "Cannot update to next status, already at the final status",
});
}
} else {
res.status(404).json({ message: "User not found" });
}
} catch (error) {
console.log("donation-status error", error);
res.status(500).json({ message: "Internal server error" });
}
});

module.exports = router;
Loading