@@ -42,6 +42,8 @@ const upload = multer({
4242const questionModel = require ( "../models/questionModel" ) ;
4343const userModel = require ( "../models/userModel" ) ;
4444const imageModel = require ( "../models/imageModel" ) ;
45+ //import the controllers
46+ const notificationController = require ( "../controllers/notificationController" ) ;
4547
4648//-------------------------------------------------------------------------------------------------------------------------------------
4749
@@ -302,43 +304,56 @@ const answerQ = asyncHandler(async (req, res) => {
302304
303305 const body = req . body [ "answers" ] ;
304306 console . log ( "body" , body ) ;
305- const um = await userModel . findOne ( { user_ID : body . user_ID } ) ;
306- if ( ! um ) {
307- res . status ( 401 ) . json ( { error :"User not found" } )
307+ const um = await userModel . findOne ( { user_ID : req . body . user_ID } ) ;
308+ if ( ! um ) {
309+ return res . status ( 401 ) . json ( { error : "User not found" } ) ;
308310 }
309311 console . log ( "user model" , um ) ;
310312 let verified = false ;
311313 if ( um . role === 5980 ) {
312314 verified = true ;
313315 }
314316 const message = "Successfully answered the question" ;
317+ // Update the question with the provided answer
315318 await questionModel
316- . updateOne (
317- { _id : req . params . qid } , // this line is to find the question with the id
319+ . findByIdAndUpdate (
320+ req . params . qid ,
318321 {
319322 $push : {
320- answers : [
321- {
322- body : body . body ,
323- user_ID : body . user_ID ,
324- user_Name : um . name ,
325- images : savedImages ,
326- verified : verified ,
327- } ,
328- ] ,
323+ answers : {
324+ body : req . body . body ,
325+ user_ID : req . body . user_ID ,
326+ user_Name : um . name ,
327+ images : savedImages ,
328+ verified : verified ,
329+ } ,
329330 } ,
330331 $set : { status : true } ,
331- }
332+ } ,
333+ { new : true } // To return the updated document
332334 )
333- . then ( ( data ) => {
334- res . json ( { data, message} ) ;
335+ . then ( async ( updatedQuestion ) => {
336+ // To check if the question was found and updated
337+ if ( ! updatedQuestion ) {
338+ return res . status ( 404 ) . json ( { error : "Question not found" } ) ;
339+ }
340+ // Notify the student that their question has been answered
341+ const studentId = updatedQuestion . user_ID ;
342+ const answererId = req . body . user_ID ;
343+ const notifMessage = "Your question has been answered" ;
344+ notificationController . createNotification (
345+ answererId ,
346+ [ studentId ] ,
347+ req . params . qid ,
348+ notifMessage
349+ ) ;
350+ res . json ( { data : updatedQuestion , message } ) ;
335351 } ) ;
336352 } ) ;
337353 } catch ( err ) {
338- res . status ( 400 ) . json ( { message : "Error occured while answering the question" } ) ;
354+ res . status ( 400 ) . json ( { message : "Error occurred while answering the question" } ) ;
339355 }
340356} ) ;
341-
342357//Commenting
343358//Commenting on a question
344359//no point of keeping track of comments
@@ -347,42 +362,46 @@ const commentQ = asyncHandler(async (req, res) => {
347362 try {
348363 const cID = new mongoose . Types . ObjectId ( ) ;
349364 const body = req . body [ "comments" ] ;
350- const um = await userModel . findOne ( { user_ID : body . user_ID } ) ;
351- if ( ! um ) {
352- res . status ( 401 ) . json ( { error :"User not found" } )
365+ const um = await userModel . findOne ( { user_ID : req . body . user_ID } ) ;
366+ if ( ! um ) {
367+ return res . status ( 401 ) . json ( { error : "User not found" } ) ;
353368 }
354369 console . log ( "user model" , um ) ;
355370 const message = "Successfully commented on the question" ;
356- await questionModel
357- . updateOne (
358- { _id : req . params . qid } ,
359- {
360- $push : {
361- comments : [
362- {
363- _id : cID ,
364- body : body . body ,
365- user_ID : body . user_ID ,
366- user_Name : um . name ,
367- } ,
368- ] ,
371+ // Update the question document by pushing the new comment
372+ await questionModel . findByIdAndUpdate (
373+ req . params . qid ,
374+ {
375+ $push : {
376+ comments : {
377+ _id : cID ,
378+ body : req . body . body ,
379+ user_ID : req . body . user_ID ,
380+ user_Name : um ? um . name : 'Unknown' , // Handle case where user is not found
369381 } ,
370- }
371- )
372- . then ( async ( data ) => {
373- console . log ( cID . valueOf ( ) ) ;
374- //inserting posted comment id to user model
375-
376- const temp = um . question_comments . concat ( [
377- { questionID : req . params . qid , commentID : cID . valueOf ( ) } ,
378- ] ) ;
379- um . question_comments = temp ;
380- await um . save ( ) ;
381- res . json ( { data, message} ) ;
382- } ) ;
382+ } ,
383+ } ,
384+ ) ;
385+ //Notify the student that someone commented on their question
386+ const studentid = ( await questionModel . findById ( req . params . qid ) ) . user_ID ;
387+ const senderid = req . body . user_ID ;
388+ const notifMessage = "Your question has been commented on" ;
389+ notificationController . createNotification (
390+ senderid ,
391+ [ studentid ] ,
392+ req . params . qid ,
393+ notifMessage
394+ ) ;
395+ // Insert the posted comment ID to user model
396+ const temp = um . question_comments . concat ( [
397+ { questionID : req . params . qid , commentID : cID . valueOf ( ) } ,
398+ ] ) ;
399+ um . question_comments = temp ;
400+ await um . save ( ) ;
401+ res . json ( { message } ) ;
383402 } catch ( err ) {
384403 console . log ( err ) ;
385- res . status ( 400 ) . res . json ( { message : "Error occured while commenting on the question" } ) ;
404+ res . status ( 400 ) . json ( { message : "Error occurred while commenting on the question" } ) ;
386405 }
387406} ) ;
388407
0 commit comments