From 4748e442709afc00c548c1c6db74cc6416810005 Mon Sep 17 00:00:00 2001 From: tanish35 Date: Tue, 15 Oct 2024 11:09:12 +0530 Subject: [PATCH] Fix backend --- backend/dist/controllers/otpController.js | 108 ++++++++++++++++++ backend/dist/controllers/postController.js | 109 ++++++++++++++++++- backend/dist/controllers/routeControllers.js | 7 ++ backend/dist/controllers/userControllers.js | 5 +- backend/dist/index.js | 11 +- backend/dist/mail/sendMail.js | 10 +- backend/dist/routes/otpRoute.js | 12 ++ backend/dist/routes/postsRoutes.js | 2 + backend/src/controllers/postController.ts | 54 +++++++++ backend/src/routes/postsRoutes.ts | 4 + 10 files changed, 311 insertions(+), 11 deletions(-) create mode 100644 backend/dist/controllers/otpController.js create mode 100644 backend/dist/routes/otpRoute.js diff --git a/backend/dist/controllers/otpController.js b/backend/dist/controllers/otpController.js new file mode 100644 index 0000000..cdcbc94 --- /dev/null +++ b/backend/dist/controllers/otpController.js @@ -0,0 +1,108 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.changePassword = exports.verifyOtp = exports.otpGenerator = void 0; +const express_async_handler_1 = __importDefault(require("express-async-handler")); +const otp_generator_1 = __importDefault(require("otp-generator")); +const prisma_1 = __importDefault(require("../lib/prisma")); +const sendMail_1 = __importDefault(require("../mail/sendMail")); +const bcrypt_1 = __importDefault(require("bcrypt")); +exports.otpGenerator = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { email } = req.body; + const otpCode = otp_generator_1.default.generate(6, { + upperCaseAlphabets: false, + specialChars: false, + }); + try { + const user = yield prisma_1.default.user.findFirst({ + where: { + email, + }, + }); + if (!user) { + return res.status(404).json({ message: "User not found" }); + } + if (!user.emailVerified) { + return res.status(400).json({ message: "Email not verified" }); + } + const response = yield prisma_1.default.otp.create({ + data: { + otp: otpCode, + user: { + connect: { + email, + }, + }, + expiresAt: new Date(Date.now() + 10 * 60 * 1000), + }, + }); + const htmlContent = `

Your OTP is ${otpCode}

`; + yield (0, sendMail_1.default)(htmlContent, email); + res + .status(200) + .json({ message: "OTP generated successfully and email sent" }); + } + catch (error) { + console.error(error); + res.status(500).json({ message: "Error in generating OTP" }); + } +})); +exports.verifyOtp = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { otp, email } = req.body; + try { + const otpData = yield prisma_1.default.otp.findFirst({ + where: { + otp: otp, + user: { + email, + }, + expiresAt: { + gte: new Date(), + }, + }, + }); + if (otpData === null) { + return res.status(404).json({ message: "Invalid OTP" }); + } + res.status(200).json({ message: "OTP verified successfully" }); + // await prisma.otp.delete({ + // where: { + // id: otpData.id, + // }, + // }); + } + catch (error) { + console.error(error); + res.status(500).json({ message: "Error in verifying OTP" }); + } +})); +exports.changePassword = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { email, password } = req.body; + const hashedPassword = yield bcrypt_1.default.hash(password, 8); + try { + const response = yield prisma_1.default.user.update({ + where: { + email, + }, + data: { + password: hashedPassword, + }, + }); + res.status(200).json({ message: "Password changed successfully" }); + } + catch (error) { + console.error(error); + res.status(500).json({ message: "Error in changing password" }); + } +})); diff --git a/backend/dist/controllers/postController.js b/backend/dist/controllers/postController.js index d2a060d..7a7ad68 100644 --- a/backend/dist/controllers/postController.js +++ b/backend/dist/controllers/postController.js @@ -12,10 +12,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getAllCommunities = exports.searchPosts = exports.unlikePost = exports.postLiked = exports.createComment = exports.fetchSinglePost = exports.likePost = exports.fetchPosts = exports.createPost = exports.getCommunities = void 0; +exports.deleteComment = exports.deletePost = exports.getAllCommunities = exports.searchPosts = exports.unlikePost = exports.postLiked = exports.createComment = exports.fetchSinglePost = exports.likePost = exports.fetchPosts = exports.createPost = exports.getCommunities = void 0; const express_async_handler_1 = __importDefault(require("express-async-handler")); const prisma_1 = __importDefault(require("../lib/prisma")); const fuse_js_1 = __importDefault(require("fuse.js")); +const sendMail_1 = __importDefault(require("../mail/sendMail")); // @ts-ignore const searchPosts = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { const { query } = req.body; @@ -32,6 +33,12 @@ const searchPosts = (0, express_async_handler_1.default)((req, res) => __awaiter name: true, }, }, + User: { + select: { + username: true, + pic: true, + }, + }, }, }); const fuse = new fuse_js_1.default(posts, { @@ -135,6 +142,11 @@ const fetchPosts = (0, express_async_handler_1.default)((req, res) => __awaiter( pic: true, }, }, + _count: { + select: { + Comments: true, + }, + }, }, take: postsPerPage, skip: offset, @@ -202,6 +214,7 @@ const fetchSinglePost = (0, express_async_handler_1.default)((req, res) => __awa User: { select: { username: true, + pic: true, }, }, Comments: { @@ -212,6 +225,7 @@ const fetchSinglePost = (0, express_async_handler_1.default)((req, res) => __awa User: { select: { username: true, + pic: true, }, }, }, @@ -225,6 +239,30 @@ const fetchSinglePost = (0, express_async_handler_1.default)((req, res) => __awa })); exports.fetchSinglePost = fetchSinglePost; // @ts-ignore +const deletePost = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { postId } = req.body; + const post = yield prisma_1.default.post.findUnique({ + select: { + User: { + select: { + user_id: true, + }, + }, + }, + where: { post_id: postId }, + }); + // @ts-ignore + const user_id = req.user.user_id; + if (!post) { + return res.status(404).json({ message: "Post not found" }); + } + if (post.User.user_id !== user_id) { + return res.status(401).json({ message: "Unauthorized" }); + } + return res.status(200).json({ message: "Post deleted" }); +})); +exports.deletePost = deletePost; +// @ts-ignore const createComment = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { const { postId, content } = req.body; // @ts-ignore @@ -245,10 +283,79 @@ const createComment = (0, express_async_handler_1.default)((req, res) => __await post_id: postId, }, }); + const post = yield prisma_1.default.post.findUnique({ + where: { post_id: postId }, + select: { + title: true, + User: { + select: { + email: true, + user_id: true, + }, + }, + }, + }); + if (!post) { + return res.status(404).json({ message: "Post not found" }); + } + if (post.User.user_id === user_id) { + return res.status(201).json({ comment }); + } + const email = post.User.email; + const postTitle = post.title; + const commentContent = comment.content; + const commentAuthor = user.username; + const htmlContent = ` +
+

New Comment on Your Post

+ +
+

Post Title: ${postTitle}

+

A new comment has been added to your post:

+ +
+ ${commentContent} +
+ +

Commented by: ${commentAuthor}

+ View Post +
+ + +
+`; + (0, sendMail_1.default)(htmlContent, email, "New Comment on Your Post"); return res.status(201).json({ comment }); })); exports.createComment = createComment; // @ts-ignore +const deleteComment = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { commentId } = req.body; + const comment = yield prisma_1.default.comment.findUnique({ + select: { + User: { + select: { + user_id: true, + }, + }, + }, + where: { comment_id: commentId }, + }); + // @ts-ignore + const user_id = req.user.user_id; + if (!comment) { + return res.status(404).json({ message: "Comment not found" }); + } + if (comment.User.user_id !== user_id) { + return res.status(401).json({ message: "Unauthorized" }); + } + return res.status(200).json({ message: "Comment deleted" }); +})); +exports.deleteComment = deleteComment; +// @ts-ignore const postLiked = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { const { postId } = req.body; // @ts-ignore diff --git a/backend/dist/controllers/routeControllers.js b/backend/dist/controllers/routeControllers.js index eb9d054..3df8445 100644 --- a/backend/dist/controllers/routeControllers.js +++ b/backend/dist/controllers/routeControllers.js @@ -19,6 +19,13 @@ const searchRoom = (0, express_async_handler_1.default)((req, res) => __awaiter( // @ts-ignore const user_id = req.user.user_id; const rooms = yield prisma_1.default.chatRoom.findMany({ + where: { + users: { + some: { + user_id + }, + }, + }, select: { id: true, users: { diff --git a/backend/dist/controllers/userControllers.js b/backend/dist/controllers/userControllers.js index c7be914..7a6a895 100644 --- a/backend/dist/controllers/userControllers.js +++ b/backend/dist/controllers/userControllers.js @@ -68,7 +68,8 @@ const googleSignInOrSignUp = (0, express_async_handler_1.default)( sameSite: "lax", }); const username = user.username; - res.status(200).json({ isCollegeEmail, username }); + const userId = user.user_id; + res.status(200).json({ isCollegeEmail, username, userId }); })); exports.googleSignInOrSignUp = googleSignInOrSignUp; const githubSignInOrSignUp = (0, express_async_handler_1.default)( @@ -317,6 +318,7 @@ const getCurrentUserDetails = (0, express_async_handler_1.default)((req, res) => user_id: true, email: true, username: true, + pic: true, userCourses: { select: { Course: { @@ -596,6 +598,7 @@ const updateDetails = (0, express_async_handler_1.default)((req, res) => __await }, data: { username, + // @ts-ignore pic, }, }); diff --git a/backend/dist/index.js b/backend/dist/index.js index 477d762..d8c1135 100644 --- a/backend/dist/index.js +++ b/backend/dist/index.js @@ -14,14 +14,16 @@ const reviewRoutes_1 = __importDefault(require("./routes/reviewRoutes")); const ratingRoute_1 = __importDefault(require("./routes/ratingRoute")); const postsRoutes_1 = __importDefault(require("./routes/postsRoutes")); const roomRoutes_1 = __importDefault(require("./routes/roomRoutes")); +const otpRoute_1 = __importDefault(require("./routes/otpRoute")); // import { getCommunities } from "./controllers/postController"; const app = (0, express_1.default)(); app.use(express_1.default.json()); const corsOptions = { origin: [ "http://localhost:3001", - "https://app-statuscode1.wedevelopers.online", "http://localhost:5173", + "https://www.campusify.site/", + "https://app.campusify.site/", ], credentials: true, }; @@ -35,10 +37,11 @@ app.use("/api/review", reviewRoutes_1.default); app.use("/api/rating", ratingRoute_1.default); app.use("/api/chat", chatRoutes_1.default); // Use the chat routes app.use("/api/post", postsRoutes_1.default); -app.use('/api/room', roomRoutes_1.default); +app.use("/api/room", roomRoutes_1.default); +app.use("/api/otp", otpRoute_1.default); // app.get("/api/post/communities", getCommunities); -app.get('/api/logout', (req, res) => { - res.clearCookie('Authorization').json({ message: 'Logged out successfully' }); +app.get("/api/logout", (req, res) => { + res.clearCookie("Authorization").json({ message: "Logged out successfully" }); }); app.get("/", (req, res) => { res.send("Backend is running"); diff --git a/backend/dist/mail/sendMail.js b/backend/dist/mail/sendMail.js index 335107b..cae9853 100644 --- a/backend/dist/mail/sendMail.js +++ b/backend/dist/mail/sendMail.js @@ -4,14 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const nodemailer_1 = __importDefault(require("nodemailer")); -const sendMail = (htmlContent, receiverEmail) => { +const sendMail = (htmlContent, receiverEmail, subject = "Verification Email") => { const port = process.env.SMTP_PORT; const host = process.env.SMTP_HOST; const senderEmail = process.env.SMTP_EMAIL; const password = process.env.SMTP_PASSWORD; let transporter = nodemailer_1.default.createTransport({ // @ts-ignore - host: 'smtp.gmail.com', + host: "smtp.gmail.com", port: port, secure: true, auth: { @@ -22,15 +22,15 @@ const sendMail = (htmlContent, receiverEmail) => { let mailOptions = { from: `"Campus-Chatter Admin" <${senderEmail}>`, to: receiverEmail, - subject: 'OTP Verification', + subject: subject, text: htmlContent, html: htmlContent, }; transporter.sendMail(mailOptions, (error, info) => { if (error) { - return console.log('Error while sending email:', error); + return console.log("Error while sending email:", error); } - console.log('Email sent successfully:', info.response); + // console.log('Email sent successfully:', info.response); }); }; exports.default = sendMail; diff --git a/backend/dist/routes/otpRoute.js b/backend/dist/routes/otpRoute.js new file mode 100644 index 0000000..6e65359 --- /dev/null +++ b/backend/dist/routes/otpRoute.js @@ -0,0 +1,12 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = __importDefault(require("express")); +const otpController_1 = require("../controllers/otpController"); +const Otprouter = express_1.default.Router(); +Otprouter.post('/', otpController_1.otpGenerator); +Otprouter.post('/verify', otpController_1.verifyOtp); +Otprouter.post('/change', otpController_1.changePassword); +exports.default = Otprouter; diff --git a/backend/dist/routes/postsRoutes.js b/backend/dist/routes/postsRoutes.js index 4307b46..a35ec96 100644 --- a/backend/dist/routes/postsRoutes.js +++ b/backend/dist/routes/postsRoutes.js @@ -17,4 +17,6 @@ postsRoutes.post("/liked", checkAuth_1.default, postController_1.postLiked); postsRoutes.post("/unlike", checkAuth_1.default, postController_1.unlikePost); postsRoutes.post("/search", checkAuth_1.default, postController_1.searchPosts); postsRoutes.get("/allcommunities", checkAuth_1.default, postController_1.getAllCommunities); +postsRoutes.post("/deletecomment", checkAuth_1.default, postController_1.deleteComment); +postsRoutes.post("/deletepost", checkAuth_1.default, postController_1.deletePost); exports.default = postsRoutes; diff --git a/backend/src/controllers/postController.ts b/backend/src/controllers/postController.ts index 5b038c1..353866c 100644 --- a/backend/src/controllers/postController.ts +++ b/backend/src/controllers/postController.ts @@ -247,6 +247,32 @@ const fetchSinglePost = asyncHandler(async (req: Request, res: Response) => { return res.status(200).json({ post }); }); +// @ts-ignore +const deletePost = asyncHandler(async (req: Request, res: Response) => { + const { postId } = req.body; + const post = await prisma.post.findUnique({ + select: { + User: { + select: { + user_id: true, + }, + }, + }, + where: { post_id: postId }, + }); + // @ts-ignore + const user_id = req.user.user_id; + + if (!post) { + return res.status(404).json({ message: "Post not found" }); + } + if (post.User.user_id !== user_id) { + return res.status(401).json({ message: "Unauthorized" }); + } + + return res.status(200).json({ message: "Post deleted" }); +}); + // @ts-ignore const createComment = asyncHandler(async (req: Request, res: Response) => { const { postId, content } = req.body; @@ -325,6 +351,32 @@ const createComment = asyncHandler(async (req: Request, res: Response) => { return res.status(201).json({ comment }); }); +// @ts-ignore +const deleteComment = asyncHandler(async (req: Request, res: Response) => { + const { commentId } = req.body; + const comment = await prisma.comment.findUnique({ + select: { + User: { + select: { + user_id: true, + }, + }, + }, + where: { comment_id: commentId }, + }); + // @ts-ignore + const user_id = req.user.user_id; + + if (!comment) { + return res.status(404).json({ message: "Comment not found" }); + } + if (comment.User.user_id !== user_id) { + return res.status(401).json({ message: "Unauthorized" }); + } + + return res.status(200).json({ message: "Comment deleted" }); +}); + // @ts-ignore const postLiked = asyncHandler(async (req: Request, res: Response) => { const { postId } = req.body; @@ -414,4 +466,6 @@ export { unlikePost, searchPosts, getAllCommunities, + deletePost, + deleteComment, }; diff --git a/backend/src/routes/postsRoutes.ts b/backend/src/routes/postsRoutes.ts index 4f2163c..1e481ce 100644 --- a/backend/src/routes/postsRoutes.ts +++ b/backend/src/routes/postsRoutes.ts @@ -10,6 +10,8 @@ import { unlikePost, searchPosts, getAllCommunities, + deleteComment, + deletePost, } from "../controllers/postController"; import checkAuth from "../middleware/checkAuth"; @@ -25,5 +27,7 @@ postsRoutes.post("/liked", checkAuth, postLiked); postsRoutes.post("/unlike", checkAuth, unlikePost); postsRoutes.post("/search", checkAuth, searchPosts); postsRoutes.get("/allcommunities", checkAuth, getAllCommunities); +postsRoutes.post("/deletecomment", checkAuth, deleteComment); +postsRoutes.post("/deletepost", checkAuth, deletePost); export default postsRoutes;