@@ -12,7 +12,25 @@ export interface MongoJob extends Omit<Job, "id"> {
1212 _id : ObjectId ;
1313}
1414
15- export async function getJobs ( filters : Partial < JobFilters > ) : Promise < Job [ ] > {
15+ /**
16+ * Fetches paginated and filtered job listings from MongoDB.
17+ *
18+ * @param filters - Partial JobFilters object containing:
19+ * - workingRights?: Array of required working rights
20+ * - jobTypes?: Array of job types to include
21+ * - industryFields?: Array of industry fields
22+ * - search?: Full-text search on job titles and company names (case-insensitive)
23+ * - page?: Page number (defaults to 1)
24+ *
25+ * @returns Promise containing:
26+ * - jobs: Array of serialized Job objects
27+ * - total: Total count of jobs matching the filters
28+ *
29+ * @throws Error if MongoDB connection fails or if MONGODB_URI is not configured
30+ */
31+ export async function getJobs (
32+ filters : Partial < JobFilters > ,
33+ ) : Promise < { jobs : Job [ ] ; total : number } > {
1634 if ( ! process . env . MONGODB_URI ) {
1735 throw new Error (
1836 "MongoDB URI is not configured. Please check environment variables." ,
@@ -28,11 +46,25 @@ export async function getJobs(filters: Partial<JobFilters>): Promise<Job[]> {
2846 const query = {
2947 outdated : false ,
3048 ...( filters . workingRights ?. length && {
31- working_rights : { $in : filters . workingRights } ,
49+ working_rights : {
50+ $in : Array . isArray ( filters . workingRights )
51+ ? filters . workingRights
52+ : [ filters . workingRights ] ,
53+ } ,
54+ } ) ,
55+ ...( filters . jobTypes ?. length && {
56+ type : {
57+ $in : Array . isArray ( filters . jobTypes )
58+ ? filters . jobTypes
59+ : [ filters . jobTypes ] ,
60+ } ,
3261 } ) ,
33- ...( filters . jobTypes ?. length && { type : { $in : filters . jobTypes } } ) ,
3462 ...( filters . industryFields ?. length && {
35- industry_field : { $in : filters . industryFields } ,
63+ industry_field : {
64+ $in : Array . isArray ( filters . industryFields )
65+ ? filters . industryFields
66+ : [ filters . industryFields ] ,
67+ } ,
3668 } ) ,
3769 ...( filters . search && {
3870 $or : [
@@ -45,13 +77,15 @@ export async function getJobs(filters: Partial<JobFilters>): Promise<Job[]> {
4577 const page = filters . page || 1 ;
4678 const skip = ( page - 1 ) * PAGE_SIZE ;
4779
48- const jobs = ( await collection
49- . find ( query )
50- . skip ( skip )
51- . limit ( PAGE_SIZE )
52- . toArray ( ) ) as MongoJob [ ] ;
80+ const [ jobs , total ] = await Promise . all ( [
81+ collection . find ( query ) . skip ( skip ) . limit ( PAGE_SIZE ) . toArray ( ) ,
82+ collection . countDocuments ( query ) ,
83+ ] ) ;
5384
54- return jobs . map ( serializeJob ) ;
85+ return {
86+ jobs : ( jobs as MongoJob [ ] ) . map ( serializeJob ) ,
87+ total,
88+ } ;
5589 } catch ( error ) {
5690 console . error ( "Server Error:" , {
5791 error,
0 commit comments