From f8699080a8cdabce33b7b4ab6471a1c841443755 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Wed, 13 Sep 2023 21:08:21 +0200 Subject: [PATCH] Adding parsing pipes and converting dayjs to strings before using in db --- src/common/pipes/parseDayjsPipe.ts | 16 ++++++++++++++++ src/reservations/controller.ts | 7 +++++-- src/reservations/service.ts | 10 +++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/common/pipes/parseDayjsPipe.ts diff --git a/src/common/pipes/parseDayjsPipe.ts b/src/common/pipes/parseDayjsPipe.ts new file mode 100644 index 0000000..d0d2659 --- /dev/null +++ b/src/common/pipes/parseDayjsPipe.ts @@ -0,0 +1,16 @@ +import { BadRequestException, PipeTransform } from '@nestjs/common' + +import dayjsTz from '../dayjs' + +export class ParseDayjsPipe implements PipeTransform { + transform(value: any) { + switch (typeof value) { + case 'undefined': + return undefined + case 'string': + return dayjsTz(value) + default: + throw new BadRequestException('Non-parsable date') + } + } +} diff --git a/src/reservations/controller.ts b/src/reservations/controller.ts index 13569d1..205728a 100644 --- a/src/reservations/controller.ts +++ b/src/reservations/controller.ts @@ -7,6 +7,7 @@ import { Get, Inject, Param, + ParseBoolPipe, Post, Query, UseInterceptors, @@ -15,6 +16,7 @@ import { } from '@nestjs/common' import { Queue } from 'bull' import { Dayjs } from 'dayjs' +import { ParseDayjsPipe } from 'src/common/pipes/parseDayjsPipe' import { LoggerService } from '../logger/service.logger' import { RESERVATIONS_QUEUE_NAME } from './config' @@ -37,9 +39,10 @@ export class ReservationsController { @Get() getReservations( - @Query('date') date?: Dayjs, - @Query('schedulable') schedulable?: boolean, + @Query('date', ParseDayjsPipe) date?: Dayjs, + @Query('schedulable', ParseBoolPipe) schedulable?: boolean, ) { + console.log(schedulable) if (schedulable) { return this.reservationsService.getSchedulable() } diff --git a/src/reservations/service.ts b/src/reservations/service.ts index 809cc13..054063c 100644 --- a/src/reservations/service.ts +++ b/src/reservations/service.ts @@ -43,7 +43,7 @@ export class ReservationsService { const query = this.reservationsRepository .createQueryBuilder() .where(`DATE(dateRangeStart) <= DATE(:date)`, { - date: dayjs().add(7, 'days'), + date: dayjs().add(7, 'days').toISOString(), }) .andWhere(`waitListed = false`) @@ -55,8 +55,12 @@ export class ReservationsService { async getByDateOnWaitingList(date = dayjs()) { return await this.reservationsRepository .createQueryBuilder() - .where(`DATE(dateRangeStart) <= DATE(:date)`, { date }) - .andWhere(`DATE(dateRangeEnd) >= DATE(:date)`, { date }) + .where(`DATE(dateRangeStart) <= DATE(:date)`, { + date: date.toISOString(), + }) + .andWhere(`DATE(dateRangeEnd) >= DATE(:date)`, { + date: date.toISOString(), + }) .andWhere('waitListed = true') .getMany() }