Skip to content

Commit 4fca97e

Browse files
Update hr.route.js
1 parent 1c13738 commit 4fca97e

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

Backend/routes/hr.route.js

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ router.get('/get-candidate-company', async (req, res) => {
273273
router.post('/candidate-register', upload.single('resume'), async (req, res) => {
274274
try {
275275
console.log('Candidate register request body:', req.body);
276-
const { email, name, phone, answers } = req.body;
276+
const { email, name, phone, answers, interviewId } = req.body;
277277
const resume = req.file ? req.file.path : null;
278278

279279
// Only require email
@@ -284,21 +284,48 @@ router.post('/candidate-register', upload.single('resume'), async (req, res) =>
284284
});
285285
}
286286

287-
// Define real questions
288-
const realQuestions = [
289-
{
290-
text: "What is your experience with React?",
291-
expectedAnswer: "React is a JavaScript library for building user interfaces, particularly single-page applications. It's used for handling the view layer and allows you to create reusable UI components."
292-
},
293-
{
294-
text: "Explain the concept of state management in frontend applications.",
295-
expectedAnswer: "State management refers to the management of the application state which includes user inputs, UI state, server responses, etc. Libraries like Redux, MobX, and React Context API help manage state across components."
296-
},
297-
{
298-
text: "How do you handle API calls in a React application?",
299-
expectedAnswer: "API calls in React can be handled using fetch, Axios, or other HTTP clients. These are typically made in useEffect hooks, component lifecycle methods, or through custom hooks. Async/await is commonly used for cleaner code."
287+
// Try to find existing interview with this candidate
288+
let existingHr = null;
289+
let existingInterview = null;
290+
291+
if (interviewId) {
292+
existingHr = await Hr.findOne({ 'interviews._id': interviewId });
293+
if (existingHr) {
294+
existingInterview = existingHr.interviews.find(i => i._id.toString() === interviewId);
295+
}
296+
}
297+
298+
// If no existing interview found, check if any HR has this candidate
299+
if (!existingInterview) {
300+
existingHr = await Hr.findOne({ 'interviews.candidates.email': email });
301+
if (existingHr) {
302+
existingInterview = existingHr.interviews.find(i => i.candidates.some(c => c.email === email));
300303
}
301-
];
304+
}
305+
306+
// If we found an existing interview, use its questions
307+
let realQuestions = [];
308+
if (existingInterview && existingInterview.questions && existingInterview.questions.length > 0) {
309+
console.log('Using existing interview questions:', existingInterview.questions.map(q => q.text));
310+
realQuestions = existingInterview.questions;
311+
} else {
312+
// Fallback to default questions if no existing interview found
313+
console.log('Using default questions');
314+
realQuestions = [
315+
{
316+
text: "What is your experience with React?",
317+
expectedAnswer: "React is a JavaScript library for building user interfaces, particularly single-page applications. It's used for handling the view layer and allows you to create reusable UI components."
318+
},
319+
{
320+
text: "Explain the concept of state management in frontend applications.",
321+
expectedAnswer: "State management refers to the management of the application state which includes user inputs, UI state, server responses, etc. Libraries like Redux, MobX, and React Context API help manage state across components."
322+
},
323+
{
324+
text: "How do you handle API calls in a React application?",
325+
expectedAnswer: "API calls in React can be handled using fetch, Axios, or other HTTP clients. These are typically made in useEffect hooks, component lifecycle methods, or through custom hooks. Async/await is commonly used for cleaner code."
326+
}
327+
];
328+
}
302329

303330
try {
304331
// Find or create a demo HR user
@@ -389,7 +416,7 @@ router.post('/candidate-register', upload.single('resume'), async (req, res) =>
389416
// Continue even if DB operations fail
390417
}
391418

392-
// Return success response with real questions from the interview
419+
// Return success response with real questions
393420
return res.json({
394421
success: true,
395422
message: 'Candidate registered successfully',
@@ -399,8 +426,8 @@ router.post('/candidate-register', upload.single('resume'), async (req, res) =>
399426
phone: phone || 'Not provided',
400427
resumeUploaded: !!resume
401428
},
402-
questions: interview.questions.map(q => ({ text: q.text })),
403-
interviewId: interview._id
429+
questions: realQuestions.map(q => ({ text: q.text })),
430+
interviewId: existingInterview ? existingInterview._id : (interview ? interview._id : null)
404431
});
405432

406433
} catch (error) {

0 commit comments

Comments
 (0)