Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
UmairFarooqLone committed Oct 20, 2023
1 parent 2bee69b commit da6d35d
Show file tree
Hide file tree
Showing 37 changed files with 1,138 additions and 271 deletions.
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"express-session": "^1.17.3",
"mongoose": "^7.4.5",
"multer": "^1.4.5-lts.1",
"path": "^0.12.7"
"path": "^0.12.7",
"zlib": "^1.0.5"
},
"devDependencies": {
"nodemon": "^3.0.1"
Expand Down
47 changes: 44 additions & 3 deletions routes/Admin/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,52 @@ const { MongoClient } = require("mongodb");
const router = express.Router();
const bcrypt = require("bcrypt");
const { connect, getCollection } = require("../../db");
const { ObjectId } = require("mongodb");

(async () => {
try {
// Get a reference to the users collection
const userCollection = await getCollection("users");
router.get("/dashboard", (req, res) => {
const productCollection = await getCollection("products");
router.get("/dashboard/users", async (req, res) => {
if (req.session.isAuthenticated && req.session.role == "admin") {
const username = req.session.username;
// Access session data here, e.g., username
res.send(`<h1>ADMIN DASHBOARD</h1>`);
const users = await userCollection.find({}).toArray();
const visitors = users.filter((user) => user.role === "visitor");
res.render("adminVisitors", { visitors: visitors });
//res.render("dashboard", { username });
} else {
// Redirect or handle unauthenticated access
res.redirect("/login");
}
});
router.get("/dashboard/company", async (req, res) => {
if (req.session.isAuthenticated && req.session.role == "admin") {
const username = req.session.username;
const users = await userCollection.find({}).toArray();
const visitors = users.filter((user) => user.role === "company");
res.render("adminCompany", { visitors: visitors });
//res.render("dashboard", { username });
} else {
// Redirect or handle unauthenticated access
res.redirect("/login");
}
});
router.get("/dashboard/products", async (req, res) => {
if (req.session.isAuthenticated && req.session.role == "admin") {
const username = req.session.username;
const products = await productCollection.find({}).toArray();

for (let product of products) {
const user = await userCollection.findOne({
_id: new ObjectId(product.createdBy),
});
if (user) {
product.CreatedByUser = user.name;
}
}
console.log(products);
res.render("adminProducts", { products: products });
//res.render("dashboard", { username });
} else {
// Redirect or handle unauthenticated access
Expand All @@ -23,3 +59,8 @@ const { connect, getCollection } = require("../../db");
}
})();
module.exports = router;
// <!-- <div class="hidden shrink-0 sm:flex sm:flex-col sm:items-end">
// <p class="text-sm leading-6 text-white">Co-Founder / CEO</p>
// <p class="mt-1 text-xs leading-5 text-white">Last seen <time datetime="2023-01-23T13:23Z">3h
// ago</time></p>
// </div> -->
50 changes: 50 additions & 0 deletions routes/Company/company.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const express = require("express");
const { MongoClient } = require("mongodb");
const router = express.Router();
const bcrypt = require("bcrypt");
const { connect, getCollection } = require("../../db");
const { ObjectId } = require("mongodb");

(async () => {
try {
const userCollection = await getCollection("users");
const productsCollection = await getCollection("products");
router.get("/company/:id", async (req, res) => {
try {
const company = await userCollection.findOne({
_id: new ObjectId(req.params.id),
});

const products = await productsCollection
.find({
createdBy: req.params.id,
})
.toArray();
// console.log(products, company);
let loggedInUser = await userCollection.findOne({
_id: new ObjectId(req.session.username),
});
let cartItemCount = 0;
if (req.session.role == "visitor") {
cartItemCount = loggedInUser.cart.length;
}

res.render("company", {
loggedIn: req.session.username ? true : false,
user: req.session,
products: products,
company: company,
logo: loggedInUser,
cartCount: cartItemCount,
});
} catch (error) {
res.status(500).send({ message: "Server error" });
}
});
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
})();

module.exports = router;
18 changes: 8 additions & 10 deletions routes/Company/coverPicture.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const express = require("express");
const { MongoClient } = require("mongodb");
const router = express.Router();
const bcrypt = require("bcrypt");
const { connect, getCollection } = require("../../db");
const { getCollection } = require("../../db");
const { ObjectId } = require("mongodb");
const multer = require("multer");
const fs = require("fs");

const storage = multer.diskStorage({
destination: function (req, file, cb) {
Expand Down Expand Up @@ -32,17 +31,16 @@ const upload = multer({ storage: storage });
// Assuming 'cover' is the name of the form field in your client-side form
const coverImage = req.file.path; // get the path of the uploaded file

// Update the user document with the cover image path
const result = await userCollection.updateOne(
{ _id: new ObjectId(id) },
{ $set: { cover: coverImage } }
);

if (result.modifiedCount === 1) {
res.redirect(`/cover/${id}`);
} else {
res.status(500).send({ message: "Failed to upload cover image" });
}
res.redirect(`/cover/${id}`);
// if (result.modifiedCount === 1) {
//
// } else {
// res.status(500).send({ message: "Failed to upload cover image" });
// }
});

router.get("/cover/:id", async (req, res) => {
Expand Down
24 changes: 11 additions & 13 deletions routes/Company/logo.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const express = require("express");
const { MongoClient } = require("mongodb");
const router = express.Router();
const bcrypt = require("bcrypt");
const { connect, getCollection } = require("../../db");
const { getCollection } = require("../../db");
const { ObjectId } = require("mongodb");
const multer = require("multer");
const fs = require("fs");
const zlib = require("zlib");

const storage = multer.diskStorage({
destination: function (req, file, cb) {
Expand Down Expand Up @@ -32,27 +32,25 @@ const upload = multer({ storage: storage });
// Assuming 'cover' is the name of the form field in your client-side form
const logoImage = req.file.path; // get the path of the uploaded file

// Update the user document with the cover image path
// Convert image to base64
// const logoImageBase64 = fs.readFileSync(logoImage, {
// encoding: "base64",
// });
// const compressedImage = zlib
// .deflateSync(Buffer.from(logoImageBase64))
// .toString("base64");
const result = await userCollection.updateOne(
{ _id: new ObjectId(id) },
{ $set: { logo: logoImage } }
);

if (result.modifiedCount === 1) {
res.redirect(`/logo/${id}`);
} else {
res.status(500).send({ message: "Failed to upload Logo image" });
}
res.redirect(`/logo/${id}`);
});

router.get("/logo/:id", async (req, res) => {
// Redirect to /
// Get the user id from the request parameters
const id = req.params.id;
let idString = id.toString();
const user = await userCollection.findOne({ _id: new ObjectId(id) });
user._id = idString;
// Render the 'cover' view and pass the user data and id to it
res.render("company/logo", { user: user });
});
} finally {
Expand Down
12 changes: 9 additions & 3 deletions routes/Mutual/landing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const bcrypt = require("bcrypt");
const { connect, getCollection } = require("../../db");
const { ObjectId } = require("mongodb");
const multer = require("multer");
const zlib = require("zlib");

const storage = multer.diskStorage({
destination: function (req, file, cb) {
Expand All @@ -23,10 +24,15 @@ const upload = multer({ storage: storage });
const productsCollection = await getCollection("products");
router.get("/", async (req, res) => {
const products = await productsCollection.find().toArray();
const users = await userCollection.find({}, { cover: 1 }).toArray();
const companyUsers = await userCollection.find({}).toArray();
const companies = companyUsers.filter((user) => user.role === "company");
const users = await userCollection
.find({}, { cover: 1, logo: 1 })
.toArray();
const covers = users
.filter((user) => user.cover)
.map((user) => user.cover);
const logos = users.filter((user) => user.logo).map((user) => user.logo);
let loggedInUser = await userCollection.findOne({
_id: new ObjectId(req.session.username),
});
Expand All @@ -35,11 +41,11 @@ const upload = multer({ storage: storage });
cartItemCount = loggedInUser.cart.length;
}

console.log(loggedInUser);
res.render("landing", {
loggedIn: req.session.username ? true : false,
user: req.session,
products: products,
companies: companies,
logos: logos,
covers: covers,
logo: loggedInUser,
cartCount: cartItemCount,
Expand Down
2 changes: 2 additions & 0 deletions routes/Visitor/addtocart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const { ObjectId } = require("mongodb");
const user = await userCollection.findOne({ _id: userId });
if (!user) {
return res.status(404).send({ message: "User not found" });
} else if (user.role != "visitor") {
return res.status(404).send({ message: "User is not a Visitor" });
}

// Initialize cart if it does not exist
Expand Down
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const coverRouter = require("./routes/Company/coverPicture");
const logoRouter = require("./routes/Company/logo");
const productRouter = require("./routes/Mutual/product");
const productsRouter = require("./routes/Company/products");
const companyRouter = require("./routes/Company/company");
const createproductsRouter = require("./routes/Company/createproduct");
const editproductsRouter = require("./routes/Company/editproduct");
const deleteproductsRouter = require("./routes/Company/deleteproduct");
Expand Down Expand Up @@ -83,6 +84,7 @@ app.use(logoRouter);
app.use(productsRouter);

app.use(productRouter);
app.use(companyRouter);

app.use(cartRouter);

Expand Down Expand Up @@ -129,5 +131,5 @@ app.use(contactusRouter);
app.use(logoutRouter);

app.listen(7000, () => {
console.log("Server listening on port 7000");
console.log("Server listening on port http://localhost:7000");
});
Binary file removed uploads/61p6R+WqedL._SX522_.jpg
Binary file not shown.
Binary file removed uploads/61q-bIO7ecL._SX522_.jpg
Binary file not shown.
Binary file removed uploads/811aBK9bUFL._SX522_.jpg
Binary file not shown.
Binary file removed uploads/Acxiom.png
Binary file not shown.
Binary file removed uploads/COVERPCSLIDE.png
Binary file not shown.
Binary file added uploads/Cover_64f2c6ca44233e3c20d52f58.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/Cover_65112b86fe52d3973e892b51.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/Logo_64f2c6ca44233e3c20d52f58.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/Logo_65112b86fe52d3973e892b51.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed uploads/Sign2.jpg
Binary file not shown.
Binary file removed uploads/image 1.jpg
Binary file not shown.
Binary file removed uploads/photo-1682687982470-8f1b0e79151a.jpeg
Binary file not shown.
Binary file removed uploads/photo-1682695796954-bad0d0f59ff1.jpeg
Binary file not shown.
Binary file removed uploads/photo-1683009427479-c7e36bbb7bca.jpeg
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added uploads/tree-736885_1280.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed uploads/urgentbacups.jpg
Binary file not shown.
Binary file not shown.
42 changes: 42 additions & 0 deletions views/adminCompany.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<style>
body {
background-color: #374151;
padding: 1rem;
}
</style>
</head>

<body>
<h1>Dashboard</h1>
<p>All Users
<ul role="list" class="divide-y divide-gray-100">
<% visitors.forEach(visitor=> { %>
<li class="flex justify-between gap-x-6 py-5">
<div class="flex min-w-0 gap-x-4">
<img class="h-12 w-12 flex-none rounded-full bg-gray-50"
src="<%= visitor.logo ? '/' + visitor.logo : 'https://static.vecteezy.com/system/resources/previews/019/896/008/original/male-user-avatar-icon-in-flat-design-style-person-signs-illustration-png.png' %>"
alt="">
<div class="min-w-0 flex-auto">
<p class="text-sm font-semibold leading-6 text-white">
<%= visitor.name %>
</p>
<p class="mt-1 truncate text-xs leading-5 text-white">
<%= visitor.email %>
</p>
</div>
</div>
</li>
<% }); %>
</ul>

</body>

</html>
Loading

0 comments on commit da6d35d

Please sign in to comment.