Skip to content

Commit f0754b9

Browse files
committed
2 parents 4bccae6 + 08c7e28 commit f0754b9

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

backend/controllers/jobController.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { index } = require("../vector_database/connectVectorDB.js");
1010
exports.createJobForms = catchAsyncErrors(async (req, res) => {
1111
try {
1212
const userId = req.user._id;
13-
const { content } = req.body;
13+
const content = req.body.content || {};
1414

1515
const formDetails = {
1616
ownerProfile: userId,
@@ -70,8 +70,10 @@ exports.createJobForms = catchAsyncErrors(async (req, res) => {
7070

7171
const newJobForm = new JobApplicationForm(formDetails);
7272
const jobForm = await newJobForm.save();
73-
createJobSkillRelation(jobForm);
74-
await addJobDataToVDB(jobForm._id);
73+
await createJobSkillRelation(jobForm);
74+
const similarJobids = await addJobDataToVDB(jobForm._id);
75+
jobForm.similarJobs = similarJobids;
76+
await jobForm.save();
7577
res.status(200).json(jobForm);
7678
} catch (error) {
7779
console.error(error);
@@ -205,7 +207,12 @@ exports.fetchJobById = catchAsyncErrors(async (req, res) => {
205207
.populate({
206208
path: "requiredSkills",
207209
// select: "_id skill",
208-
});
210+
})
211+
.populate({
212+
path: "similarJobs",
213+
select: "_id jobRole company",
214+
model: "JobApplicationForm"
215+
})
209216
res.status(200).json(formData);
210217
} catch (error) {
211218
res.status(500).json({ error: "Internal server error" });

backend/models/jobForms.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@ const Userauth = require("./userModel.js");
44
const Skill = require("./skillModel.js");
55

66
const JobApplicationFormSchema = new Schema({
7+
78
jobRole: {
89
type: String,
910
required: [true, "Please Enter Job Role"],
1011
maxLength: [50, "Job Role exceed 50 characters"],
1112
minLength: [2, "Job Role have less than 5 characters"],
1213
},
14+
1315
jobLocation: {
1416
type: String,
1517
required: [true, "Please Enter Job Location"],
1618
maxLength: [100, "Job Location exceed 100 characters"],
1719
minLength: [3, "Job Location have more than 4 characters"],
1820
},
21+
1922
jobLocationType: {
2023
type: String,
2124
enum: ["On-site", "Remote"],
2225
required: false,
2326
},
27+
2428
company: {
2529
type: String,
2630
required: [true, "Please Enter Company Name"],
2731
maxLength: [50, "Company Name exceed 50 characters"],
2832
minLength: [2, "Company Name have more than 1 characters"],
2933
},
34+
3035
requiredSkills: [
3136
{
3237
type: mongoose.Schema.Types.ObjectId,
@@ -75,6 +80,14 @@ const JobApplicationFormSchema = new Schema({
7580
},
7681
],
7782

83+
similarJobs: [
84+
{
85+
type: mongoose.Schema.Types.ObjectId,
86+
required: false,
87+
ref: "JobApplicationForm",
88+
},
89+
],
90+
7891
timestamp: { type: Date, default: Date.now },
7992
updated: { type: Date },
8093
});
@@ -83,7 +96,6 @@ JobApplicationFormSchema.pre("save", function (next) {
8396
let isError = false;
8497
let errStr = "";
8598

86-
// Checking totalDuration
8799
if (
88100
this.totalDuration &&
89101
this.totalDuration.value &&
@@ -93,14 +105,12 @@ JobApplicationFormSchema.pre("save", function (next) {
93105
isError = true;
94106
}
95107

96-
// Checking workingHours
97108
if (this.workingHours && this.workingHours.value && !this.workingHours.mode) {
98109
if (isError) errStr += ", ";
99110
errStr += "Working-hours-mode (as working hour is mentioned)";
100111
isError = true;
101112
}
102113

103-
// Checking salary
104114
if (
105115
this.salary &&
106116
this.salary.value &&

backend/vector_database/addJobData.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { index } = require('./connectVectorDB.js');
44
const connectDatabase = require('../database/database.js');
55

66
const makeLower = (text) => {
7-
if (!text) return '';
7+
if (!text) return '';
88
return text
99
.split(' ')
1010
.map((word) => word.toLowerCase())
@@ -19,39 +19,71 @@ const add_batch_data_to_vdb = async () => {
1919
console.log("No jobs found.");
2020
return;
2121
}
22-
23-
let modifiedJobs = jobs.map((job) => ({
24-
id: job._id.toString(),
25-
description:makeLower(job.jobRole) + " " + makeLower(job.jobDescription)
26-
}));
27-
28-
for(let job of modifiedJobs){
29-
await index.upsert({
30-
id: job.id,
31-
data: job.description
32-
});
22+
23+
for (let job of jobs) {
24+
const queryText = makeLower(job.jobRole) + " " + makeLower(job.jobDescription);
25+
const fetchedJobIds = await index.query({
26+
data: queryText,
27+
topK: 5,
28+
includeVectors: false,
29+
includeMetadata: false,
30+
});
31+
32+
if (!fetchedJobIds || !Array.isArray(fetchedJobIds)) {
33+
console.log(fetchedJobIds);
34+
throw new Error(
35+
"Invalid response format: 'result' is undefined or not an array"
36+
);
37+
}
38+
39+
const similarJobids = fetchedJobIds.map((job) => {
40+
if (!job.id) throw new Error("Missing 'id' field in one of the results");
41+
return mongoose.Types.ObjectId(job.id);
42+
});
43+
job.similarJobs = similarJobids;
44+
await job.save();
3345
}
3446
console.log("batch job successfully added to the vector database.");
3547

3648
} catch (error) {
3749
console.error("Error while adding data to vector DB:", error);
3850
}
39-
finally{
51+
finally {
4052
mongoose.connection.close();
4153
}
4254
};
4355

44-
const addJobDataToVDB = async(jobId) =>{
56+
const addJobDataToVDB = async (jobId) => {
4557
try {
4658
const job = await JobApplicationForm.findById(jobId).select('_id jobRole jobDescription');
4759
if (!job || job.length === 0) {
4860
console.log("No jobs found.");
4961
return;
5062
}
5163

64+
const queryText = makeLower(job.jobRole) + " " + makeLower(job.jobDescription);
65+
const fetchedJobIds = await index.query({
66+
data: queryText,
67+
topK: 5,
68+
includeVectors: false,
69+
includeMetadata: false,
70+
});
71+
72+
if (!fetchedJobIds || !Array.isArray(fetchedJobIds)) {
73+
console.log(fetchedJobIds);
74+
throw new Error(
75+
"Invalid response format: 'result' is undefined or not an array"
76+
);
77+
}
78+
79+
const similarJobids = fetchedJobIds.map((job) => {
80+
if (!job.id) throw new Error("Missing 'id' field in one of the results");
81+
return mongoose.Types.ObjectId(job.id);
82+
});
83+
5284
const modifiedJob = {
5385
id: job._id.toString(),
54-
description: makeLower(job.jobRole) + " " + makeLower(job.jobDescription),
86+
description: queryText,
5587
};
5688

5789
await index.upsert({
@@ -60,6 +92,7 @@ const addJobDataToVDB = async(jobId) =>{
6092
});
6193

6294
console.log("Job successfully added to the vector database.");
95+
return similarJobids;
6396

6497
} catch (error) {
6598
console.error("Error while adding data to vector DB:", error);

0 commit comments

Comments
 (0)