Adding status to query params of reservations controller endpoints
This commit is contained in:
parent
671084dc7b
commit
25efd61c99
2 changed files with 30 additions and 37 deletions
|
|
@ -12,73 +12,61 @@ import {
|
|||
Query,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common'
|
||||
import { Transform, TransformationType } from 'class-transformer'
|
||||
import { Transform } from 'class-transformer'
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
import { Dayjs } from 'dayjs'
|
||||
|
||||
import dayjs from '../common/dayjs'
|
||||
import { DayjsTransformer } from '../common/dayjs'
|
||||
import { LoggerService } from '../logger/service.logger'
|
||||
import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
|
||||
import { ReservationStatus } from './entity'
|
||||
import { ReservationsService } from './service'
|
||||
|
||||
export class GetReservationsQueryParams {
|
||||
@IsOptional()
|
||||
@Transform(() => Dayjs)
|
||||
date?: Dayjs
|
||||
readonly date?: Dayjs
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Transform(({ value }) => value === 'true')
|
||||
readonly schedulable?: boolean
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(ReservationStatus)
|
||||
readonly status?: ReservationStatus
|
||||
}
|
||||
|
||||
export class CreateReservationOpponent {
|
||||
@IsString()
|
||||
id: string
|
||||
readonly id: string
|
||||
|
||||
@IsString()
|
||||
name: string
|
||||
readonly name: string
|
||||
}
|
||||
|
||||
export class CreateReservationRequest {
|
||||
@IsString()
|
||||
ownerId: string
|
||||
readonly ownerId: string
|
||||
|
||||
@Transform(({ value, type }) => {
|
||||
switch (type) {
|
||||
case TransformationType.PLAIN_TO_CLASS:
|
||||
return dayjs(value)
|
||||
case TransformationType.CLASS_TO_PLAIN:
|
||||
return value.format()
|
||||
default:
|
||||
return value
|
||||
}
|
||||
})
|
||||
dateRangeStart: Dayjs
|
||||
@Transform(DayjsTransformer)
|
||||
readonly dateRangeStart: Dayjs
|
||||
|
||||
@IsOptional()
|
||||
@Transform(({ value, type }) => {
|
||||
switch (type) {
|
||||
case TransformationType.PLAIN_TO_CLASS:
|
||||
return dayjs(value)
|
||||
case TransformationType.CLASS_TO_PLAIN:
|
||||
return value.format()
|
||||
default:
|
||||
return value
|
||||
}
|
||||
})
|
||||
dateRangeEnd?: Dayjs
|
||||
@Transform(DayjsTransformer)
|
||||
readonly dateRangeEnd?: Dayjs
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested()
|
||||
opponents?: CreateReservationOpponent[]
|
||||
readonly opponents?: CreateReservationOpponent[]
|
||||
}
|
||||
|
||||
@Controller('reservations')
|
||||
|
|
@ -97,14 +85,14 @@ export class ReservationsController {
|
|||
|
||||
@Get()
|
||||
getReservations(@Query() params: GetReservationsQueryParams) {
|
||||
const { schedulable, date } = params
|
||||
const { schedulable, date, status } = params
|
||||
if (schedulable) {
|
||||
return this.reservationsService.getSchedulable()
|
||||
}
|
||||
if (date) {
|
||||
return this.reservationsService.getByDate(date)
|
||||
return this.reservationsService.getByDate(date, status)
|
||||
}
|
||||
return this.reservationsService.getAll()
|
||||
return this.reservationsService.getAll(status)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
|
|
|||
|
|
@ -21,19 +21,24 @@ export class ReservationsService {
|
|||
private readonly loggerService: LoggerService,
|
||||
) {}
|
||||
|
||||
async getAll() {
|
||||
return await this.reservationsRepository.find()
|
||||
async getAll(status?: ReservationStatus) {
|
||||
return await this.reservationsRepository.find({ where: { status } })
|
||||
}
|
||||
|
||||
async getById(id: string) {
|
||||
return await this.reservationsRepository.findOneBy({ id })
|
||||
}
|
||||
|
||||
async getByDate(date = dayjs()) {
|
||||
return await this.reservationsRepository
|
||||
async getByDate(date = dayjs(), status?: ReservationStatus) {
|
||||
let qb = this.reservationsRepository
|
||||
.createQueryBuilder()
|
||||
.where(`DATE(dateRangeStart) = DATE(:date)`, { date: date.toISOString() })
|
||||
.getMany()
|
||||
|
||||
if (status != null) {
|
||||
qb = qb.andWhere(`status = :status`, { status })
|
||||
}
|
||||
|
||||
return await qb.orderBy('dateRangeStart', 'ASC').getMany()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue