@@ -10,58 +10,37 @@ const UserTokenRequest = require("../models/TokenRequest");
1010
1111const getSupervisorForms = async ( req , res , filter ) => {
1212 try {
13- // ----------------------------
14- // Fetching A1 Form
15- // ----------------------------
16- const requests = await InternshipRequest . find ( filter )
17- . populate ( "_id" , "fullName ouEmail soonerId" ) ;
18-
19- const typedRequests = requests . map ( req => ( {
20- ...req . toObject ( ) , // convert Mongoose doc to plain JS object
21- form_type : "A1" // add the custom type
13+ const InternshipRequest = require ( "../models/InternshipRequest" ) ;
14+ const WeeklyReport = require ( "../models/WeeklyReport" ) ;
15+ const Evaluation = require ( "../models/Evaluation" ) ;
16+
17+ const a1Forms = await InternshipRequest . find ( filter ) . populate ( "student" , "fullName ouEmail soonerId" ) ;
18+ const typedA1 = a1Forms . map ( ( form ) => ( {
19+ ...form . toObject ( ) ,
20+ form_type : "A1" ,
2221 } ) ) ;
2322
24- // ----------------------------
25- // Fetching A2 Form
26- // ----------------------------
27- const reports = await WeeklyReport . find ( filter )
28- . populate ( "student_id" , "fullName ouEmail soonerId" ) ;
29-
30- // Adding custom type to A2 Form
31- const typedReports = reports . map ( report => ( {
32- ...report . toObject ( ) , // convert Mongoose doc to plain JS object
33- form_type : "A2" // add the custom type
23+ const a2Forms = await WeeklyReport . find ( filter ) . populate ( "student_id" , "fullName ouEmail soonerId" ) ;
24+ const typedA2 = a2Forms . map ( ( form ) => ( {
25+ ...form . toObject ( ) ,
26+ form_type : "A2" ,
3427 } ) ) ;
3528
36- // ----------------------------
37- // Fetching A3 Form
38- // ----------------------------
39- const evaluations = await Evaluation . find ( filter )
40- . populate ( "student_id" , "fullName ouEmail soonerId" ) ;
41-
42- // Adding custom type to A3 Form
43- const typedEvaluations = evaluations . map ( evaluation => ( {
44- ...evaluation . toObject ( ) , // convert Mongoose doc to plain JS object
45- form_type : "A3" // add the custom type
29+ const a3Forms = await Evaluation . find ( filter ) . populate ( "student_id" , "fullName ouEmail soonerId" ) ;
30+ const typedA3 = a3Forms . map ( ( form ) => ( {
31+ ...form . toObject ( ) ,
32+ form_type : "A3" ,
4633 } ) ) ;
4734
48- // ----------------------------
49- // Combine forms
50- // ----------------------------
51- const allRequests = [ ...typedRequests , ...typedReports , ...typedEvaluations ] ;
35+ const allForms = [ ...typedA1 , ...typedA2 , ...typedA3 ] ;
36+ allForms . sort ( ( a , b ) => new Date ( b . createdAt ) - new Date ( a . createdAt ) ) ;
5237
53- // Sort by createdAt date
54- allRequests . sort ( ( a , b ) => new Date ( b . createdAt ) - new Date ( a . createdAt ) ) ;
55-
56- // Send response
57- res . status ( 200 ) . json ( allRequests ) ;
38+ return res . status ( 200 ) . json ( allForms ) ;
5839 } catch ( err ) {
59- res . status ( 500 ) . json ( {
60- message : "Failed to fetch supervisor forms" ,
61- error : err . message ,
62- } ) ;
40+ console . error ( "Error in getSupervisorForms:" , err . message ) ;
41+ return res . status ( 500 ) . json ( { message : "Failed to fetch supervisor forms" , error : err . message } ) ;
6342 }
64- }
43+ } ;
6544
6645const handleSupervisorFormAction = async ( req , res , action ) => {
6746 try {
@@ -89,26 +68,18 @@ const handleSupervisorFormAction = async (req, res, action) => {
8968 supervisor_comment : comment ,
9069 } ;
9170
92- const form = await FormModel . findByIdAndUpdate ( formId , update , { new : true } ) . populate ( "student_id" , "userName email" ) ;
71+ const form = await FormModel . findByIdAndUpdate ( formId , update , { new : true } )
72+ . populate ( "student_id" , "userName email" ) ;
9373
9474 if ( ! form ) {
9575 return res . status ( 404 ) . json ( { message : "Form not found" } ) ;
9676 }
9777
98- const studentEmail =
99- form . student_id ?. email ||
100- form . interneeEmail ||
101- form . studentEmail ||
102- null ;
103-
104- if ( ! studentEmail ) {
105- console . warn ( "⚠️ No student email found for form:" , form . _id ) ;
106- } else {
107- const emailSubject = `Form ${ action === "approve" ? "Approved" : "Rejected" } ` ;
78+ const studentEmail = form . student_id ?. email || form . interneeEmail || form . studentEmail || null ;
79+ let emailSubject = `Form ${ action === "approve" ? "Approved" : "Rejected" } ` ;
10880 let emailBody = `<p>Your ${ form_type } form has been ${ action } ed by the supervisor.</p>` ;
10981 if ( comment ) {
11082 emailBody += `<p>Comment: ${ comment } </p>` ;
111- }
11283 }
11384
11485 const student_id = form . student_id || form . internee_id || form . student ;
@@ -126,36 +97,31 @@ const handleSupervisorFormAction = async (req, res, action) => {
12697 }
12798
12899 console . log ( "Email sent to:" , student_mail ) ;
129-
130- res . status ( 200 ) . json ( {
131- message : `Form ${ action } ed successfully` ,
132- updatedForm : form ,
133- } ) ;
100+ res . status ( 200 ) . json ( { message : `Form ${ action } ed successfully` , updatedForm : form } ) ;
134101 } catch ( err ) {
135102 console . error ( "SupervisorFormAction error:" , err ) ;
136103 res . status ( 500 ) . json ( { message : "Error processing form" , error : err . message } ) ;
137104 }
138105} ;
139106
140- // =========================================== //
141- // Coordinator Dashboard //
142- // =========================================== //
143-
144107const getCoordinatorRequests = async ( req , res ) => {
145108 try {
146109 const requests = await InternshipRequest . find ( {
147110 coordinator_status : "pending" ,
148111 } ) . populate ( "student" , "userName email" ) ;
112+
149113 res . status ( 200 ) . json ( requests ) ;
150114 } catch ( err ) {
151115 res . status ( 500 ) . json ( { message : "Failed to fetch requests" } ) ;
152116 }
153117} ;
154118
155- // Coordinator View Single Request
156119const getCoordinatorRequestDetails = async ( req , res ) => {
157120 try {
158- const requestData = await InternshipRequest . findById ( req . params . id ) . lean ( ) ;
121+ const requestData = await InternshipRequest . findById ( req . params . id )
122+ . populate ( "student" , "userName email" )
123+ . lean ( ) ;
124+
159125 if ( ! requestData ) {
160126 return res . status ( 404 ) . json ( { message : "Request not found" } ) ;
161127 }
@@ -168,19 +134,22 @@ const getCoordinatorRequestDetails = async (req, res) => {
168134 }
169135} ;
170136
171- // Coordinator Approve Request
172137const coordinatorApproveRequest = async ( req , res ) => {
173138 try {
174139 const request = await InternshipRequest . findByIdAndUpdate (
175140 req . params . id ,
176- { coordinator_status : "approved" } ,
141+ { status : "approved" } ,
177142 { new : true }
178- ) ;
143+ ) . populate ( "student" , "userName email" ) ;
179144
180145 if ( ! request ) {
181146 return res . status ( 404 ) . json ( { message : "Request not found" } ) ;
182147 }
183148
149+ request . coordinator_status = "Approved" ;
150+ request . coordinator_comment = "Approved by Coordinator" ;
151+ await request . save ( ) ;
152+
184153 await EmailService . sendEmail ( {
185154 to : request . student . email ,
186155 subject : "Internship Request Approved" ,
@@ -193,22 +162,25 @@ const coordinatorApproveRequest = async (req, res) => {
193162 }
194163} ;
195164
196- // Coordinator Reject Request
197165const coordinatorRejectRequest = async ( req , res ) => {
198166 const { reason } = req . body ;
199167 if ( ! reason ) return res . status ( 400 ) . json ( { message : "Reason required" } ) ;
200168
201169 try {
202170 const request = await InternshipRequest . findByIdAndUpdate (
203171 req . params . id ,
204- { coordinator_status : "rejected" } ,
172+ { status : "rejected" } ,
205173 { new : true }
206- ) ;
174+ ) . populate ( "student" , "userName email" ) ;
207175
208176 if ( ! request ) {
209177 return res . status ( 404 ) . json ( { message : "Request not found" } ) ;
210178 }
211179
180+ request . coordinator_status = "Rejected" ;
181+ request . coordinator_comment = reason ;
182+ await request . save ( ) ;
183+
212184 await EmailService . sendEmail ( {
213185 to : request . student . email ,
214186 subject : "Internship Request Rejected" ,
@@ -347,18 +319,18 @@ const deleteStalledSubmission = async (req, res) => {
347319} ;
348320
349321
350- // module.exports = {
351- // getCoordinatorRequests,
352- // getCoordinatorRequestDetails,
353- // coordinatorApproveRequest,
354- // coordinatorRejectRequest,
355- // coordinatorResendRequest,
356- // deleteStudentSubmission,
357- // getStudentSubmissions,
358- // getPendingSubmissions,
359- // getSupervisorForms,
360- // handleSupervisorFormAction,
361- // approveSubmission,
362- // rejectSubmission,
363- // deleteStalledSubmission,
364- // };
322+ module . exports = {
323+ getCoordinatorRequests,
324+ getCoordinatorRequestDetails,
325+ coordinatorApproveRequest,
326+ coordinatorRejectRequest,
327+ coordinatorResendRequest,
328+ deleteStudentSubmission,
329+ getStudentSubmissions,
330+ getPendingSubmissions,
331+ getSupervisorForms,
332+ handleSupervisorFormAction,
333+ approveSubmission,
334+ rejectSubmission,
335+ deleteStalledSubmission,
336+ } ;
0 commit comments