Skip to content

Commit

Permalink
Merge pull request #101 from Tarunag03/main
Browse files Browse the repository at this point in the history
Added certification feature .. thank you for your contribution
  • Loading branch information
Sahil1786 authored May 17, 2024
2 parents ebabe57 + 22c965f commit 7333325
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 76 deletions.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ app.set('view engine', 'ejs');
const userRouter=require("./routers/userRoutes")
const NgoRouter=require("./routers/NgoRoutes")
const adminRouter=require("./routers/adminRoutes")
const donationRouter=require("./routers/donationRoutes");

//api endpoints
app.use(userRouter)
app.use(NgoRouter)
app.use(adminRouter)

app.use(donationRouter)


app.listen( process.env.port|| 3000,function(){
Expand Down
Binary file added public/img/seal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions routers/donationRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const express = require("express");
const router = express.Router();
const pdf = require('html-pdf');
//const PDFDocument = require('pdfkit');
const fs = require('fs');
const path = require('path');
const PDFDocument = require('pdfkit');

const User = require("../model/user");
const Donation = require("../model/donation");
const { METHODS } = require("http");

// Donation route to save donation details and generate certificate
router.post("/donate", async (req, res) => {
try {
// Find the user by their email
let user = await User.findOne({ email: req.body.email });

// If the user exists, update their details
if (user) {
// Update the user's details
user.flatNo = req.body.flatNo;
user.addressLine1 = req.body.addressLine1;
user.addressLine2 = req.body.addressLine2;
user.city = req.body.city;
user.state = req.body.state;
user.zip = req.body.zip;
user.foodInventory.push({
foodItem: req.body.foodItem,
quantity: req.body.quantity,
});
let item=req.body.foodItem;
let quantity=req.body.quantity;
// Save the updated user document
const saveDetails = await user.save();
if (saveDetails) {
res.render('certificate', { userName: user.fullName,quantity:quantity,item:item });
}
}
} catch (error) {
console.error("Error adding details:", error);
res.status(500).json({ error: "Internal server error" });
}

});





module.exports = router;
36 changes: 1 addition & 35 deletions routers/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ router.get("/", function (req, res) {
});

router.get("/user_login", function (req, res) {
res.render("user_login");
res.render("user_login.ejs");
});

router.get("/Ngo_login", function (req, res) {
Expand Down Expand Up @@ -160,40 +160,6 @@ router.post(async (req, res) => {
}
});

// extra details added for the user
router.post("/add-details", async (req, res) => {
try {
// Find the user by their email
let user = await User.findOne({ email: req.body.email });

// If the user exists, update their details
if (user) {
// Update the user's details
user.flatNo = req.body.flatNo;
user.addressLine1 = req.body.addressLine1;
user.addressLine2 = req.body.addressLine2;
user.city = req.body.city;
user.state = req.body.state;
user.zip = req.body.zip;
user.foodInventory.push({
foodItem: req.body.foodItem,
quantity: req.body.quantity,
});

// Save the updated user document
const saveDetails = await user.save();
if (saveDetails) {
res.status(200).json({ message: "Details added successfully" });
console.log("added details", saveDetails);
}
} else {
res.status(404).json({ error: "User not found" });
}
} catch (error) {
console.error("Error adding details:", error);
res.status(500).json({ error: "Internal server error" });
}
});

// user delete from the databse
router.post("/delete-details/:email/:ngoEmail", async (req, res) => {
Expand Down
156 changes: 116 additions & 40 deletions views/UserDashBoard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -364,54 +364,66 @@
<div class="form-container">
<h2>Donation Form</h2>
<br>
<form id="donationForm" action="/add-details" method="post">
<!-- Include a field for the user ID -->
<label for="userID">Email Id:</label>
<input type="text" id="userId" name="email" >

<form id="donationForm" action="/donate" method="post">
<label for="userId">Email Id:</label>
<input type="email" id="userId" name="email" required>
<span id="emailError" class="error"></span>
<label for="flatNo">Flat/Door/Building No.:</label>
<input type="text" id="flatNo" name="flatNo" required>
<span id="flatNoError" class="error"></span>

<label for="addressLine1">Street/Locality:</label>
<input type="text" id="addressLine1" name="addressLine1" required>
<span id="addressLine1Error" class="error"></span>

<label for="addressLine2">Area/Landmark (Optional):</label>
<input type="text" id="addressLine2" name="addressLine2">

<label for="city">City:</label>
<input type="text" id="city" name="city" required>
<span id="cityError" class="error"></span>

<label for="state">State:</label>
<select id="state" name="state" required>
<option value="">Select State</option>
<option value="AP">Andhra Pradesh</option>
<option value="AR">Arunachal Pradesh</option>
<option value="AS">Assam</option>
<option value="BR">Bihar</option>
<option value="CH">Chhattisgarh</option>
<option value="GA">Goa</option>
<option value="GU">Gujarat</option>
<option value="HR">Haryana</option>
<option value="HP">Himachal Pradesh</option>
<option value="JH">Jharkhand</option>
<option value="KA">Karnataka</option>
<option value="KL">Kerala</option>
<option value="MP">Madhya Pradesh</option>
<option value="MH">Maharashtra</option>
<option value="MN">Manipur</option>
<option value="ME">Meghalaya</option>
<option value="MZ">Mizoram</option>
<option value="NL">Nagaland</option>
<option value="OD">Odisha</option>
<option value="PB">Punjab</option>
<option value="RJ">Rajasthan</option>
<option value="SK">Sikkim</option>
<option value="TN">Tamil Nadu</option>
<option value="TG">Telangana</option>
<option value="TR">Tripura</option>
<option value="UT">Uttarakhand</option>
<option value="UP">Uttar Pradesh</option>
<option value="WB">West Bengal</option>
</select>

<option value="">Select State</option>
<option value="AP">Andhra Pradesh</option>
<option value="AR">Arunachal Pradesh</option>
<option value="AS">Assam</option>
<option value="BR">Bihar</option>
<option value="CH">Chhattisgarh</option>
<option value="GA">Goa</option>
<option value="GU">Gujarat</option>
<option value="HR">Haryana</option>
<option value="HP">Himachal Pradesh</option>
<option value="JH">Jharkhand</option>
<option value="KA">Karnataka</option>
<option value="KL">Kerala</option>
<option value="MP">Madhya Pradesh</option>
<option value="MH">Maharashtra</option>
<option value="MN">Manipur</option>
<option value="ME">Meghalaya</option>
<option value="MZ">Mizoram</option>
<option value="NL">Nagaland</option>
<option value="OD">Odisha</option>
<option value="PB">Punjab</option>
<option value="RJ">Rajasthan</option>
<option value="SK">Sikkim</option>
<option value="TN">Tamil Nadu</option>
<option value="TG">Telangana</option>
<option value="TR">Tripura</option>
<option value="UT">Uttarakhand</option>
<option value="UP">Uttar Pradesh</option>
<option value="WB">West Bengal</option>

</select>
<span id="stateError" class="error"></span>

<label for="zip">ZIP/Postal Code:</label>
<input type="text" id="zip" name="zip" required>

<input type="text" id="zip" name="zip" required pattern="[0-9]{6}">
<span id="zipError" class="error"></span>

<h3>Food Inventory</h3>
<table>
<thead>
Expand All @@ -434,9 +446,7 @@
<div style="display: flex; justify-content: center;">
<button class="button" type="submit">Donate</button>
</div>
</form>
</div>
</div>
</form>
</section>


Expand Down Expand Up @@ -534,6 +544,72 @@ function deleteRow() {
alert("Cannot delete the only row");
}
}
function validateForm() {
var isValid = true;
// Validate Email
var email = document.getElementById("userId").value;
var emailError = document.getElementById("emailError");
if (!email.match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)) {
emailError.textContent = "Please enter a valid email address";
isValid = false;
} else {
emailError.textContent = "";
}
// Validate Flat/Door/Building No.
var flatNo = document.getElementById("flatNo").value;
var flatNoError = document.getElementById("flatNoError");
if (!flatNo.match(/^[\w\s]*$/)) {
flatNoError.textContent = "Please enter a valid flat/door/building number";
isValid = false;
} else {
flatNoError.textContent = "";
}
// Validate Street/Locality
var addressLine1 = document.getElementById("addressLine1").value;
var addressLine1Error = document.getElementById("addressLine1Error");
if (addressLine1 === "") {
addressLine1Error.textContent = "This field is required";
isValid = false;
} else {
addressLine1Error.textContent = "";
}
// Validate City
var city = document.getElementById("city").value;
var cityError = document.getElementById("cityError");
if (!city.match(/^[\w\s]*$/)) {
cityError.textContent = "Please enter a valid city name";
isValid = false;
} else {
cityError.textContent = "";
}
// Validate State
var state = document.getElementById("state").value;
var stateError = document.getElementById("stateError");
if (state === "") {
stateError.textContent = "Please select a state";
isValid = false;
} else {
stateError.textContent = "";
}
// Validate ZIP/Postal Code
var zip = document.getElementById("zip").value;
var zipError = document.getElementById("zipError");
if (!zip.match(/^\d{6}$/)) {
zipError.textContent = "Please enter a valid ZIP/Postal code (6 digits)";
isValid = false;
} else {
zipError.textContent = "";
}
return isValid;
}
</script>
</body>

Expand Down
Loading

0 comments on commit 7333325

Please sign in to comment.