From 25efd61c99835b2465ab23ac3c4f2c174606e641 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:46:15 +0100 Subject: [PATCH] Adding status to query params of reservations controller endpoints --- src/reservations/controller.ts | 52 +++++++++++++--------------------- src/reservations/service.ts | 15 ++++++---- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/reservations/controller.ts b/src/reservations/controller.ts index 069d1e5..40a2a06 100644 --- a/src/reservations/controller.ts +++ b/src/reservations/controller.ts @@ -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') diff --git a/src/reservations/service.ts b/src/reservations/service.ts index 391dfd6..4e7fc86 100644 --- a/src/reservations/service.ts +++ b/src/reservations/service.ts @@ -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() } /**