Adding parsing pipes and converting dayjs to strings before using in db

This commit is contained in:
Collin Duncan 2023-09-13 21:08:21 +02:00
parent 8dd435a950
commit f8699080a8
No known key found for this signature in database
3 changed files with 28 additions and 5 deletions

View file

@ -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')
}
}
}

View file

@ -7,6 +7,7 @@ import {
Get, Get,
Inject, Inject,
Param, Param,
ParseBoolPipe,
Post, Post,
Query, Query,
UseInterceptors, UseInterceptors,
@ -15,6 +16,7 @@ import {
} from '@nestjs/common' } from '@nestjs/common'
import { Queue } from 'bull' import { Queue } from 'bull'
import { Dayjs } from 'dayjs' import { Dayjs } from 'dayjs'
import { ParseDayjsPipe } from 'src/common/pipes/parseDayjsPipe'
import { LoggerService } from '../logger/service.logger' import { LoggerService } from '../logger/service.logger'
import { RESERVATIONS_QUEUE_NAME } from './config' import { RESERVATIONS_QUEUE_NAME } from './config'
@ -37,9 +39,10 @@ export class ReservationsController {
@Get() @Get()
getReservations( getReservations(
@Query('date') date?: Dayjs, @Query('date', ParseDayjsPipe) date?: Dayjs,
@Query('schedulable') schedulable?: boolean, @Query('schedulable', ParseBoolPipe) schedulable?: boolean,
) { ) {
console.log(schedulable)
if (schedulable) { if (schedulable) {
return this.reservationsService.getSchedulable() return this.reservationsService.getSchedulable()
} }

View file

@ -43,7 +43,7 @@ export class ReservationsService {
const query = this.reservationsRepository const query = this.reservationsRepository
.createQueryBuilder() .createQueryBuilder()
.where(`DATE(dateRangeStart) <= DATE(:date)`, { .where(`DATE(dateRangeStart) <= DATE(:date)`, {
date: dayjs().add(7, 'days'), date: dayjs().add(7, 'days').toISOString(),
}) })
.andWhere(`waitListed = false`) .andWhere(`waitListed = false`)
@ -55,8 +55,12 @@ export class ReservationsService {
async getByDateOnWaitingList(date = dayjs()) { async getByDateOnWaitingList(date = dayjs()) {
return await this.reservationsRepository return await this.reservationsRepository
.createQueryBuilder() .createQueryBuilder()
.where(`DATE(dateRangeStart) <= DATE(:date)`, { date }) .where(`DATE(dateRangeStart) <= DATE(:date)`, {
.andWhere(`DATE(dateRangeEnd) >= DATE(:date)`, { date }) date: date.toISOString(),
})
.andWhere(`DATE(dateRangeEnd) >= DATE(:date)`, {
date: date.toISOString(),
})
.andWhere('waitListed = true') .andWhere('waitListed = true')
.getMany() .getMany()
} }