diff --git a/.husky/commit-msg b/.husky/commit-msg index 5a8500090..42644c815 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/bin/sh -. "$(dirname "$0")/_/husky.sh" +# . "$(dirname "$0")/_/husky.sh" npx --no -- commitlint --edit $1 diff --git a/.husky/pre-commit b/.husky/pre-commit index 36af21989..a67e26242 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/bin/sh -. "$(dirname "$0")/_/husky.sh" +# . "$(dirname "$0")/_/husky.sh" npx lint-staged diff --git a/src/modules/jobs/jobs.controller.ts b/src/modules/jobs/jobs.controller.ts index eabfeb9c0..193ffca4e 100644 --- a/src/modules/jobs/jobs.controller.ts +++ b/src/modules/jobs/jobs.controller.ts @@ -10,10 +10,11 @@ import { UseGuards, ValidationPipe, ParseUUIDPipe, + ParseIntPipe, Patch, UseInterceptors, UploadedFile, - BadRequestException, + BadRequestException, } from '@nestjs/common'; import { ApiConsumes, @@ -108,11 +109,23 @@ export class JobsController { @skipAuth() @Get('/') - @ApiOperation({ summary: 'Gets all jobs' }) + @ApiOperation({ summary: 'Gets all jobs with pagination' }) + @ApiQuery({ name: 'page', required: false, type: Number, example: 1, description: 'Page number (default: 1)' }) + @ApiQuery({ + name: 'limit', + required: false, + type: Number, + example: 10, + description: 'Number of jobs per page (default: 10, max: 100)', + }) @ApiResponse({ status: 200, description: 'Jobs returned successfully' }) - @ApiResponse({ status: 404, description: 'Job not found' }) - async getAllJobs() { - return this.jobService.getJobs(); + @ApiResponse({ status: 400, description: 'Invalid pagination parameters' }) + @ApiResponse({ status: 404, description: 'No jobs found' }) + async getAllJobs( + @Query('page', new ParseIntPipe({ errorHttpStatusCode: 400 })) page: number = 1, + @Query('limit', new ParseIntPipe({ errorHttpStatusCode: 400 })) limit: number = 10 + ) { + return this.jobService.getJobs(page, limit); } @skipAuth() diff --git a/src/modules/jobs/jobs.service.ts b/src/modules/jobs/jobs.service.ts index 0501ee9e1..483b23a42 100644 --- a/src/modules/jobs/jobs.service.ts +++ b/src/modules/jobs/jobs.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Injectable } from '@nestjs/common'; +import { HttpStatus, Injectable, BadRequestException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import * as SYS_MSG from '@shared/constants/SystemMessages'; @@ -94,14 +94,34 @@ export class JobsService { }; } - async getJobs() { - const jobs = await this.jobRepository.find({ where: { is_deleted: false } }); + async getJobs(page: number = 1, limit: number = 10) { + if (!Number.isInteger(page) || page < 1) { + throw new BadRequestException('Page must be a positive integer.'); + } + if (!Number.isInteger(limit) || limit < 1 || limit > 100) { + throw new BadRequestException('Limit must be a positive integer between 1 and 100.'); + } + + limit = limit > 100 ? 100 : limit; + + const [jobs, total] = await this.jobRepository.findAndCount({ + where: { is_deleted: false }, + take: limit, + skip: (page - 1) * limit, + }); + + jobs.forEach(job => delete job.is_deleted); - jobs.map(x => delete x.is_deleted); return { message: SYS_MSG.JOB_LISTING_RETRIEVAL_SUCCESSFUL, status_code: 200, data: jobs, + meta: { + total_jobs: total, + total_pages: Math.ceil(total / limit), + current_page: page, + per_page: limit, + }, }; }