251 lines
7.0 KiB
TypeScript
251 lines
7.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import * as candidateService from '@/lib/services/candidateService';
|
|
|
|
// Demo data for when MongoDB is not available
|
|
const DEMO_CANDIDATES = [
|
|
{
|
|
_id: "1",
|
|
name: "John Doe",
|
|
email: "john.doe@example.com",
|
|
phone: "123-456-7890",
|
|
technology: "React, Node.js",
|
|
visaStatus: "H1B",
|
|
location: "New York, NY",
|
|
openToRelocate: true,
|
|
experience: "5 years",
|
|
currentStatus: "In marketing",
|
|
status: "Active",
|
|
marketingStartDate: "2023-04-15",
|
|
dayRecruiter: "Jane Smith",
|
|
nightRecruiter: "Mike Johnson",
|
|
},
|
|
{
|
|
_id: "2",
|
|
name: "Jane Smith",
|
|
email: "jane.smith@example.com",
|
|
phone: "234-567-8901",
|
|
technology: "Angular, Python",
|
|
visaStatus: "Green Card",
|
|
location: "San Francisco, CA",
|
|
openToRelocate: false,
|
|
experience: "3 years",
|
|
currentStatus: "In marketing",
|
|
status: "Active",
|
|
marketingStartDate: "2023-04-10",
|
|
dayRecruiter: "Bob Williams",
|
|
nightRecruiter: "Alice Brown",
|
|
},
|
|
{
|
|
_id: "3",
|
|
name: "Mike Johnson",
|
|
email: "mike.johnson@example.com",
|
|
phone: "345-678-9012",
|
|
technology: "Java, Spring",
|
|
visaStatus: "OPT",
|
|
location: "Austin, TX",
|
|
openToRelocate: true,
|
|
experience: "2 years",
|
|
currentStatus: "In marketing",
|
|
status: "Active",
|
|
marketingStartDate: "2023-04-05",
|
|
dayRecruiter: "Jane Smith",
|
|
nightRecruiter: "Bob Williams",
|
|
},
|
|
{
|
|
_id: "4",
|
|
name: "Sarah Williams",
|
|
email: "sarah.williams@example.com",
|
|
phone: "456-789-0123",
|
|
technology: "Python, Django",
|
|
visaStatus: "H1B",
|
|
location: "Chicago, IL",
|
|
openToRelocate: true,
|
|
experience: "4 years",
|
|
currentStatus: "Hold",
|
|
status: "Hold",
|
|
marketingStartDate: "2023-03-25",
|
|
dayRecruiter: "Mike Johnson",
|
|
nightRecruiter: "Jane Smith",
|
|
},
|
|
{
|
|
_id: "5",
|
|
name: "David Brown",
|
|
email: "david.brown@example.com",
|
|
phone: "567-890-1234",
|
|
technology: "React, Redux, TypeScript",
|
|
visaStatus: "Citizen",
|
|
location: "Seattle, WA",
|
|
openToRelocate: false,
|
|
experience: "6 years",
|
|
currentStatus: "Placed",
|
|
status: "Inactive",
|
|
marketingStartDate: "2023-03-15",
|
|
dayRecruiter: "Alice Brown",
|
|
nightRecruiter: "Bob Williams",
|
|
}
|
|
];
|
|
|
|
// GET all candidates, with optional filtering
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const status = searchParams.get('status');
|
|
const technology = searchParams.get('technology');
|
|
const visaStatus = searchParams.get('visaStatus');
|
|
const openToRelocate = searchParams.get('openToRelocate');
|
|
const currentStatus = searchParams.get('currentStatus');
|
|
const search = searchParams.get('search');
|
|
const limit = Number(searchParams.get('limit') || '50');
|
|
const page = Number(searchParams.get('page') || '1');
|
|
|
|
try {
|
|
// Try to get candidates using the service
|
|
const result = await candidateService.getCandidates({
|
|
status,
|
|
technology,
|
|
visaStatus,
|
|
openToRelocate: openToRelocate === 'true',
|
|
currentStatus,
|
|
search,
|
|
limit,
|
|
page
|
|
});
|
|
console.log('mongo result', result);
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
console.log("MongoDB connection error, using demo candidates", error);
|
|
|
|
// Fall back to demo data
|
|
let candidates = [...DEMO_CANDIDATES];
|
|
|
|
// Filter demo data based on query params
|
|
if (status) {
|
|
candidates = candidates.filter(c => c.status === status);
|
|
}
|
|
|
|
if (technology) {
|
|
candidates = candidates.filter(c =>
|
|
c.technology.toLowerCase().includes(technology.toLowerCase())
|
|
);
|
|
}
|
|
|
|
if (visaStatus) {
|
|
candidates = candidates.filter(c => c.visaStatus === visaStatus);
|
|
}
|
|
|
|
if (openToRelocate) {
|
|
candidates = candidates.filter(c =>
|
|
c.openToRelocate === (openToRelocate === 'true')
|
|
);
|
|
}
|
|
|
|
if (currentStatus) {
|
|
candidates = candidates.filter(c => c.currentStatus === currentStatus);
|
|
}
|
|
|
|
if (search) {
|
|
const query = search.toLowerCase();
|
|
candidates = candidates.filter(c =>
|
|
c.name.toLowerCase().includes(query) ||
|
|
c.email.toLowerCase().includes(query) ||
|
|
c.technology.toLowerCase().includes(query)
|
|
);
|
|
}
|
|
|
|
const total = candidates.length;
|
|
const skip = (page - 1) * limit;
|
|
candidates = candidates.slice(skip, skip + limit);
|
|
|
|
return NextResponse.json({
|
|
candidates,
|
|
pagination: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit)
|
|
}
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching candidates:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch candidates' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// POST a new candidate
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
// Get the session to associate the candidate with the user
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (!session?.user) {
|
|
return NextResponse.json(
|
|
{ error: 'Authentication required' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
|
|
try {
|
|
// Try to create a candidate using the service
|
|
const newCandidate = await candidateService.createCandidate(
|
|
body,
|
|
session.user.id
|
|
);
|
|
|
|
return NextResponse.json(
|
|
{ message: 'Candidate created successfully', candidate: newCandidate },
|
|
{ status: 201 }
|
|
);
|
|
} catch (error) {
|
|
// Check if it's a duplicate email error
|
|
if ((error as Error).message === 'A candidate with this email already exists') {
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message },
|
|
{ status: 409 }
|
|
);
|
|
}
|
|
|
|
console.log("MongoDB connection error, handling demo creation", error);
|
|
|
|
// Check for duplicate in demo data
|
|
const existingCandidate = DEMO_CANDIDATES.find(c => c.email === body.email);
|
|
if (existingCandidate) {
|
|
return NextResponse.json(
|
|
{ error: 'A candidate with this email already exists' },
|
|
{ status: 409 }
|
|
);
|
|
}
|
|
|
|
// Create demo candidate with generated ID
|
|
const newCandidate = {
|
|
_id: (DEMO_CANDIDATES.length + 1).toString(),
|
|
...body,
|
|
createdBy: session.user.id,
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString()
|
|
};
|
|
|
|
// Add to demo candidates array
|
|
DEMO_CANDIDATES.push(newCandidate);
|
|
|
|
return NextResponse.json(
|
|
{ message: 'Candidate created successfully', candidate: newCandidate },
|
|
{ status: 201 }
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating candidate:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create candidate' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|