Skip to content

Commit be0980b

Browse files
committed
fix: category filter parsing using JSON in jobs APIs
1 parent 3b9cab0 commit be0980b

1 file changed

Lines changed: 80 additions & 2 deletions

File tree

src/controllers/jobsController.js

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,47 @@ const paginationForJobs = async (req, res) => {
5050
const pageNumber = Math.max(1, parseInt(page, 10));
5151
const limitNumber = Math.max(1, parseInt(limit, 10));
5252

53-
const query = buildJobQuery({ search, category, position });
53+
const conditions = [];
54+
55+
/* ----------------------------
56+
SEARCH FILTER
57+
---------------------------- */
58+
if (search && search.trim() !== '') {
59+
const searchString = search.trim();
60+
conditions.push({
61+
$or: [
62+
{ title: { $regex: new RegExp(searchString, 'i') } },
63+
{ description: { $regex: new RegExp(searchString, 'i') } },
64+
],
65+
});
66+
}
67+
68+
/* ----------------------------
69+
POSITION FILTER (only if non-empty)
70+
---------------------------- */
71+
if (position && position.trim() !== '') {
72+
conditions.push({ title: { $in: [position.trim()] } });
73+
}
74+
75+
/* ----------------------------
76+
MULTI-CATEGORY FILTER (only if non-empty)
77+
---------------------------- */
78+
79+
if (category) {
80+
let categoryList = [];
81+
82+
try {
83+
categoryList = JSON.parse(category);
84+
} catch (e) {
85+
console.error('Invalid category format:', category);
86+
}
87+
88+
if (Array.isArray(categoryList) && categoryList.length > 0) {
89+
conditions.push({ category: { $in: categoryList } });
90+
}
91+
}
92+
93+
const query = conditions.length ? { $and: conditions } : {};
5494

5595
const totalJobs = await Job.countDocuments(query);
5696
const totalPages = Math.max(1, Math.ceil(totalJobs / limitNumber));
@@ -96,8 +136,46 @@ const getJobSummaries = async (req, res) => {
96136
const { search = '', category = '', position = '' } = req.query;
97137

98138
try {
99-
const query = buildJobQuery({ search, category, position });
139+
const conditions = [];
140+
141+
if (search && search.trim() !== '') {
142+
const searchString = search.trim();
143+
conditions.push({
144+
$or: [
145+
{ title: { $regex: new RegExp(searchString, 'i') } },
146+
{ description: { $regex: new RegExp(searchString, 'i') } },
147+
],
148+
});
149+
}
150+
151+
if (position && position.trim() !== '') {
152+
conditions.push({ title: { $in: [position.trim()] } });
153+
}
154+
155+
if (category) {
156+
let categoryList = [];
157+
158+
try {
159+
categoryList = JSON.parse(category);
160+
} catch (e) {
161+
console.error('Invalid category format:', category);
162+
}
163+
164+
if (Array.isArray(categoryList) && categoryList.length > 0) {
165+
conditions.push({ category: { $in: categoryList } });
166+
}
167+
}
168+
169+
const query = conditions.length ? { $and: conditions } : {};
100170

171+
const sortCriteria = {
172+
displayOrder: 1,
173+
featured: -1,
174+
datePosted: -1,
175+
title: 1,
176+
};
177+
178+
const totalJobs = await Job.countDocuments(query);
101179
const jobs = await Job.find(query)
102180
.select('title category location description datePosted featured jobDetailsLink')
103181
.sort({ displayOrder: 1, featured: -1, datePosted: -1, title: 1 })

0 commit comments

Comments
 (0)