@@ -147,4 +147,78 @@ router.get('/get-candidate-company', async (req, res) => {
147147 }
148148} ) ;
149149
150+ // ✅ POST: Register candidate (missing endpoint)
151+ router . post ( '/candidate-register' , upload . single ( 'resume' ) , async ( req , res ) => {
152+ try {
153+ const { interviewId, email, name, phone } = req . body ;
154+ const resume = req . file ? req . file . path : null ;
155+
156+ if ( ! interviewId || ! email ) {
157+ return res . status ( 400 ) . json ( {
158+ success : false ,
159+ message : 'Interview ID and email are required'
160+ } ) ;
161+ }
162+
163+ // Find the HR that has this interview
164+ const hr = await Hr . findOne ( { 'interviews._id' : interviewId } ) ;
165+ if ( ! hr ) {
166+ return res . status ( 404 ) . json ( {
167+ success : false ,
168+ message : 'Interview not found'
169+ } ) ;
170+ }
171+
172+ // Find the interview
173+ const interviewIndex = hr . interviews . findIndex ( i => i . _id . toString ( ) === interviewId ) ;
174+ if ( interviewIndex === - 1 ) {
175+ return res . status ( 404 ) . json ( {
176+ success : false ,
177+ message : 'Interview not found'
178+ } ) ;
179+ }
180+
181+ const interview = hr . interviews [ interviewIndex ] ;
182+
183+ // Check if candidate already exists
184+ const candidateIndex = interview . candidates . findIndex ( c => c . email === email ) ;
185+ if ( candidateIndex === - 1 ) {
186+ // Add new candidate if not found
187+ interview . candidates . push ( {
188+ email,
189+ name : name || '' ,
190+ phone : phone || '' ,
191+ resume : resume || '' ,
192+ status : 'pending' ,
193+ interviewLink : `https://neorecruiter.vercel.app/interview?id=${ interview . _id } &email=${ encodeURIComponent ( email ) } `
194+ } ) ;
195+ } else {
196+ // Update existing candidate
197+ hr . interviews [ interviewIndex ] . candidates [ candidateIndex ] . name = name || hr . interviews [ interviewIndex ] . candidates [ candidateIndex ] . name || '' ;
198+ hr . interviews [ interviewIndex ] . candidates [ candidateIndex ] . phone = phone || hr . interviews [ interviewIndex ] . candidates [ candidateIndex ] . phone || '' ;
199+ if ( resume ) {
200+ hr . interviews [ interviewIndex ] . candidates [ candidateIndex ] . resume = resume ;
201+ }
202+ }
203+
204+ // Save the changes
205+ hr . markModified ( 'interviews' ) ;
206+ await hr . save ( ) ;
207+
208+ // Return success
209+ res . json ( {
210+ success : true ,
211+ message : 'Candidate registered successfully' ,
212+ questions : interview . questions . map ( q => ( { text : q . text } ) )
213+ } ) ;
214+ } catch ( error ) {
215+ console . error ( 'Error registering candidate:' , error ) ;
216+ res . status ( 500 ) . json ( {
217+ success : false ,
218+ message : 'Server error' ,
219+ error : error . message
220+ } ) ;
221+ }
222+ } ) ;
223+
150224module . exports = router ;
0 commit comments