Skip to content

Commit d794778

Browse files
committed
WIP crontab implementation
1 parent bedc66b commit d794778

File tree

6 files changed

+85
-35
lines changed

6 files changed

+85
-35
lines changed

service/src/core/categories/_dto/categories.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class CategoriesCreateDto extends AbstractCustomFieldsDto {
4444
}
4545

4646
export class CategoriesDto extends CategoriesCreateDto {
47-
@IsString()
47+
@IsMongoId()
4848
@ApiProperty()
4949
public _id: string
5050
}
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
1-
import { PartialType } from "@nestjs/swagger"
1+
import { ApiProperty, PartialType } from '@nestjs/swagger'
2+
import { IsBoolean, IsMongoId, IsNotEmpty, IsOptional, IsString, ValidateNested, IsArray } from 'class-validator'
3+
import { Type } from 'class-transformer'
24

35
export class CrontabsCreateDto {
6+
@IsString()
7+
@IsNotEmpty()
8+
@ApiProperty()
9+
public name: string
410

11+
@IsString()
12+
@IsOptional()
13+
@ApiProperty()
14+
public description?: string
15+
16+
@IsString()
17+
@IsNotEmpty()
18+
@ApiProperty()
19+
//TODO: validate cron expression
20+
public interval: string
21+
22+
@IsArray()
23+
@ValidateNested({ each: true })
24+
@Type(() => Object)
25+
@ApiProperty()
26+
public actions: { [key: string]: any }[]
27+
28+
@IsMongoId()
29+
@IsOptional()
30+
@ApiProperty()
31+
public pluginId?: string
32+
33+
@IsBoolean()
34+
@IsOptional()
35+
@ApiProperty()
36+
public disabled?: boolean
537
}
638

7-
export class CrontabsUpdateDto extends PartialType(CrontabsCreateDto) {}
39+
export class CrontabsDto extends CrontabsCreateDto {
40+
@IsMongoId()
41+
@ApiProperty()
42+
public _id: string
43+
}
44+
45+
export class CrontabsUpdateDto extends PartialType(CrontabsCreateDto) {
46+
}

service/src/core/crontabs/_schemas/crontabs.schema.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
2-
import { Document, ObjectId, Types } from 'mongoose'
2+
import { ObjectId, Types } from 'mongoose'
33
import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema'
44

5-
export type CrontabsDocument = Crontabs & Document
6-
75
@Schema({
86
collection: 'crontabs',
97
versionKey: false,
@@ -15,33 +13,28 @@ export class Crontabs extends AbstractSchema {
1513
})
1614
public name: string
1715

18-
@Prop({
19-
required: true,
20-
type: String,
21-
})
16+
@Prop({ type: String })
2217
public description: string
2318

2419
@Prop({
2520
required: true,
26-
type: Object,
21+
type: String,
22+
//TODO: validate cron expression
2723
})
28-
public interval: { [key: string]: any }
24+
public interval: string
2925

3026
@Prop({
3127
required: true,
32-
type: Object,
28+
type: [Object],
3329
})
34-
public actions: { [key: string]: any }
30+
public actions: { [key: string]: any }[]
3531

36-
@Prop({
37-
required: true,
38-
type: Types.ObjectId,
39-
})
40-
public pluginId: ObjectId
32+
@Prop({ type: Types.ObjectId })
33+
public pluginId?: ObjectId
4134

4235
@Prop({
43-
required: true,
4436
type: Boolean,
37+
default: false,
4538
})
4639
public disabled: boolean
4740
}

service/src/core/crontabs/crontabs.controller.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, Res } from '@nestjs/common'
2-
import { CrontabsCreateDto, CrontabsUpdateDto } from './_dto/crontabs.dto'
1+
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Res } from '@nestjs/common'
2+
import { CrontabsCreateDto, CrontabsDto, CrontabsUpdateDto } from './_dto/crontabs.dto'
33
import { CrontabsService } from './crontabs.service'
44
import { AbstractController } from '~/_common/abstracts/abstract.controller'
55
import { ApiParam } from '@nestjs/swagger'
6-
import { SearchFilterSchema, FilterSchema, SearchFilterOptions, FilterOptions, ObjectIdValidationPipe } from '@streamkits/nestjs_module_scrud'
6+
import {
7+
FilterOptions,
8+
FilterSchema,
9+
ObjectIdValidationPipe,
10+
SearchFilterOptions,
11+
SearchFilterSchema,
12+
} from '@streamkits/nestjs_module_scrud'
713
import { Types } from 'mongoose'
8-
import { Request, Response } from 'express'
14+
import { Response } from 'express'
15+
import { PartialProjectionType } from '~/_common/types/partial-projection.type'
16+
import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator'
17+
import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator'
18+
import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper'
19+
import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator'
20+
import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator'
21+
import { ApiDeletedResponseDecorator } from '~/_common/decorators/api-deleted-response.decorator'
922

