Skip to content

Commit

Permalink
fix (170): allow all users to create and update blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
mutsinziisaac committed Dec 3, 2024
1 parent fb3ec49 commit dc75626
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 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
16 changes: 2 additions & 14 deletions src/resolvers/blogResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,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 Down Expand Up @@ -188,13 +183,6 @@ export const blogResolvers = {
throw new CustomGraphQLError("User not found or not logged in");
}

const roleName = (userWithRole.role as any)?.roleName;

// Check if user has permission to update blogs
if (!["applicant", "trainee", "admin"].includes(roleName)) {
throw new CustomGraphQLError("You do not have permission to update blogs");
}

try {
// Check if blog exists
const existingBlog = await BlogModel.findById(id);
Expand All @@ -203,7 +191,7 @@ export const blogResolvers = {
}

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

Expand Down Expand Up @@ -266,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

0 comments on commit dc75626

Please sign in to comment.