Skip to content

Commit

Permalink
#170 An author will be able to update a blog (#171)
Browse files Browse the repository at this point in the history
* fix (170): an author will be able to update a blog

* fix (170): allow all users to create and update blogs
  • Loading branch information
mutsinziisaac authored Dec 4, 2024
1 parent effed78 commit 183d2a9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/models/blogModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const blogSchema = new Schema({

isHidden: {
type: Boolean,
default: false,
default: true,
},
author: {
type: Schema.Types.ObjectId,
Expand Down
74 changes: 64 additions & 10 deletions src/resolvers/blogResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface UpdateBlogArgs {
id: string;
title?: string;
content?: string;
coverImage?: string;
images?: string[];
tags?: string[];
isHidden?: boolean;
}
Expand Down Expand Up @@ -136,11 +138,6 @@ export const blogResolvers = {
if (!userWithRole) {
throw new CustomGraphQLError("User not found or not logged in");
}
const roleName = (userWithRole.role as any)?.roleName;

if (!["applicant", "trainee"].includes(roleName)) {
throw new CustomGraphQLError("Only Trainness can create blogs");
}
try {
const existingRecord = await BlogModel.findOne({
title: args.blogFields.title,
Expand All @@ -167,15 +164,72 @@ export const blogResolvers = {
id: { type: new GraphQLNonNull(GraphQLID) },
title: { type: GraphQLString },
content: { type: GraphQLString },
images: { type: new GraphQLList(GraphQLString) },
coverImage: { type: GraphQLString },
tags: { type: new GraphQLList(GraphQLString) },
isHidden: { type: GraphQLBoolean },
},
resolve: async (
_: any,
{ id, title, content, tags, isHidden }: UpdateBlogArgs
_: any,
{ id, title, content, images, coverImage, tags, isHidden }: UpdateBlogArgs,
context: any
) => {
const updates = { title, content, tags, isHidden };
return BlogModel.findByIdAndUpdate(id, updates, { new: true });
// Check if user is logged in and has appropriate role
const userWithRole = await LoggedUserModel.findById(
context.currentUser?._id
).populate("role");

if (!userWithRole) {
throw new CustomGraphQLError("User not found or not logged in");
}

try {
// Check if blog exists
const existingBlog = await BlogModel.findById(id);
if (!existingBlog) {
throw new CustomGraphQLError("Blog not found");
}

// Optional: Check if user is the original author
if (existingBlog.author.toString() !== context.currentUser?._id.toString()) {
throw new CustomGraphQLError("You can only update your own blogs");
}

// Optional: Check for duplicate title (if title is being updated)
if (title) {
const duplicateTitleBlog = await BlogModel.findOne({
title,
_id: { $ne: id }
});

if (duplicateTitleBlog) {
throw new CustomGraphQLError("A blog with this title already exists");
}
}

// Prepare updates (only include non-null values)
const updates = Object.fromEntries(
Object.entries({
title,
content,
images,
coverImage,
tags,
isHidden
}).filter(([_, v]) => v != null)
);

// Update blog and return new document
const updatedBlog = await BlogModel.findByIdAndUpdate(
id,
updates,
{ new: true }
);

return updatedBlog;
} catch (error : any) {
throw new CustomGraphQLError(`Failed to update blog: ${error.message}`);
}
},
},

Expand All @@ -200,7 +254,7 @@ export const blogResolvers = {

const blog = await BlogModel.findById(blogId);
if (!blog) {
throw new CustomGraphQLError("Blog not found.");
throw new CustomGraphQLError('Blog not found.');
}

blog.isHidden = !blog.isHidden;
Expand Down
2 changes: 2 additions & 0 deletions src/schema/blogSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export const blogSchema = gql`
id: ID!
title: String
content: String
images: [String]
coverImage: String
tags: [String]
): Blog!
deleteBlog(id: ID!): String!
Expand Down

0 comments on commit 183d2a9

Please sign in to comment.