Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions frontend/src/app/jobs/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { MongoClient, ObjectId } from "mongodb";
import { JobFilters } from "@/types/filters";
import { Job } from "@/types/job";

import serializeJob from "@/lib/utils";

const PAGE_SIZE = 20;
Expand Down Expand Up @@ -42,30 +43,34 @@ export async function getJobs(
try {
await client.connect();
const collection = client.db("default").collection("active_jobs");

const array_jobs = JSON.parse(JSON.stringify(filters, null, 2));
// Build the query object with proper typing
const query = {
outdated: false,
...(filters.workingRights?.length && {
working_rights: {
$in: Array.isArray(filters.workingRights)
? filters.workingRights
: [filters.workingRights],
},
}),
...(filters.jobTypes?.length && {
type: {
$in: Array.isArray(filters.jobTypes)
? filters.jobTypes
: [filters.jobTypes],
},
}),
...(filters.industryFields?.length && {
industry_field: {
$in: Array.isArray(filters.industryFields)
? filters.industryFields
: [filters.industryFields],
},
}),
...(array_jobs["workingRights[]"] !== undefined &&
array_jobs["workingRights[]"].length && {
working_rights: {
$in: Array.isArray(array_jobs["workingRights[]"])
? array_jobs["workingRights[]"]
: [array_jobs["workingRights[]"]],
},
}),
...(array_jobs["locations[]"] !== undefined &&
array_jobs["locations[]"].length && {
locations: {
$in: Array.isArray(array_jobs["locations[]"])
? array_jobs["locations[]"]
: [array_jobs["locations[]"]],
},
}),
...(array_jobs["industryFields[]"] !== undefined &&
array_jobs["industryFields[]"].length && {
industry_field: {
$in: Array.isArray(array_jobs["industryFields[]"])
? array_jobs["industryFields[]"]
: [array_jobs["industryFields[]"]],
},
}),
...(filters.search && {
$or: [
{ title: { $regex: filters.search, $options: "i" } },
Expand All @@ -81,7 +86,6 @@ export async function getJobs(
collection.find(query).skip(skip).limit(PAGE_SIZE).toArray(),
collection.countDocuments(query),
]);

return {
jobs: (jobs as MongoJob[]).map(serializeJob),
total,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/jobs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default async function JobsPage({
// https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional
// searchParams is a promise that resolves to an object containing the search
// parameters of the current URL.

const { jobs, total } = await getJobs(await searchParams);

return (
Expand Down