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

Added certification feature #101

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
12 changes: 12 additions & 0 deletions model/donation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose');

const donationSchema = new mongoose.Schema({
userId: mongoose.Schema.Types.ObjectId,
item: String,
quantity: Number,
donatedAt: { type: Date, default: Date.now }
});

const Donation = mongoose.model('Donation', donationSchema);

module.exports = Donation;
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
2 changes: 1 addition & 1 deletion views/UserDashBoard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
<div class="form-container">
<h2>Donation Form</h2>
<br>
<form id="donationForm" action="/add-details" method="post">
<form id="donationForm" action="/donate" method="post">
<!-- Include a field for the user ID -->
<label for="userID">Email Id:</label>
<input type="text" id="userId" name="email" >
Expand Down
147 changes: 147 additions & 0 deletions views/certificate.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Certificate of Donation</title>
<style>
body {
font-family: 'Georgia', serif;
background: linear-gradient(135deg, #f8f9fa, #eceff1);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.certificate-container {
background: white;
padding: 30px;
border: 10px solid #ffcc00;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
max-width: 750px;
width: 100%;
border-radius: 10px;
box-sizing: border-box;
text-align: center;
position: relative;
}
.certificate-header {
padding: 20px;
border-bottom: 2px solid #ffcc00;
margin-bottom: 20px;
background-color: #ffcc00;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
.certificate-title {
font-size: 2.5em;
margin: 0;
color: #ffffff;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
.certificate-subtitle {
font-size: 1.5em;
color: #ffffff;
text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
}
.certificate-body {
padding: 20px 0;
}
.certificate-body p {
font-size: 1.2em;
margin: 10px 0;
color: #333;
}
.highlight {
color: #4CAF50;
font-weight: bold;
}
.certificate-footer {
padding-top: 20px;
border-top: 2px solid #ffffff;
margin-top: 20px;
background-color: #ffffff;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.certificate-footer p {
font-size: 1em;
color:black;
}
.certificate-signature {
display: flex;
justify-content: space-around;
padding: 20px 50px;
margin-top: 20px;
}
.signature {
text-align: center;
}
.signature img {
max-height: 80px;
margin-bottom: 10px;
}
.download-btn {
margin-top: 20px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
text-decoration: none;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
transition: background-color 0.3s ease;
}
.download-btn:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="certificate-container" id="certificate">
<div class="certificate-header">
<h1 class="certificate-title">Certificate of Donation</h1>
<p class="certificate-subtitle">Presented by Petari Foundation
<img src="/img/logo.png" width="40" height="30 "alt="Signature"></p>
</div>
<div class="certificate-body">
<p>This certificate is awarded to <span class="highlight"><%= userName %></span></p>
<p>for their generous donation of</p>
<p><span class="highlight"><%= quantity %> <%= item %>(s)</span></p>
</div>
<div class="certificate-signature">
<div class="signature">
<img src="/img/logo.png" width="220" height="30 "alt="Signature">
<p>Authorized Signature</p>
</div>
<div class="signature">
<img src="/img/seal.png" alt="Seal" width="220" height="30">
<p>Petari Foundation Seal</p>
</div>
</div>
<div class="certificate-footer">
<p>Thank you for your contribution to the community!</p>
</div>
</div>
<button class="download-btn" onclick="downloadPDF()">Download Certificate</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.min.js"></script>
<script>
function downloadPDF() {
const element = document.getElementById('certificate');
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: '<%= userName %>_certificate.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
html2pdf().from(element).set(opt).save();
}
</script>
</body>
</html>