Changing to use global validation pipes and introducing query params validation for reservations controller
This commit is contained in:
parent
dbca10c63e
commit
c2bb21d55a
3 changed files with 19 additions and 7 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ValidationPipe } from '@nestjs/common'
|
||||||
import { ConfigService } from '@nestjs/config'
|
import { ConfigService } from '@nestjs/config'
|
||||||
import { NestFactory } from '@nestjs/core'
|
import { NestFactory } from '@nestjs/core'
|
||||||
|
|
||||||
|
|
@ -9,6 +10,7 @@ async function bootstrap() {
|
||||||
const config = app.get(ConfigService)
|
const config = app.get(ConfigService)
|
||||||
const port = config.get('PORT', 3000)
|
const port = config.get('PORT', 3000)
|
||||||
app.enableShutdownHooks()
|
app.enableShutdownHooks()
|
||||||
|
app.useGlobalPipes(new ValidationPipe())
|
||||||
app.useGlobalInterceptors(new CustomResponseTransformInterceptor())
|
app.useGlobalInterceptors(new CustomResponseTransformInterceptor())
|
||||||
await app.listen(port, () => console.log(`Listening on port ${port}`))
|
await app.listen(port, () => console.log(`Listening on port ${port}`))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import {
|
||||||
Get,
|
Get,
|
||||||
Inject,
|
Inject,
|
||||||
Param,
|
Param,
|
||||||
ParseBoolPipe,
|
|
||||||
Post,
|
Post,
|
||||||
Query,
|
Query,
|
||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
|
|
@ -15,14 +14,26 @@ import {
|
||||||
ValidationPipe,
|
ValidationPipe,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { Queue } from 'bull'
|
import { Queue } from 'bull'
|
||||||
|
import { Transform } from 'class-transformer'
|
||||||
|
import { IsBoolean, IsOptional } from 'class-validator'
|
||||||
import { Dayjs } from 'dayjs'
|
import { Dayjs } from 'dayjs'
|
||||||
import { ParseDayjsPipe } from 'src/common/pipes/parseDayjsPipe'
|
|
||||||
|
|
||||||
import { LoggerService } from '../logger/service.logger'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from './config'
|
import { RESERVATIONS_QUEUE_NAME } from './config'
|
||||||
import { Reservation } from './entity'
|
import { Reservation } from './entity'
|
||||||
import { ReservationsService } from './service'
|
import { ReservationsService } from './service'
|
||||||
|
|
||||||
|
export class GetReservationsQueryParams {
|
||||||
|
@IsOptional()
|
||||||
|
@Transform(() => Dayjs)
|
||||||
|
date?: Dayjs
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
@Transform(({ value }) => value === 'true')
|
||||||
|
readonly schedulable?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
@Controller('reservations')
|
@Controller('reservations')
|
||||||
@UseInterceptors(ClassSerializerInterceptor)
|
@UseInterceptors(ClassSerializerInterceptor)
|
||||||
export class ReservationsController {
|
export class ReservationsController {
|
||||||
|
|
@ -38,11 +49,8 @@ export class ReservationsController {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getReservations(
|
getReservations(@Query() params: GetReservationsQueryParams) {
|
||||||
@Query('date', ParseDayjsPipe) date?: Dayjs,
|
const { schedulable, date } = params
|
||||||
@Query('schedulable', ParseBoolPipe) schedulable?: boolean,
|
|
||||||
) {
|
|
||||||
console.log(schedulable)
|
|
||||||
if (schedulable) {
|
if (schedulable) {
|
||||||
return this.reservationsService.getSchedulable()
|
return this.reservationsService.getSchedulable()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { Module } from '@nestjs/common'
|
||||||
import { EMAILS_QUEUE_NAME } from '../email/config'
|
import { EMAILS_QUEUE_NAME } from '../email/config'
|
||||||
import { EmailModule } from '../email/module'
|
import { EmailModule } from '../email/module'
|
||||||
import { LoggerModule } from '../logger/module'
|
import { LoggerModule } from '../logger/module'
|
||||||
|
import { NtfyModule } from '../ntfy/module'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from '../reservations/config'
|
import { RESERVATIONS_QUEUE_NAME } from '../reservations/config'
|
||||||
import { ReservationsModule } from '../reservations/module'
|
import { ReservationsModule } from '../reservations/module'
|
||||||
import { WaitingListService } from './service'
|
import { WaitingListService } from './service'
|
||||||
|
|
@ -15,6 +16,7 @@ import { WaitingListService } from './service'
|
||||||
BullModule.registerQueue({ name: EMAILS_QUEUE_NAME }),
|
BullModule.registerQueue({ name: EMAILS_QUEUE_NAME }),
|
||||||
BullModule.registerQueue({ name: RESERVATIONS_QUEUE_NAME }),
|
BullModule.registerQueue({ name: RESERVATIONS_QUEUE_NAME }),
|
||||||
EmailModule,
|
EmailModule,
|
||||||
|
NtfyModule,
|
||||||
],
|
],
|
||||||
providers: [WaitingListService],
|
providers: [WaitingListService],
|
||||||
exports: [WaitingListService],
|
exports: [WaitingListService],
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue