|
1 | | -import { Controller } from '@nestjs/common'; |
2 | | -import { JobsService } from './jobs.service'; |
| 1 | +import { Controller, Get, HttpStatus, Param, Res } from '@nestjs/common'; |
| 2 | +import { ApiParam, ApiTags } from '@nestjs/swagger'; |
| 3 | +import { FilterOptions, FilterSchema, SearchFilterOptions, SearchFilterSchema } from '@streamkits/nestjs_module_scrud'; |
| 4 | +import { Response } from 'express'; |
| 5 | +import { Types } from 'mongoose'; |
3 | 6 | import { AbstractController } from '~/_common/abstracts/abstract.controller'; |
| 7 | +import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator'; |
| 8 | +import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator'; |
| 9 | +import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper'; |
| 10 | +import { ObjectIdValidationPipe } from '~/_common/pipes/object-id-validation.pipe'; |
| 11 | +import { PartialProjectionType } from '~/_common/types/partial-projection.type'; |
| 12 | +import { JobsDto } from './_dto/jobs.dto'; |
| 13 | +import { JobsService } from './jobs.service'; |
4 | 14 |
|
5 | | -@Controller('tasks') |
6 | | -export class TasksController extends AbstractController { |
7 | | - constructor(private readonly _service: JobsService) { |
| 15 | +@ApiTags('core') |
| 16 | +@Controller('jobs') |
| 17 | +export class JobsController extends AbstractController { |
| 18 | + protected static readonly projection: PartialProjectionType<JobsDto> = { |
| 19 | + jobId: 1, |
| 20 | + action: 1, |
| 21 | + concernedTo: 1, |
| 22 | + params: 1, |
| 23 | + result: 1, |
| 24 | + }; |
| 25 | + |
| 26 | + public constructor(private readonly _service: JobsService) { |
8 | 27 | super(); |
9 | 28 | } |
| 29 | + |
| 30 | + @Get() |
| 31 | + @ApiPaginatedDecorator(PickProjectionHelper(JobsDto, JobsController.projection)) |
| 32 | + public async search( |
| 33 | + @Res() res: Response, |
| 34 | + @SearchFilterSchema({ unsafe: true }) searchFilterSchema: FilterSchema, |
| 35 | + @SearchFilterOptions() searchFilterOptions: FilterOptions, |
| 36 | + ): Promise<Response> { |
| 37 | + //TODO: search tree by parentId |
| 38 | + const [data, total] = await this._service.findAndCount( |
| 39 | + searchFilterSchema, |
| 40 | + JobsController.projection, |
| 41 | + searchFilterOptions, |
| 42 | + ); |
| 43 | + return res.status(HttpStatus.OK).json({ |
| 44 | + statusCode: HttpStatus.OK, |
| 45 | + total, |
| 46 | + data, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Get(':_id([0-9a-fA-F]{24})') |
| 51 | + @ApiParam({ name: '_id', type: String }) |
| 52 | + @ApiReadResponseDecorator(JobsDto) |
| 53 | + public async read( |
| 54 | + @Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, |
| 55 | + @Res() res: Response, |
| 56 | + ): Promise<Response> { |
| 57 | + const data = await this._service.findById(_id, { |
| 58 | + password: 0, |
| 59 | + }); |
| 60 | + return res.status(HttpStatus.OK).json({ |
| 61 | + statusCode: HttpStatus.OK, |
| 62 | + data, |
| 63 | + }); |
| 64 | + } |
10 | 65 | } |
0 commit comments