0% found this document useful (0 votes)
22 views7 pages

Page 1

This document contains code for creating, updating, deleting, and searching for job postings in a database. It defines functions for creating a new job posting by saving data to a Jobs model, updating an existing job posting by finding and updating a document by ID, deleting a job posting by ID, and searching/filtering jobs by fields like title, location, salary etc. Responses return status codes and job data or error messages.

Uploaded by

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

Page 1

This document contains code for creating, updating, deleting, and searching for job postings in a database. It defines functions for creating a new job posting by saving data to a Jobs model, updating an existing job posting by finding and updating a document by ID, deleting a job posting by ID, and searching/filtering jobs by fields like title, location, salary etc. Responses return status codes and job data or error messages.

Uploaded by

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

import { Request, Response } from "express";

import Employer, { IJobs } from "../models/jobs";


import mongoose from "mongoose";
import Jobs from "../models/jobs";
import JobApplication from "../models/jobApplication";

export async function createJob(req: Request, res: Response) {


try {
const userId = res.get("id");
const id = new mongoose.Types.ObjectId(userId);
const jobData: IJobs = req.body;
jobData.employerId = id;
jobData.keyQualifications = Array.isArray(jobData.keyQualifications) ?
jobData.keyQualifications : [];

const newJob = new Jobs();


newJob.employerId = id
const { jobBasics, jobDetails, payDetails, benifits, jobDescription,
setPreference } = jobData;
if (jobBasics) {
const { jobTitle, jobOption, jobLocation } = jobBasics;
if (jobTitle) newJob.jobBasics.jobTitle = jobTitle;
if (jobOption) newJob.jobBasics.jobOption = jobOption;
if (jobLocation) {
const { city, area, street, pincode } = jobLocation;
if (city) newJob.jobBasics.jobLocation.city = city;
if (area) newJob.jobBasics.jobLocation.area = area;
if (street) newJob.jobBasics.jobLocation.street = street;
if (pincode) newJob.jobBasics.jobLocation.pincode = pincode;
}
newJob.jobBasics.isJobBasics = true;
}

if (jobDetails) {
const { jobSchedule, jobType, numberOfPositions, recruitTimeline } =
jobDetails;
if (jobSchedule) newJob.jobDetails.jobSchedule = jobSchedule;
if (jobType) newJob.jobDetails.jobType = jobType;
if (numberOfPositions) newJob.jobDetails.numberOfPositions =
numberOfPositions
if (recruitTimeline) newJob.jobDetails.recruitTimeline =
recruitTimeline
}
if (payDetails) {
newJob.payDetails = payDetails
}
if (benifits) {
newJob.benifits = benifits
}
if (jobDescription) {
newJob.jobDescription = jobDescription
}
if (setPreference) {
newJob.setPreference = setPreference
}
await newJob.save();
res.status(201).json({
statusCode: 201,
message: "Job posted successfully",
job: newJob,
});
} catch (error) {
console.error("Error creating job:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

export async function updateJob(req: Request, res: Response) {


try {
const jobId = req.params.id;
const updatedJobData: IJobs = req.body;
updatedJobData.keyQualifications =
Array.isArray(updatedJobData.keyQualifications) ?
updatedJobData.keyQualifications : [];

const updatedJob = await Employer.findByIdAndUpdate(


jobId,
{ $set: updatedJobData },
{ new: true }
);

if (updatedJob) {
res.status(200).json({
statusCode: 200,
message: "Job updated successfully",
job: updatedJob,
});
} else {
res.status(404).json({
statusCode: 404,
message: "Job not found",
});
}
} catch (error) {
console.error("Error updating job:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

export async function getAllJobs(req: Request, res: Response) {


try {
const jobs = await Employer.find();
res.status(200).json({
statusCode: 200,
jobs: jobs,
});
} catch (error) {
console.error("Error fetching jobs:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

export async function deleteJob(req: Request, res: Response) {


try {
const _id = req.params.id;
console.log("jobId", _id)

const deletedJob = await Employer.findByIdAndDelete({ _id: _id });


console.log("deletedJob ", deletedJob)

if (deletedJob) {
res.status(200).json({
statusCode: 200,
message: "Job deleted successfully",
job: deletedJob,
});
} else {
res.status(404).json({
statusCode: 404,
message: "Job not found",
});
}
} catch (error) {
console.error("Error deleting job:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

export async function searchJobs(req: Request, res: Response) {


try {
const { keyword, location } = req.query;
let query: any = {};
if (keyword) {
query.$or = [
{ jobTitle: { $regex: new RegExp(keyword as string, "i") } },
{ jobDescription: { $regex: new RegExp(keyword as string, "i") }
},
{ companyName: { $regex: new RegExp(keyword as string, "i") } },
];
}
if (location) {
query["jobLocation.city"] = { $regex: new RegExp(location as string,
"i") };
}
const jobs = await Employer.find(query);
res.status(200).json({
statusCode: 200,
jobs: jobs,
});
} catch (error) {
console.error("Error searching jobs:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}
export async function getJobsById(req: Request, res: Response) {
try {
const employerId = res.get('id');
console.log("employerId", employerId);

const jobs = await Jobs.find({ employerId: employerId });

if (!jobs || jobs.length === 0) {


return res.status(404).json({
statusCode: 404,
message: "Jobs not found",
});
}
const jobsWithHiredCount = await Promise.all(jobs.map(async (job) => {
const hiredCount = await JobApplication.countDocuments({ jobId:
job._id, status: 'hired' });
return { ...job.toObject(), hiredCount };
}));

return res.status(200).json({
statusCode: 200,
message: "Jobs fetch successfully",
jobs: jobsWithHiredCount,
});
} catch (error) {
console.error("Error fetching Jobs", error);
return res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

export async function filterJobPost(req: Request, res: Response) {


try {
const { filters } = req.body;
let query: any = {};
if (filters) {
for (const key in filters) {
if (filters.hasOwnProperty(key)) {
const value = filters[key];
if (value !== undefined && value !== null && value !== '') {
if (key === 'keyword') {
query.$or = [
{ jobTitle: { $regex: new RegExp(value, "i") } }
];
} else if (key === 'location') {
query["jobLocation.city"] = { $regex: new
RegExp(value, "i") };
} else if (key === 'minSalary' || key === 'maxSalary') {
query["salary"] = query["salary"] || {};
if (key === 'minSalary') {
query["salary"]["$gte"] = parseInt(value, 10);
} if (key === 'maxSalary') {
query["salary"]["$lte"] = parseInt(value, 10);
}
} else if (key === 'jobType') {
query["jobType"] = value;
} else if (key === 'createdAt') {
query["createdAt"] = value;
} else if (key === 'educationLevel') {
query["educationLevel"] = value;
} else if (key === 'industry') {
query["industry"] = value;
} else if (key === 'industry') {
query["industry"] = value;
} else if (key === 'companyName') {
query["companyName"] = value;
} else if (key === 'jobLanguage') {
query["jobLanguage"] = value;
} else if (key === 'jobSite') {
query["jobSite"] = value;
}
}
}
}
}
const jobs = await Employer.find(query);
res.status(200).json({
statusCode: 200,
jobs: jobs,
});
} catch (error) {
console.error("Error searching jobs:", error);
res.status(500).json({
statusCode: 500,
message: "Internal server error",
});
}
}

You might also like