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,
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()
}

View file

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