0% found this document useful (0 votes)
5 views

HR backend

Uploaded by

abhilash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

HR backend

Uploaded by

abhilash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

To create the backend structure for your HR Dashboard component, you'll need to

build RESTful APIs to handle various operations such as fetching jobs, candidates,
interviews, and company statuses, as well as handling job postings. Here's how you
can structure the backend using Node.js, Express, and MongoDB:

1. Project Setup

Install necessary packages:


bash

npm init -y
npm install express mongoose body-parser cors

2. Create the Server (server.js)


javascript

const express = require('express');


const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();


app.use(cors());
app.use(bodyParser.json());

// MongoDB connection
mongoose.connect('mongodb://localhost/hr_dashboard', {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

// Import routes
const jobsRouter = require('./routes/jobs');
const candidatesRouter = require('./routes/candidates');
const interviewsRouter = require('./routes/interviews');
const companyStatusRouter = require('./routes/companyStatus');

// Use routes
app.use('/api/jobs', jobsRouter);
app.use('/api/candidates', candidatesRouter);
app.use('/api/interviews', interviewsRouter);
app.use('/api/company-status', companyStatusRouter);

// Start the server


const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
3. Define MongoDB Schemas and Models
Job Model (models/Job.js)
javascript

const mongoose = require('mongoose');

const jobSchema = new mongoose.Schema({


title: String,
companyName: String,
location: String,
eligibility: String,
skillsRequired: [String],
experience: String,
minSalary: Number,
maxSalary: Number,
workingDays: String,
jobType: String,
startDate: Date,
endDate: Date,
description: String,
updatedOn: { type: Date, default: Date.now },
});

module.exports = mongoose.model('Job', jobSchema);


Candidate Model (models/Candidate.js)
javascript

const mongoose = require('mongoose');

const candidateSchema = new mongoose.Schema({


name: String,
jobTitle: String,
companyName: String,
experience: String,
skills: [String],
contact: String,
email: String,
resumeLink: String,
status: String,
});

module.exports = mongoose.model('Candidate', candidateSchema);


Interview Model (models/Interview.js)
javascript

const mongoose = require('mongoose');

const interviewSchema = new mongoose.Schema({


name: String,
jobTitle: String,
companyName: String,
skills: [String],
experience: String,
interviewDate: Date,
time: String,
status: String,
});

module.exports = mongoose.model('Interview', interviewSchema);


CompanyStatus Model (models/CompanyStatus.js)
javascript

const mongoose = require('mongoose');

const companyStatusSchema = new mongoose.Schema({


jobTitle: String,
companyName: String,
location: String,
eligibility: String,
skillsRequired: [String],
experience: String,
minSalary: Number,
maxSalary: Number,
workingDays: String,
jobType: String,
updatedOn: { type: Date, default: Date.now },
});

module.exports = mongoose.model('CompanyStatus', companyStatusSchema);


4. Create Routes
Jobs Routes (routes/jobs.js)
javascript

const express = require('express');


const router = express.Router();
const Job = require('../models/Job');

// Get all jobs


router.get('/', async (req, res) => {
try {
const jobs = await Job.find();
res.json(jobs);
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Create a new job


router.post('/', async (req, res) => {
const job = new Job(req.body);
try {
const newJob = await job.save();
res.status(201).json(newJob);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

module.exports = router;
Candidates Routes (routes/candidates.js)
javascript

const express = require('express');


const router = express.Router();
const Candidate = require('../models/Candidate');

// Get all candidates


router.get('/', async (req, res) => {
try {
const candidates = await Candidate.find();
res.json(candidates);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// Create a new candidate
router.post('/', async (req, res) => {
const candidate = new Candidate(req.body);
try {
const newCandidate = await candidate.save();
res.status(201).json(newCandidate);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

module.exports = router;

Interviews Routes (routes/interviews.js)


javascript

const express = require('express');


const router = express.Router();
const Interview = require('../models/Interview');

// Get all interviews


router.get('/', async (req, res) => {
try {
const interviews = await Interview.find();
res.json(interviews);
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Create a new interview


router.post('/', async (req, res) => {
const interview = new Interview(req.body);
try {
const newInterview = await interview.save();
res.status(201).json(newInterview);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

module.exports = router;

Company Status Routes (routes/companyStatus.js)


javascript

const express = require('express');


const router = express.Router();
const CompanyStatus = require('../models/CompanyStatus');

// Get company status


router.get('/', async (req, res) => {
try {
const statuses = await CompanyStatus.find();
res.json(statuses);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// Create a new company status
router.post('/', async (req, res) => {
const status = new CompanyStatus(req.body);
try {
const newStatus = await status.save();
res.status(201).json(newStatus);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

module.exports = router;

5. Testing and Integration


Use tools like Postman to test the API endpoints.
Ensure that the frontend fetch calls in your React component are correctly pointing
to the backend API routes.
This backend structure will provide all the necessary CRUD operations for your HR
Dashboard component. Each API route corresponds to the specific data being managed
(jobs, candidates, interviews, and company status).

Complete HRserver.js Code:

javascript

const express = require('express');


const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();

// Middleware
app.use(cors());
app.use(bodyParser.json());

// Database Connection
mongoose.connect('mongodb://localhost:27017/hrportal', { useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('Failed to connect to MongoDB', err));

// Job Schema and Model


const jobSchema = new mongoose.Schema({
title: String,
description: String,
location: String,
company: String,
salary: String,
postedDate: { type: Date, default: Date.now },
});

const Job = mongoose.model('Job', jobSchema);

// Profile Schema and Model


const profileSchema = new mongoose.Schema({
name: String,
email: String,
contactNumber: String,
position: String,
bio: String,
});

const Profile = mongoose.model('Profile', profileSchema);

// Application Schema and Model


const applicationSchema = new mongoose.Schema({
jobId: { type: mongoose.Schema.Types.ObjectId, ref: 'Job' },
applicantName: String,
applicantEmail: String,
resume: String, // Could be a file path or a link to the resume
appliedDate: { type: Date, default: Date.now },
});

const Application = mongoose.model('Application', applicationSchema);

// Job Routes

// Get all jobs


app.get('/api/jobs', async (req, res) => {
try {
const jobs = await Job.find();
res.json(jobs);
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Add a new job


app.post('/api/jobs', async (req, res) => {
const job = new Job({
title: req.body.title,
description: req.body.description,
location: req.body.location,
company: req.body.company,
salary: req.body.salary,
});

try {
const newJob = await job.save();
res.status(201).json(newJob);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

// Profile Routes
// Get all profiles
app.get('/api/profiles', async (req, res) => {
try {
const profiles = await Profile.find();
res.json(profiles);
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Add or update a profile


app.post('/api/profiles', async (req, res) => {
const { email } = req.body;

try {
let profile = await Profile.findOne({ email });

if (profile) {
// Update existing profile
profile.name = req.body.name;
profile.contactNumber = req.body.contactNumber;
profile.position = req.body.position;
profile.bio = req.body.bio;
} else {
// Create new profile
profile = new Profile({
name: req.body.name,
email: req.body.email,
contactNumber: req.body.contactNumber,
position: req.body.position,
bio: req.body.bio,
});
}

const savedProfile = await profile.save();


res.status(201).json(savedProfile);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

// Application Routes

// Get all applications


app.get('/api/applications', async (req, res) => {
try {
const applications = await Application.find().populate('jobId');
res.json(applications);
} catch (err) {
res.status(500).json({ message: err.message });
}
});

// Submit a job application


app.post('/api/applications', async (req, res) => {
const application = new Application({
jobId: req.body.jobId,
applicantName: req.body.applicantName,
applicantEmail: req.body.applicantEmail,
resume: req.body.resume,
});

try {
const newApplication = await application.save();
res.status(201).json(newApplication);
} catch (err) {
res.status(400).json({ message: err.message });
}
});

// Listen on a port
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

Explanation:
Job Schema and Routes:

The job schema includes fields like title, description, location, company, and
salary.
Routes allow fetching all jobs and adding a new job.
Profile Schema and Routes:

The profile schema includes fields like name, email, contactNumber, position, and
bio.
Routes allow fetching all profiles and adding/updating a profile.
Application Schema and Routes:

The application schema links to a job using jobId and includes fields like
applicantName, applicantEmail, and resume.
Routes allow fetching all applications (with job details populated) and submitting
a job application.
Running the Server:
Ensure MongoDB is running on your system or provide a MongoDB connection string.
Install necessary packages using:

npm install express mongoose body-parser cors

Start the server using:

node HRserver.js
This server will handle routes for jobs, profiles, and applications all in one
place.

You might also like