candidate-portal-2/src/lib/services/seedService.ts

227 lines
6.7 KiB
TypeScript

import { connectToDatabase } from '@/lib/db';
import { Candidate } from '@/models/Candidate';
import { User } from '@/models/User';
import bcrypt from 'bcryptjs';
import mongoose from 'mongoose';
/**
* Seed demo users
*/
export async function seedUsers() {
await connectToDatabase();
const users = [
{
name: 'Admin User',
email: 'admin@example.com',
password: await bcrypt.hash('password123', 10),
role: 'ADMIN',
},
{
name: 'Regular User',
email: 'user@example.com',
password: await bcrypt.hash('password123', 10),
role: 'USER',
},
];
// Clear existing users first
await User.deleteMany({});
// Create new users
const createdUsers = await User.create(users);
return createdUsers;
}
/**
* Seed demo candidates
*/
export async function seedCandidates(userId: mongoose.Types.ObjectId) {
await connectToDatabase();
const technologies = [
'React', 'Angular', 'Vue', 'Node.js', 'Python', 'Java', 'Spring',
'Django', 'Ruby on Rails', 'Flutter', 'React Native', 'iOS', 'Android',
'.NET', 'PHP', 'Laravel', 'Go', 'Rust', 'C#', 'C++', 'TypeScript'
];
const visaStatuses = ['H1B', 'Green Card', 'OPT', 'CPT', 'Citizen', 'Other'];
const currentStatuses = ['In marketing', 'Hold', 'Placed', 'Inactive'];
const statuses = ['Active', 'Hold', 'Inactive'];
const locations = [
'New York, NY', 'San Francisco, CA', 'Austin, TX', 'Chicago, IL',
'Seattle, WA', 'Boston, MA', 'Los Angeles, CA', 'Denver, CO',
'Atlanta, GA', 'Washington, DC', 'Portland, OR', 'Dallas, TX'
];
const universities = [
'Stanford University', 'MIT', 'University of California', 'Georgia Tech',
'University of Texas', 'Cornell University', 'NYU', 'Purdue University',
'Columbia University', 'University of Michigan', 'University of Illinois'
];
const companies = [
'Google', 'Microsoft', 'Amazon', 'Facebook', 'Apple', 'Netflix', 'Uber',
'Airbnb', 'Salesforce', 'Oracle', 'IBM', 'Intel', 'Cisco', 'Twitter'
];
const recruiters = [
'Jane Smith', 'Mike Johnson', 'Sarah Williams', 'Bob Williams',
'Alice Brown', 'David Wilson', 'Emily Davis', 'Alex Martinez'
];
const candidates = [];
// Generate 50 random candidates
for (let i = 0; i < 50; i++) {
const firstName = `FirstName${i}`;
const lastName = `LastName${i}`;
const name = `${firstName} ${lastName}`;
const email = `${firstName.toLowerCase()}.${lastName.toLowerCase()}@example.com`;
// Random data
const technology = sample(technologies) + ', ' + sample(technologies);
const visaStatus = sample(visaStatuses);
const location = sample(locations);
const openToRelocate = Math.random() > 0.3; // 70% chance of being open to relocate
const experience = `${Math.floor(Math.random() * 10) + 1} years`;
const currentStatus = sample(currentStatuses);
const status = sample(statuses);
const bsYear = String(2010 + Math.floor(Math.random() * 12));
const msYear = String(parseInt(bsYear, 10) + 2);
// Generate education
const education = [
{
degree: 'BS',
field: 'Computer Science',
university: sample(universities),
yearCompleted: bsYear
}
];
// 70% chance of having a Masters degree
if (Math.random() > 0.3) {
education.push({
degree: 'MS',
field: 'Computer Science',
university: sample(universities),
yearCompleted: msYear
});
}
// Generate skills
const skills = [];
const skillCount = Math.floor(Math.random() * 6) + 3; // 3 to 8 skills
for (let j = 0; j < skillCount; j++) {
const skill = sample(technologies);
if (!skills.includes(skill)) {
skills.push(skill);
}
}
// Generate interviews
const interviews = [];
const interviewCount = Math.floor(Math.random() * 3); // 0 to 2 interviews
for (let j = 0; j < interviewCount; j++) {
const company = sample(companies);
const position = `${sample(['Senior', 'Junior', 'Lead', ''])} ${sample(['Frontend', 'Backend', 'Full Stack'])} Developer`;
const date = new Date();
date.setDate(date.getDate() + Math.floor(Math.random() * 14)); // Interview in the next 14 days
const status = sample(['Scheduled', 'Completed', 'Cancelled', 'No Show']);
const feedback = status === 'Completed' ? sample(['Positive', 'Negative', 'Mixed', 'Need follow-up']) : '';
interviews.push({
company,
position,
date,
status,
feedback
});
}
// Generate assessments
const assessments = [];
const assessmentCount = Math.floor(Math.random() * 3); // 0 to 2 assessments
for (let j = 0; j < assessmentCount; j++) {
const type = sample(['Technical', 'Communication', 'Mock Interview']);
const score = `${Math.floor(Math.random() * 40) + 60}/100`; // Score between 60 and 100
const date = new Date();
date.setDate(date.getDate() - Math.floor(Math.random() * 30)); // Assessment in the last 30 days
assessments.push({
type,
score,
date,
notes: `${type} assessment completed with ${score} score.`
});
}
// Marketing start date (between 1 and 180 days ago)
const marketingStartDate = new Date();
marketingStartDate.setDate(marketingStartDate.getDate() - Math.floor(Math.random() * 180) + 1);
// Create candidate object
candidates.push({
name,
email,
phone: `${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 9000) + 1000}`,
visaStatus,
location,
openToRelocate,
experience,
technology,
bsYear,
msYear,
education,
skills,
currentStatus,
status,
marketingStartDate,
dayRecruiter: sample(recruiters),
nightRecruiter: sample(recruiters),
trainerName: sample(recruiters),
fullAddress: `${Math.floor(Math.random() * 9000) + 1000} ${sample(['Main', 'Oak', 'Maple', 'Pine', 'Cedar'])} St, ${location}`,
resumeUrl: '',
hotlisted: Math.random() > 0.7, // 30% chance of being hotlisted
interviewing: interviews,
assessment: assessments,
notes: `Candidate has ${experience} of experience in ${technology}.`,
createdBy: userId
});
}
// Clear existing candidates first
await Candidate.deleteMany({});
// Create new candidates
const createdCandidates = await Candidate.create(candidates);
return createdCandidates;
}
/**
* Helper function to get a random item from an array
*/
function sample<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}
/**
* Purge all data
*/
export async function purgeData() {
await connectToDatabase();
await Candidate.deleteMany({});
await User.deleteMany({});
return { success: true };
}