@@ -11,6 +11,7 @@ const feedbackValidator = require("../utility/inputValidator/feedbackValidator")
1111const {
1212 studentNotification,
1313} = require ( "../utility/notifications/flockNotification" ) ;
14+ const logger = require ( '../utility/logger/logger' ) ;
1415
1516/*This controller allows the interns to request for feedback using the request
1617feedback forms.
@@ -23,8 +24,8 @@ const submitFeedBack = async (req, res) => {
2324 const { errors, validationCheck } = feedbackrequestValidator ( req . body ) ;
2425
2526 if ( ! validationCheck ) {
26- res . status ( 400 ) . json ( errors ) ;
27- return ;
27+ logger . error ( `error validating inputs: ${ JSON . stringify ( errors ) } ` )
28+ return res . status ( 400 ) . json ( errors ) ;
2829 }
2930
3031 let fullName = await User . findOne ( {
@@ -59,11 +60,14 @@ const submitFeedBack = async (req, res) => {
5960 await exerciseInfo . create ( add_User_To_ExerciseInfo_Table ) ;
6061 await FeedbackRequest . create ( feedBackRequestData ) ;
6162 studentNotification ( feedBackRequestData ) ;
63+
6264 }
6365
6466 res . status ( 200 ) . json ( { data : feedBackRequestData } ) ;
67+ logger . info ( `User submitted request successfully` , { log : JSON . stringify ( feedBackRequestData ) } ) ;
6568 } catch ( err ) {
6669 res . status ( 400 ) . json ( { msg : err . message } ) ;
70+ logger . error ( `error submitting request` , { error : JSON . stringify ( err ) } ) ;
6771 }
6872} ;
6973
@@ -89,6 +93,7 @@ const getAllFeedBackRequestsForms = async (req, res) => {
8993 } ) ;
9094 res . status ( 200 ) . json ( { data : feedBackrequests } ) ;
9195 }
96+ //Not sure why this was added. Might not be useful. Will revisit to make sure it's needed.
9297 if ( ! isMentor ) {
9398 const feedBackrequests = await FeedbackRequest . findAll ( {
9499 where : {
@@ -102,6 +107,7 @@ const getAllFeedBackRequestsForms = async (req, res) => {
102107 }
103108 } catch ( err ) {
104109 res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
110+ logger . error ( `Server Error` , { error : JSON . stringify ( err ) } )
105111 }
106112} ;
107113
@@ -115,8 +121,10 @@ const getUserFeedBackRequestForms = async (req, res) => {
115121 where : { userId : id } ,
116122 } ) ;
117123 res . status ( 200 ) . json ( { data : singleFeedBack } ) ;
124+ logger . info ( `Feedback Requests was Fetached Successfully` )
118125 } catch ( err ) {
119126 res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
127+ logger . error ( `error is ${ JSON . stringify ( err ) } ` )
120128 }
121129} ;
122130
@@ -132,6 +140,7 @@ const getMentorFeedback = async (req, res) => {
132140 } ) ;
133141
134142 if ( ! feedbackRequest ) {
143+ logger . error ( `Feedback request not found for mentor` , { error : JSON . stringify ( feedbackRequest ) } ) ;
135144 return res . status ( 404 ) . json ( { error : "Feedback request not found" } ) ;
136145 }
137146
@@ -143,6 +152,7 @@ const getMentorFeedback = async (req, res) => {
143152 res . json ( { data : allFeedbackOnFeedbackRequest } ) ;
144153 } catch ( err ) {
145154 res . status ( 500 ) . json ( { error : "Internal server error" } ) ;
155+ logger . error ( `Feedback was not fetched due to error` , { error : JSON . stringify ( error ) } )
146156 }
147157} ;
148158
@@ -168,8 +178,10 @@ const flockNotification = async (req, res) => {
168178
169179 studentNotification ( data ) ;
170180 res . status ( 200 ) . json ( { message : "Notification was sent successfully" } ) ;
181+ logger . info ( `Notification was sent successfully` , { log : JSON . stringify ( data ) } )
171182 } catch ( err ) {
172183 res . status ( 500 ) . json ( { msg : err } ) ;
184+ logger . error ( `error sending notification` , { error : JSON . stringify ( err ) } )
173185 }
174186} ;
175187
@@ -189,6 +201,7 @@ const addFeedBack = async (req, res) => {
189201 const { id } = jwt . verify ( authToken , process . env . JWT_SECRET ) ;
190202
191203 if ( ! validationCheck ) {
204+ logger . error ( `Bad Input:` , { log : JSON . stringify ( errors ) } )
192205 return res . status ( 400 ) . json ( errors ) ;
193206 }
194207
@@ -201,6 +214,7 @@ const addFeedBack = async (req, res) => {
201214 } ) ;
202215
203216 if ( ! isMentor ) {
217+ logger . error ( `Unauthorized user` , { log : JSON . stringify ( isMentor ) } )
204218 return res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
205219 }
206220
@@ -214,8 +228,8 @@ const addFeedBack = async (req, res) => {
214228 } ) ;
215229
216230 if ( ! feedbackRequest ) {
217- res . status ( 404 ) . json ( { msg : "Feedback request not found" } ) ;
218- return ;
231+ logger . error ( `Feeback request not found` , { log : JSON . stringify ( feedbackRequest ) } )
232+ return res . status ( 404 ) . json ( { msg : "not found" } ) ; ;
219233 }
220234
221235 // Create the feedback and associate it with the feedback request and mentor
@@ -227,11 +241,14 @@ const addFeedBack = async (req, res) => {
227241 } ;
228242
229243 const createdFeedback = await Feedbacks . create ( feedBackData ) ;
244+
245+ logger . info ( `Feedback added successfully` , { log : JSON . stringify ( createdFeedback ) } )
230246 res . status ( 200 ) . json ( {
231247 msg : "Feedback added successfully" ,
232248 data : createdFeedback ,
233249 } ) ;
234250 } catch ( err ) {
251+ logger . error ( `An error occurred adding feedback` , { log : JSON . stringify ( err ) } )
235252 res . status ( 500 ) . json ( { error : "An error occurred adding feedback" } ) ;
236253 }
237254} ;
@@ -253,8 +270,8 @@ const assignFeedBackToMentor = async (req, res) => {
253270 } ) ;
254271
255272 if ( ! isMentor ) {
256- res . status ( 401 ) . json ( { msg : " Unauthorized user" } ) ;
257- return ;
273+ logger . error ( ` Unauthorized user` , { log : JSON . stringify ( isMentor ) } )
274+ return res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
258275 }
259276
260277 // Find the specific feedback request record based on feedbackrequestId
@@ -263,16 +280,19 @@ const assignFeedBackToMentor = async (req, res) => {
263280 } ) ;
264281
265282 if ( ! feedbackRecord ) {
266- res . status ( 404 ) . json ( { msg : " Feedback record not found" } ) ;
267- return ;
283+ logger . error ( ` Feedback record not found` , { log : JSON . stringify ( feedbackRecord ) } )
284+ return res . status ( 404 ) . json ( { msg : "Feedback record not found" } ) ;
268285 }
269286
270287 feedbackRecord . isAssigned = true ;
271288 feedbackRecord . mentorId = isMentor . mentorId ;
272289 feedbackRecord . whoisAssigned = isMentor . fName ;
273290 await feedbackRecord . save ( ) ;
291+
292+ logger . info ( `Feedback assigned to mentor` , { log : JSON . stringify ( ) } )
274293 res . json ( { msg : "Feedback assigned to mentor" } ) ;
275294 } catch ( err ) {
295+ logger . error ( `An error occurred while updating feedback` , { log : JSON . stringify ( err ) } )
276296 res
277297 . status ( 500 )
278298 . json ( { error : "An error occurred while updating feedback" } ) ;
@@ -300,6 +320,7 @@ const getAssignedFeedBacks = async (req, res) => {
300320 } ) ;
301321
302322 if ( ! isMentor ) {
323+ logger . error ( `Unauthorized user` , { log : JSON . stringify ( isMentor ) } ) ;
303324 res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
304325 return ;
305326 }
@@ -312,6 +333,8 @@ const getAssignedFeedBacks = async (req, res) => {
312333 } ,
313334 } ,
314335 } ) ;
336+
337+ logger . info ( `List fetched successfully` , { log : JSON . stringify ( assignedList ) } )
315338 res . status ( 200 ) . json ( { data : assignedList } ) ;
316339 } catch ( err ) {
317340 console . error ( err ) ;
@@ -330,18 +353,15 @@ const getSelectedFeedback = async (req, res) => {
330353 } ) ;
331354
332355 if ( ! feedbackRequest ) {
356+ logger . error ( `Feedback request not found` , { log : JSON . stringify ( feedbackRequest ) } )
333357 res . status ( 404 ) . json ( { msg : "Feedback request not found" } ) ;
334358 return ;
335359 }
336-
360+
361+ logger . info ( `Fetched feedback successfully` , { log : JSON . stringify ( feedbackRequest ) } )
337362 res . json ( { data : feedbackRequest } ) ;
338363 } catch ( error ) {
339- console . error ( error ) ;
340- if ( error . name === "JsonWebTokenError" ) {
341- res . status ( 401 ) . json ( { msg : "Invalid authentication token" } ) ;
342- } else {
343- res . status ( 500 ) . json ( { msg : "Internal Server Error" } ) ;
344- }
364+ logger . error ( `error:` , { log : JSON . stringify ( error ) } ) ;
345365 }
346366} ;
347367
@@ -360,6 +380,7 @@ const markFeedbackRequestComplete = async (req, res) => {
360380 } ) ;
361381
362382 if ( ! isMentor ) {
383+ logger . error ( `Unauthorized user` , { log : isMentor } )
363384 res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
364385 return ;
365386 }
@@ -369,6 +390,7 @@ const markFeedbackRequestComplete = async (req, res) => {
369390 } ) ;
370391
371392 if ( ! markAsComplete ) {
393+ logger . error ( `FeedbackRequest not found` , { log : markAsComplete } )
372394 return res . status ( 404 ) . json ( { message : "FeedbackRequest not found" } ) ;
373395 }
374396
@@ -381,6 +403,7 @@ const markFeedbackRequestComplete = async (req, res) => {
381403 } ) ;
382404
383405 if ( ! exerciseInfo ) {
406+ logger . error ( `ExerciseInfo not found` , { log : exerciseInfo } )
384407 return res . status ( 404 ) . json ( { message : "ExerciseInfo not found" } ) ;
385408 }
386409
@@ -389,9 +412,9 @@ const markFeedbackRequestComplete = async (req, res) => {
389412
390413 // Update the status attribute of the FeedbackRequest record
391414 await markAsComplete . update ( { status : true } ) ;
392-
393415 res . status ( 200 ) . json ( { msg : "Exercise Marked As Complete" } ) ;
394416 } catch ( err ) {
417+ logger . error ( `Error:` , { log : JSON . stringify ( err ) } )
395418 res . status ( 500 ) . json ( { msg : err } ) ;
396419 }
397420} ;
0 commit comments