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,
|
Query,
|
||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { Transform, TransformationType } from 'class-transformer'
|
import { Transform } from 'class-transformer'
|
||||||
import {
|
import {
|
||||||
IsArray,
|
IsArray,
|
||||||
IsBoolean,
|
IsBoolean,
|
||||||
|
IsEnum,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator'
|
} from 'class-validator'
|
||||||
import { Dayjs } from 'dayjs'
|
import { Dayjs } from 'dayjs'
|
||||||
|
|
||||||
import dayjs from '../common/dayjs'
|
import { DayjsTransformer } from '../common/dayjs'
|
||||||
import { LoggerService } from '../logger/service.logger'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
|
import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
|
||||||
|
import { ReservationStatus } from './entity'
|
||||||
import { ReservationsService } from './service'
|
import { ReservationsService } from './service'
|
||||||
|
|
||||||
export class GetReservationsQueryParams {
|
export class GetReservationsQueryParams {
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Transform(() => Dayjs)
|
@Transform(() => Dayjs)
|
||||||
date?: Dayjs
|
readonly date?: Dayjs
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
@Transform(({ value }) => value === 'true')
|
@Transform(({ value }) => value === 'true')
|
||||||
readonly schedulable?: boolean
|
readonly schedulable?: boolean
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsEnum(ReservationStatus)
|
||||||
|
readonly status?: ReservationStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateReservationOpponent {
|
export class CreateReservationOpponent {
|
||||||
@IsString()
|
@IsString()
|
||||||
id: string
|
readonly id: string
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
name: string
|
readonly name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateReservationRequest {
|
export class CreateReservationRequest {
|
||||||
@IsString()
|
@IsString()
|
||||||
ownerId: string
|
readonly ownerId: string
|
||||||
|
|
||||||
@Transform(({ value, type }) => {
|
@Transform(DayjsTransformer)
|
||||||
switch (type) {
|
readonly dateRangeStart: Dayjs
|
||||||
case TransformationType.PLAIN_TO_CLASS:
|
|
||||||
return dayjs(value)
|
|
||||||
case TransformationType.CLASS_TO_PLAIN:
|
|
||||||
return value.format()
|
|
||||||
default:
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
})
|
|
||||||
dateRangeStart: Dayjs
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Transform(({ value, type }) => {
|
@Transform(DayjsTransformer)
|
||||||
switch (type) {
|
readonly dateRangeEnd?: Dayjs
|
||||||
case TransformationType.PLAIN_TO_CLASS:
|
|
||||||
return dayjs(value)
|
|
||||||
case TransformationType.CLASS_TO_PLAIN:
|
|
||||||
return value.format()
|
|
||||||
default:
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
})
|
|
||||||
dateRangeEnd?: Dayjs
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested()
|
@ValidateNested()
|
||||||
opponents?: CreateReservationOpponent[]
|
readonly opponents?: CreateReservationOpponent[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@Controller('reservations')
|
@Controller('reservations')
|
||||||
|
|
@ -97,14 +85,14 @@ export class ReservationsController {
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getReservations(@Query() params: GetReservationsQueryParams) {
|
getReservations(@Query() params: GetReservationsQueryParams) {
|
||||||
const { schedulable, date } = params
|
const { schedulable, date, status } = params
|
||||||
if (schedulable) {
|
if (schedulable) {
|
||||||
return this.reservationsService.getSchedulable()
|
return this.reservationsService.getSchedulable()
|
||||||
}
|
}
|
||||||
if (date) {
|
if (date) {
|
||||||
return this.reservationsService.getByDate(date)
|
return this.reservationsService.getByDate(date, status)
|
||||||
}
|
}
|
||||||
return this.reservationsService.getAll()
|
return this.reservationsService.getAll(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
|
|
|
||||||
|
|
@ -21,19 +21,24 @@ export class ReservationsService {
|
||||||
private readonly loggerService: LoggerService,
|
private readonly loggerService: LoggerService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getAll() {
|
async getAll(status?: ReservationStatus) {
|
||||||
return await this.reservationsRepository.find()
|
return await this.reservationsRepository.find({ where: { status } })
|
||||||
}
|
}
|
||||||
|
|
||||||
async getById(id: string) {
|
async getById(id: string) {
|
||||||
return await this.reservationsRepository.findOneBy({ id })
|
return await this.reservationsRepository.findOneBy({ id })
|
||||||
}
|
}
|
||||||
|
|
||||||
async getByDate(date = dayjs()) {
|
async getByDate(date = dayjs(), status?: ReservationStatus) {
|
||||||
return await this.reservationsRepository
|
let qb = this.reservationsRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.where(`DATE(dateRangeStart) = DATE(:date)`, { date: date.toISOString() })
|
.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