@@ -73,37 +73,17 @@ const submitFeedBack = async (req, res) => {
7373/*This retrieves every single feedback (only requests that are not marked as complete requests made by the interns
7474 */
7575const getAllFeedBackRequestsForms = async ( req , res ) => {
76- const { authToken } = req . cookies ;
77- const { id, username } = jwt . verify ( authToken , process . env . JWT_SECRET ) ;
7876 try {
79- const isMentor = await User . findOne ( {
80- where : {
81- id : id ,
82- mentor : true ,
83- } ,
84- } ) ;
85- if ( isMentor ) {
86- const feedBackrequests = await FeedbackRequest . findAll ( {
87- where : {
88- status : {
89- [ Op . not ] : true ,
90- } ,
91- } ,
92- } ) ;
93- res . status ( 200 ) . json ( { data : feedBackrequests } ) ;
94- }
95- //Not sure why this was added. Might not be useful. Will revisit to make sure it's needed.
96- if ( ! isMentor ) {
77+
9778 const feedBackrequests = await FeedbackRequest . findAll ( {
9879 where : {
99- studentName : username ,
10080 status : {
10181 [ Op . not ] : true ,
10282 } ,
10383 } ,
10484 } ) ;
10585 res . status ( 200 ) . json ( { data : feedBackrequests } ) ;
106- }
86+
10787 } catch ( err ) {
10888 res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
10989 logger . error ( `Server Error` , { error : JSON . stringify ( err ) } )
@@ -184,13 +164,49 @@ const flockNotification = async (req, res) => {
184164 }
185165} ;
186166
187- //////////////////////////////////////////
188- /* Code below here are basically controller functions
189- for the Code leads or mentor endpoints
190- */
191- ///////////////////////////////////////////////
192167
193- /*This controller allows the Code leads to add feedbacks to the feedback requests made by the interns */
168+ /*This controller allows users to assign feedback requests to themselves
169+ for
170+ */
171+ const assignFeedBack = async ( req , res ) => {
172+ try {
173+ const { feedbackrequestId } = req . params ;
174+ const { authToken } = req . cookies ;
175+ const { id, username } = jwt . verify ( authToken , process . env . JWT_SECRET ) ;
176+
177+
178+ // Grab the user information based on the id for updates of the request form.
179+ const userDetail = await User . findOne ( {
180+ where : username ,
181+ where : { id : id }
182+ } )
183+
184+ // Find the specific feedback request record based on feedbackrequestId
185+ const feedbackRecord = await FeedbackRequest . findOne ( {
186+ where : { id : feedbackrequestId } ,
187+ } ) ;
188+
189+ if ( ! feedbackRecord ) {
190+ logger . error ( `Feedback record not found` , { log : JSON . stringify ( feedbackRecord ) } )
191+ return res . status ( 404 ) . json ( { msg : "Feedback record not found" } ) ;
192+ }
193+
194+ feedbackRecord . isAssigned = true ;
195+ feedbackRecord . whoisAssigned = userDetail . fName ;
196+ await feedbackRecord . save ( ) ;
197+
198+ logger . info ( `Feedback assigned to mentor` , { log : JSON . stringify ( feedbackRecord ) } )
199+ res . json ( { msg : "Feedback assigned to mentor" , data : feedbackRecord } ) ;
200+ } catch ( err ) {
201+ logger . error ( `An error occurred while updating feedback` , { log : JSON . stringify ( err ) } )
202+ res
203+ . status ( 500 )
204+ . json ( { error : "An error occurred while updating feedback" } ) ;
205+ }
206+ } ;
207+
208+
209+ /*This controller allows users to add feedbacks to the feedback requests in the queue */
194210const addFeedBack = async ( req , res ) => {
195211 try {
196212 const { feedback } = req . body ;
@@ -204,18 +220,6 @@ const addFeedBack = async (req, res) => {
204220 return res . status ( 400 ) . json ( errors ) ;
205221 }
206222
207- // Check if the user is a mentor
208- const isMentor = await User . findOne ( {
209- where : {
210- id : id ,
211- mentor : true ,
212- } ,
213- } ) ;
214-
215- if ( ! isMentor ) {
216- logger . error ( `Unauthorized user` , { log : JSON . stringify ( isMentor ) } )
217- return res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
218- }
219223
220224 // Find the specific feedback request record based on feedbackrequestId
221225 const feedbackRequest = await FeedbackRequest . findOne ( {
@@ -252,51 +256,12 @@ const addFeedBack = async (req, res) => {
252256 }
253257} ;
254258
255- /*This controller allows the Code leads to assign feedback requests to themselves
256- */
257- const assignFeedBackToMentor = async ( req , res ) => {
258- try {
259- const { feedbackrequestId } = req . params ;
260- const { authToken } = req . cookies ;
261- const { id } = jwt . verify ( authToken , process . env . JWT_SECRET ) ;
262-
263- // Check if the user is a mentor
264- const isMentor = await User . findOne ( {
265- where : {
266- id : id ,
267- mentor : true ,
268- } ,
269- } ) ;
270-
271- if ( ! isMentor ) {
272- logger . error ( `Unauthorized user` , { log : JSON . stringify ( isMentor ) } )
273- return res . status ( 401 ) . json ( { msg : "Unauthorized user" } ) ;
274- }
275-
276- // Find the specific feedback request record based on feedbackrequestId
277- const feedbackRecord = await FeedbackRequest . findOne ( {
278- where : { id : feedbackrequestId } ,
279- } ) ;
280-
281- if ( ! feedbackRecord ) {
282- logger . error ( `Feedback record not found` , { log : JSON . stringify ( feedbackRecord ) } )
283- return res . status ( 404 ) . json ( { msg : "Feedback record not found" } ) ;
284- }
285-
286- feedbackRecord . isAssigned = true ;
287- feedbackRecord . mentorId = isMentor . mentorId ;
288- feedbackRecord . whoisAssigned = isMentor . fName ;
289- await feedbackRecord . save ( ) ;
290259
291- logger . info ( `Feedback assigned to mentor` , { log : JSON . stringify ( feedbackRecord ) } )
292- res . json ( { msg : "Feedback assigned to mentor" , data : feedbackRecord } ) ;
293- } catch ( err ) {
294- logger . error ( `An error occurred while updating feedback` , { log : JSON . stringify ( err ) } )
295- res
296- . status ( 500 )
297- . json ( { error : "An error occurred while updating feedback" } ) ;
298- }
299- } ;
260+ //////////////////////////////////////////
261+ /* Code below here are basically controller functions
262+ for the Code leads or mentor endpoints
263+ */
264+ ///////////////////////////////////////////////
300265
301266/*This controller retrieves all the assigned Feedback Requests
302267of the code lead logged in.
@@ -422,7 +387,7 @@ module.exports = {
422387 submitFeedBack,
423388 getAllFeedBackRequestsForms,
424389 getUserFeedBackRequestForms,
425- assignFeedBackToMentor ,
390+ assignFeedBack ,
426391 addFeedBack,
427392 getAssignedFeedBacks,
428393 getMentorFeedback,
0 commit comments