|
1 | 1 | import { BlogModel } from "../models/blogModel";
|
| 2 | +import mongoose from "mongoose"; |
2 | 3 |
|
3 | 4 | import {
|
4 | 5 | GraphQLBoolean,
|
@@ -45,19 +46,49 @@ interface DeleteBlogArgs {
|
45 | 46 | id: string;
|
46 | 47 | }
|
47 | 48 |
|
| 49 | +interface HideBlogArgs { |
| 50 | + id: string; |
| 51 | +} |
| 52 | + |
48 | 53 | export const blogResolvers = {
|
49 | 54 | Query: {
|
50 | 55 | getAllBlogs: {
|
51 | 56 | type: new GraphQLList(BlogType),
|
52 | 57 | args: {
|
53 | 58 | tag: { type: GraphQLString },
|
54 | 59 | },
|
55 |
| - resolve: async (_: any, { tag }: GetAllBlogsArgs) => { |
56 |
| - const filter = tag ? { tags: tag } : {}; |
57 |
| - return BlogModel.find(filter).populate("author likes comments"); |
| 60 | + resolve: async (_: any, { tag }: GetAllBlogsArgs, context: any) => { |
| 61 | + try { |
| 62 | + const userWithRole = context.currentUser |
| 63 | + ? await LoggedUserModel.findById(context.currentUser._id).populate("role") |
| 64 | + : null; |
| 65 | + |
| 66 | + const filter = tag ? { tags: tag } : {}; |
| 67 | + const blogs = await BlogModel.find(filter).populate("author likes comments"); |
| 68 | + |
| 69 | + return blogs.filter((blog) => { |
| 70 | + if (blog.isHidden) { |
| 71 | + if (userWithRole) { |
| 72 | + const authorId = blog.author._id; |
| 73 | + const currentUserId = context.currentUser._id; |
| 74 | + |
| 75 | + const isSameUser = new mongoose.Types.ObjectId(authorId).equals(new mongoose.Types.ObjectId(currentUserId)); |
| 76 | + const isAdmin = ["admin", "superAdmin"].includes((userWithRole.role as any)?.roleName); |
| 77 | + |
| 78 | + // Show hidden blog if the user is the author or has an admins role |
| 79 | + return isSameUser || isAdmin; |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | + return true; |
| 84 | + }); |
| 85 | + } catch (error: any) { |
| 86 | + throw new CustomGraphQLError(`Error fetching blogs: ${error.message}`); |
| 87 | + } |
58 | 88 | },
|
59 | 89 | },
|
60 | 90 |
|
| 91 | + |
61 | 92 | getBlogsByAuthor: {
|
62 | 93 | type: new GraphQLList(BlogType),
|
63 | 94 | args: {
|
@@ -156,5 +187,27 @@ export const blogResolvers = {
|
156 | 187 | return "Blog deleted successfully";
|
157 | 188 | },
|
158 | 189 | },
|
| 190 | + |
| 191 | + hideBlog: async (_: any, { id }: HideBlogArgs, context: any) => { |
| 192 | + |
| 193 | + const userWithRole = await LoggedUserModel.findById(context.currentUser?._id).populate("role"); |
| 194 | + |
| 195 | + if (!userWithRole || |
| 196 | + !["admin", "superAdmin"].includes((userWithRole.role as any)?.roleName)) { |
| 197 | + throw new CustomGraphQLError("You do not have permission to hide this blog."); |
| 198 | + } |
| 199 | + const blogId = new mongoose.Types.ObjectId(id); |
| 200 | + |
| 201 | + const blog = await BlogModel.findById(blogId); |
| 202 | + if (!blog) { |
| 203 | + throw new CustomGraphQLError("Blog not found."); |
| 204 | + } |
| 205 | + |
| 206 | + blog.isHidden = !blog.isHidden; |
| 207 | + |
| 208 | + await blog.save(); |
| 209 | + |
| 210 | + return blog; |
| 211 | + }, |
159 | 212 | },
|
160 | 213 | };
|
0 commit comments