1023
@Controller('crontabs')
1124
export class CrontabsController extends AbstractController {
12-
protected readonly projection = {
25+
protected static readonly projection: PartialProjectionType<CrontabsDto> = {
1326
name: 1,
14-
description: 1,
27+
interval: 1,
1528
}
1629

17-
constructor(private readonly _service: CrontabsService) {
30+
public constructor(private readonly _service: CrontabsService) {
1831
super()
1932
}
2033

2134
@Post()
22-
public async create(@Req() req: Request, @Res() res: Response, @Body() body: CrontabsCreateDto) {
35+
@ApiCreateDecorator(CrontabsCreateDto, CrontabsDto)
36+
public async create(@Res() res: Response, @Body() body: CrontabsCreateDto): Promise<Response> {
2337
const data = await this._service.create(body)
2438
return res.status(HttpStatus.CREATED).json({
2539
statusCode: HttpStatus.CREATED,
@@ -28,8 +42,9 @@ export class CrontabsController extends AbstractController {
2842
}
2943

3044
@Get()
31-
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions) {
32-
const [data, total] = await this._service.findAndCount(searchFilterSchema, this.projection, searchFilterOptions)
45+
@ApiPaginatedDecorator(PickProjectionHelper(CrontabsDto, CrontabsController.projection))
46+
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions): Promise<Response> {
47+
const [data, total] = await this._service.findAndCount(searchFilterSchema, CrontabsController.projection, searchFilterOptions)
3348
return res.status(HttpStatus.OK).json({
3449
statusCode: HttpStatus.OK,
3550
total,
@@ -39,7 +54,8 @@ export class CrontabsController extends AbstractController {
3954

4055
@Get(':_id([0-9a-fA-F]{24})')
4156
@ApiParam({ name: '_id', type: String })
42-
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
57+
@ApiReadResponseDecorator(CrontabsDto)
58+
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
4359
const data = await this._service.findById(_id)
4460
return res.status(HttpStatus.OK).json({
4561
statusCode: HttpStatus.OK,
@@ -49,7 +65,8 @@ export class CrontabsController extends AbstractController {
4965

5066
@Patch(':_id([0-9a-fA-F]{24})')
5167
@ApiParam({ name: '_id', type: String })
52-
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: CrontabsUpdateDto, @Res() res: Response) {
68+
@ApiUpdateDecorator(CrontabsUpdateDto, CrontabsDto)
69+
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: CrontabsUpdateDto, @Res() res: Response): Promise<Response> {
5370
const data = await this._service.update(_id, body)
5471
return res.status(HttpStatus.OK).json({
5572
statusCode: HttpStatus.OK,
@@ -59,7 +76,8 @@ export class CrontabsController extends AbstractController {
5976

6077
@Delete(':_id([0-9a-fA-F]{24})')
6178
@ApiParam({ name: '_id', type: String })
62-
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
79+
@ApiDeletedResponseDecorator(CrontabsDto)
80+
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
6381
const data = await this._service.delete(_id)
6482
return res.status(HttpStatus.OK).json({
6583
statusCode: HttpStatus.OK,

service/src/core/crontabs/crontabs.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Module } from '@nestjs/common'
22
import { MongooseModule } from '@nestjs/mongoose'
3-
import { CrontabsSchema, Crontabs } from './_schemas/crontabs.schema'
3+
import { Crontabs, CrontabsSchema } from './_schemas/crontabs.schema'
44
import { CrontabsService } from './crontabs.service'
55
import { CrontabsController } from './crontabs.controller'
66

service/src/core/crontabs/crontabs.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AbstractServiceSchema } from '~/_common/abstracts/abstract.service.sche
66

77
@Injectable()
88
export class CrontabsService extends AbstractServiceSchema {
9-
constructor(@InjectModel(Crontabs.name) protected _model: Model<Crontabs>) {
9+
public constructor(@InjectModel(Crontabs.name) protected _model: Model<Crontabs>) {
1010
super()
1111
}
1212
}

0 commit comments

Comments
 (0)