@@ -38,6 +38,8 @@ interface UpdateBlogArgs {
38
38
id : string ;
39
39
title ?: string ;
40
40
content ?: string ;
41
+ coverImage ?: string ;
42
+ images ?: string [ ] ;
41
43
tags ?: string [ ] ;
42
44
isHidden ?: boolean ;
43
45
}
@@ -136,11 +138,6 @@ export const blogResolvers = {
136
138
if ( ! userWithRole ) {
137
139
throw new CustomGraphQLError ( "User not found or not logged in" ) ;
138
140
}
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
- }
144
141
try {
145
142
const existingRecord = await BlogModel . findOne ( {
146
143
title : args . blogFields . title ,
@@ -167,15 +164,72 @@ export const blogResolvers = {
167
164
id : { type : new GraphQLNonNull ( GraphQLID ) } ,
168
165
title : { type : GraphQLString } ,
169
166
content : { type : GraphQLString } ,
167
+ images : { type : new GraphQLList ( GraphQLString ) } ,
168
+ coverImage : { type : GraphQLString } ,
170
169
tags : { type : new GraphQLList ( GraphQLString ) } ,
171
170
isHidden : { type : GraphQLBoolean } ,
172
171
} ,
173
172
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
176
176
) => {
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
+ }
179
233
} ,
180
234
} ,
181
235
@@ -200,7 +254,7 @@ export const blogResolvers = {
200
254
201
255
const blog = await BlogModel . findById ( blogId ) ;
202
256
if ( ! blog ) {
203
- throw new CustomGraphQLError ( " Blog not found." ) ;
257
+ throw new CustomGraphQLError ( ' Blog not found.' ) ;
204
258
}
205
259
206
260
blog . isHidden = ! blog . isHidden ;
0 commit comments