Skip to content

Commit 0055d90

Browse files
authored
Admin should be able to hide a blog (#153)
* define blog visibility * show blogs to unauthenticated users
1 parent 44893a9 commit 0055d90

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

Diff for: src/resolvers/blogResolvers.ts

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BlogModel } from "../models/blogModel";
2+
import mongoose from "mongoose";
23

34
import {
45
GraphQLBoolean,
@@ -45,19 +46,49 @@ interface DeleteBlogArgs {
4546
id: string;
4647
}
4748

49+
interface HideBlogArgs {
50+
id: string;
51+
}
52+
4853
export const blogResolvers = {
4954
Query: {
5055
getAllBlogs: {
5156
type: new GraphQLList(BlogType),
5257
args: {
5358
tag: { type: GraphQLString },
5459
},
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+
}
5888
},
5989
},
6090

91+
6192
getBlogsByAuthor: {
6293
type: new GraphQLList(BlogType),
6394
args: {
@@ -156,5 +187,27 @@ export const blogResolvers = {
156187
return "Blog deleted successfully";
157188
},
158189
},
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+
},
159212
},
160213
};

Diff for: src/schema/blogSchema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export const blogSchema = gql`
7878
title: String
7979
content: String
8080
tags: [String]
81-
isHidden: Boolean
8281
): Blog!
8382
deleteBlog(id: ID!): String!
83+
hideBlog(id: ID!): Blog!
8484
}
8585
`;

0 commit comments

Comments
 (0)