@@ -38,6 +38,8 @@ interface UpdateBlogArgs {
3838 id : string ;
3939 title ?: string ;
4040 content ?: string ;
41+ coverImage ?: string ;
42+ images ?: string [ ] ;
4143 tags ?: string [ ] ;
4244 isHidden ?: boolean ;
4345}
@@ -136,11 +138,6 @@ export const blogResolvers = {
136138 if ( ! userWithRole ) {
137139 throw new CustomGraphQLError ( "User not found or not logged in" ) ;
138140 }
139- const roleName = ( userWithRole . role as any ) ?. roleName ;
140-
141- if ( ! [ "applicant" , "trainee" ] . includes ( roleName ) ) {
142- throw new CustomGraphQLError ( "Only Trainness can create blogs" ) ;
143- }
144141 try {
145142 const existingRecord = await BlogModel . findOne ( {
146143 title : args . blogFields . title ,
@@ -167,15 +164,72 @@ export const blogResolvers = {
167164 id : { type : new GraphQLNonNull ( GraphQLID ) } ,
168165 title : { type : GraphQLString } ,
169166 content : { type : GraphQLString } ,
167+ images : { type : new GraphQLList ( GraphQLString ) } ,
168+ coverImage : { type : GraphQLString } ,
170169 tags : { type : new GraphQLList ( GraphQLString ) } ,
171170 isHidden : { type : GraphQLBoolean } ,
172171 } ,
173172 resolve : async (
174- _ : any ,
175- { id, title, content, tags, isHidden } : UpdateBlogArgs
173+ _ : any ,
174+ { id, title, content, images, coverImage, tags, isHidden } : UpdateBlogArgs ,
175+ context : any
176176 ) => {
177- const updates = { title, content, tags, isHidden } ;
178- return BlogModel . findByIdAndUpdate ( id , updates , { new : true } ) ;
177+ // Check if user is logged in and has appropriate role
178+ const userWithRole = await LoggedUserModel . findById (
179+ context . currentUser ?. _id
180+ ) . populate ( "role" ) ;
181+
182+ if ( ! userWithRole ) {
183+ throw new CustomGraphQLError ( "User not found or not logged in" ) ;
184+ }
185+
186+ try {
187+ // Check if blog exists
188+ const existingBlog = await BlogModel . findById ( id ) ;
189+ if ( ! existingBlog ) {
190+ throw new CustomGraphQLError ( "Blog not found" ) ;
191+ }
192+
193+ // Optional: Check if user is the original author
194+ if ( existingBlog . author . toString ( ) !== context . currentUser ?. _id . toString ( ) ) {
195+ throw new CustomGraphQLError ( "You can only update your own blogs" ) ;
196+ }
197+
198+ // Optional: Check for duplicate title (if title is being updated)
199+ if ( title ) {
200+ const duplicateTitleBlog = await BlogModel . findOne ( {
201+ title,
202+ _id : { $ne : id }
203+ } ) ;
204+
205+ if ( duplicateTitleBlog ) {
206+ throw new CustomGraphQLError ( "A blog with this title already exists" ) ;
207+ }
208+ }
209+
210+ // Prepare updates (only include non-null values)
211+ const updates = Object . fromEntries (
212+ Object . entries ( {
213+ title,
214+ content,
215+ images,
216+ coverImage,
217+ tags,
218+ isHidden
219+ } ) . filter ( ( [ _ , v ] ) => v != null )
220+ ) ;
221+
222+ // Update blog and return new document
223+ const updatedBlog = await BlogModel . findByIdAndUpdate (
224+ id ,
225+ updates ,
226+ { new : true }
227+ ) ;
228+
229+ return updatedBlog ;
230+ } catch ( error : any ) {
231+ throw new CustomGraphQLError ( `Failed to update blog: ${ error . message } ` ) ;
232+ }
179233 } ,
180234 } ,
181235
@@ -200,7 +254,7 @@ export const blogResolvers = {
200254
201255 const blog = await BlogModel . findById ( blogId ) ;
202256 if ( ! blog ) {
203- throw new CustomGraphQLError ( " Blog not found." ) ;
257+ throw new CustomGraphQLError ( ' Blog not found.' ) ;
204258 }
205259
206260 blog . isHidden = ! blog . isHidden ;
0 commit comments