Skip to content

Commit

Permalink
removed hard coded url
Browse files Browse the repository at this point in the history
  • Loading branch information
Somnath-Chattaraj committed Oct 7, 2024
1 parent a1468d4 commit 9ee34c6
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 39 deletions.
1 change: 1 addition & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3000';
240 changes: 237 additions & 3 deletions backend/dist/controllers/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,74 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPost = exports.getCommunities = void 0;
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"));
// @ts-ignore
const searchPosts = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { query } = req.body;
if (!query) {
return res.status(400).json({ message: "Search query is required" });
}
const posts = yield prisma_1.default.post.findMany({
select: {
post_id: true,
title: true,
content: true,
College: {
select: {
name: true,
},
},
},
});
const fuse = new fuse_js_1.default(posts, {
keys: ["title", "content", "College.name"],
threshold: 0.6,
});
const searchResults = fuse.search(query).map((result) => result.item);
return res.status(200).json({ posts: searchResults });
}));
exports.searchPosts = searchPosts;
// @ts-ignore
const getCommunities = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
// @ts-ignore
const user_id = req.user.user_id;
// return res.status(200).json({ message: "Communities fetched" });
// const user_id = "clzfffeey0000e6hx5j16b96c";
const communities = yield prisma_1.default.userCourse.findMany({
where: {
user_id: user_id,
},
select: {
college_id: true,
College: {
select: {
name: true,
},
},
},
distinct: ["college_id"],
});
return res.status(200).json({ communities });
// @ts-ignore
const communityIds = communities.map((community) => community.college_id);
// @ts-ignore
if (communityIds.length === 0) {
return res.status(200).json({ communityIds: [] });
}
let college = [];
for (let i = 0; i < communityIds.length; i++) {
college[i] = yield prisma_1.default.college.findUnique({
where: {
college_id: communityIds[i],
},
select: {
college_id: true,
name: true,
},
});
}
return res.status(200).json({ college });
}));
exports.getCommunities = getCommunities;
// @ts-ignore
Expand All @@ -52,8 +102,192 @@ const createPost = (0, express_async_handler_1.default)((req, res, next) => __aw
title,
content,
user_id,
college_id: collegeId,
},
});
return res.status(201).json({ post });
}));
exports.createPost = createPost;
// @ts-ignore
const fetchPosts = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { page } = req.body;
const pageNumber = page;
const postsPerPage = 3;
const offset = (pageNumber - 1) * postsPerPage;
const posts = yield prisma_1.default.post.findMany({
orderBy: {
createdAt: "desc",
},
select: {
post_id: true,
title: true,
content: true,
likes: true,
College: {
select: {
name: true,
},
},
},
take: postsPerPage,
skip: offset,
});
const totalPosts = yield prisma_1.default.post.count();
const isOver = offset + postsPerPage >= totalPosts;
return res.status(200).json({ posts, isOver });
}));
exports.fetchPosts = fetchPosts;
// @ts-ignore
const likePost = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { postId } = req.body;
// @ts-ignore
const post = yield prisma_1.default.post.findUnique({
where: { post_id: postId },
select: {
likes: true,
},
});
// @ts-ignore
const user_id = req.user.user_id;
const like = yield prisma_1.default.like.findFirst({
where: {
post_id: postId,
user_id: user_id,
},
});
if (like) {
return res.status(400).json({ message: "Post already liked" });
}
if (!post) {
return res.status(404).json({ message: "Post not found" });
}
const likes = post.likes + 1;
const updatedPost = yield prisma_1.default.post.update({
where: { post_id: postId },
data: {
likes,
},
});
yield prisma_1.default.like.create({
data: {
post_id: postId,
user_id,
},
});
return res.status(200).json({ updatedPost });
}));
exports.likePost = likePost;
// @ts-ignore
const fetchSinglePost = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const postId = req.params.id;
const post = yield prisma_1.default.post.findUnique({
where: { post_id: postId },
select: {
post_id: true,
title: true,
content: true,
likes: true,
College: {
select: {
name: true,
},
},
Comments: {
select: {
comment_id: true,
content: true,
user_id: true,
User: {
select: {
name: true,
},
},
},
},
},
});
if (!post) {
return res.status(404).json({ message: "Post not found" });
}
return res.status(200).json({ post });
}));
exports.fetchSinglePost = fetchSinglePost;
// @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
const user_id = req.user.user_id;
if (!postId || !content) {
return res.status(400).json({ message: "Required fields are missing" });
}
const user = yield prisma_1.default.user.findUnique({
where: { user_id },
});
if (!user) {
return res.status(404).json({ message: "User not recognized" });
}
const comment = yield prisma_1.default.comment.create({
data: {
content,
user_id,
post_id: postId,
},
});
return res.status(201).json({ comment });
}));
exports.createComment = createComment;
// @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
const user_id = req.user.user_id;
const likes = yield prisma_1.default.like.findFirst({
where: {
post_id: postId,
user_id: user_id,
},
});
if (likes) {
return res.status(200).json({ postLiked: true });
}
return res.status(200).json({ postLiked: false });
}));
exports.postLiked = postLiked;
// @ts-ignore
const unlikePost = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { postId } = req.body;
// @ts-ignore
const user_id = req.user.user_id;
const like = yield prisma_1.default.like.findFirst({
where: {
post_id: postId,
user_id: user_id,
},
});
const post = yield prisma_1.default.post.findUnique({
where: { post_id: postId },
select: {
likes: true,
},
});
if (!like) {
return res.status(400).json({ message: "Post not liked" });
}
if (!post) {
return res.status(404).json({ message: "Post not found" });
}
const likes = post.likes - 1;
const updatedPost = yield prisma_1.default.post.update({
where: { post_id: postId },
data: {
likes,
},
});
yield prisma_1.default.like.delete({
where: {
like_id: like.like_id,
},
});
return res.status(200).json({ updatedPost });
}));
exports.unlikePost = unlikePost;
53 changes: 48 additions & 5 deletions backend/dist/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,53 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addCourseToUser = exports.getUserDetailsById = exports.getCurrentUserDetails = exports.verifyUser = exports.loginUser = exports.registerUser = void 0;
exports.googleSignInOrSignUp = exports.addCourseToUser = exports.getUserDetailsById = exports.getCurrentUserDetails = exports.verifyUser = exports.loginUser = exports.registerUser = void 0;
const express_async_handler_1 = __importDefault(require("express-async-handler"));
const prisma_1 = __importDefault(require("../lib/prisma"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const bcrypt_1 = __importDefault(require("bcrypt"));
const sendMail_1 = __importDefault(require("../mail/sendMail"));
const academic_email_verifier_1 = require("academic-email-verifier");
const checkAcademic_1 = __importDefault(require("../mail/checkAcademic"));
//@ts-ignore
const googleSignInOrSignUp = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { email, displayName } = req.body;
const user = yield prisma_1.default.user.findUnique({
where: {
email,
},
});
if (!user) {
const user = yield prisma_1.default.user.create({
// @ts-ignore
data: {
email,
name: displayName,
collegeEmailVerified: false,
emailVerified: true,
},
});
const exp = Date.now() + 1000 * 60 * 60 * 24 * 30;
// @ts-ignore
const token = jsonwebtoken_1.default.sign({ sub: user.user_id, exp }, process.env.SECRET);
res.cookie("Authorization", token, {
httpOnly: true,
secure: false,
sameSite: "lax",
});
return res.status(201).json({ message: "User created" });
}
const exp = Date.now() + 1000 * 60 * 60 * 24 * 30;
// @ts-ignore
const token = jsonwebtoken_1.default.sign({ sub: user.user_id, exp }, process.env.SECRET);
res.cookie("Authorization", token, {
httpOnly: true,
secure: false,
sameSite: "lax",
});
res.status(200).json({ message: "User logged in" });
}));
exports.googleSignInOrSignUp = googleSignInOrSignUp;
// @ts-ignore
const registerUser = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { email, name, password, collegeName, courseName, isOnline, location } = req.body;
Expand Down Expand Up @@ -99,10 +138,10 @@ const registerUser = (0, express_async_handler_1.default)((req, res) => __awaite
const exp = Date.now() + 1000 * 60 * 5;
// @ts-ignore
const token = jsonwebtoken_1.default.sign({ sub: user.user_id, exp }, process.env.SECRET);
const url = `https://api-statuscode1.wedevelopers.online/api/user/verify/${token}`;
const url = `${process.env.BACKEND_URL}/api/user/verify/${token}`;
const htmlContent = `<a href="${url}">Verify using this link</a>`;
// @ts-ignore
yield (0, sendMail_1.default)(email, htmlContent);
(0, sendMail_1.default)(htmlContent, email);
res.status(201).json({ message: "User created" });
}
else {
Expand All @@ -117,10 +156,10 @@ const registerUser = (0, express_async_handler_1.default)((req, res) => __awaite
const exp = Date.now() + 1000 * 60 * 5;
// @ts-ignore
const token = jsonwebtoken_1.default.sign({ sub: user.user_id, exp }, process.env.SECRET);
const url = `https://api-statuscode1.wedevelopers.online/api/user/verify/${token}`;
const url = `${process.env.BACKEND_URL}/api/user/verify/${token}`;
const htmlContent = `<a href="${url}">Verify using this link</a>`;
// @ts-ignore
yield (0, sendMail_1.default)(email, htmlContent);
(0, sendMail_1.default)(htmlContent, email);
res.status(201).json({ message: "User created" });
}
}));
Expand Down Expand Up @@ -177,6 +216,10 @@ const loginUser = (0, express_async_handler_1.default)((req, res) => __awaiter(v
res.status(404).json({ message: "User not found" });
return;
}
if (!user.password) {
res.status(401).json({ message: "Logged in with Google Or Github" });
return;
}
const match = yield bcrypt_1.default.compare(password, user.password);
if (!match) {
res.status(401).json({ message: "Invalid credentials" });
Expand Down
1 change: 1 addition & 0 deletions backend/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const corsOptions = {
origin: [
"http://localhost:3001",
"https://app-statuscode1.wedevelopers.online",
"http://localhost:5173",
],
credentials: true,
};
Expand Down
Loading

0 comments on commit 9ee34c6

Please sign in to comment.