Changing to use global validation pipes and introducing query params validation for reservations controller

This commit is contained in:
Collin Duncan 2023-09-20 09:34:40 +02:00
parent dbca10c63e
commit c2bb21d55a
No known key found for this signature in database
3 changed files with 19 additions and 7 deletions

View file

@ -1,3 +1,4 @@
import { ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
@ -9,6 +10,7 @@ async function bootstrap() {
const config = app.get(ConfigService)
const port = config.get('PORT', 3000)
app.enableShutdownHooks()
app.useGlobalPipes(new ValidationPipe())
app.useGlobalInterceptors(new CustomResponseTransformInterceptor())
await app.listen(port, () => console.log(`Listening on port ${port}`))
}

View file

@ -7,7 +7,6 @@ import {
Get,
Inject,
Param,
ParseBoolPipe,
Post,
Query,
UseInterceptors,
@ -15,14 +14,26 @@ import {
ValidationPipe,
} from '@nestjs/common'
import { Queue } from 'bull'
import { Transform } from 'class-transformer'
import { IsBoolean, IsOptional } from 'class-validator'
import { Dayjs } from 'dayjs'
import { ParseDayjsPipe } from 'src/common/pipes/parseDayjsPipe'
import { LoggerService } from '../logger/service.logger'
import { RESERVATIONS_QUEUE_NAME } from './config'
import { Reservation } from './entity'
import { ReservationsService } from './service'
export class GetReservationsQueryParams {
@IsOptional()
@Transform(() => Dayjs)
date?: Dayjs
@IsOptional()
@IsBoolean()
@Transform(({ value }) => value === 'true')
readonly schedulable?: boolean
}
@Controller('reservations')
@UseInterceptors(ClassSerializerInterceptor)
export class ReservationsController {
@ -38,11 +49,8 @@ export class ReservationsController {
) {}
@Get()
getReservations(
@Query('date', ParseDayjsPipe) date?: Dayjs,
@Query('schedulable', ParseBoolPipe) schedulable?: boolean,
) {
console.log(schedulable)
getReservations(@Query() params: GetReservationsQueryParams) {
const { schedulable, date } = params
if (schedulable) {
return this.reservationsService.getSchedulable()
}

View file

@ -4,6 +4,7 @@ import { Module } from '@nestjs/common'
import { EMAILS_QUEUE_NAME } from '../email/config'
import { EmailModule } from '../email/module'
import { LoggerModule } from '../logger/module'
import { NtfyModule } from '../ntfy/module'
import { RESERVATIONS_QUEUE_NAME } from '../reservations/config'
import { ReservationsModule } from '../reservations/module'
import { WaitingListService } from './service'
@ -15,6 +16,7 @@ import { WaitingListService } from './service'
BullModule.registerQueue({ name: EMAILS_QUEUE_NAME }),
BullModule.registerQueue({ name: RESERVATIONS_QUEUE_NAME }),
EmailModule,
NtfyModule,
],
providers: [WaitingListService],
exports: [WaitingListService],