From 1da322ed059180ad8366fb094fb90a3670818f4e Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:00:32 +0200 Subject: [PATCH] Changing how cron fetches available reservations to schedule --- src/reservations/cron.ts | 6 ++---- src/reservations/service.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/reservations/cron.ts b/src/reservations/cron.ts index 2e9dd96..1cc1a59 100644 --- a/src/reservations/cron.ts +++ b/src/reservations/cron.ts @@ -3,7 +3,6 @@ import { Inject, Injectable } from '@nestjs/common' import { Cron, CronExpression } from '@nestjs/schedule' import { Queue } from 'bull' -import dayjs from '../common/dayjs' import { LoggerService } from '../logger/service.logger' import { RESERVATIONS_QUEUE_NAME } from './config' import { ReservationsService } from './service' @@ -27,9 +26,8 @@ export class ReservationsCronService { }) async handleDailyReservations() { this.loggerService.log('handleDailyReservations beginning') - const reservationsToPerform = await this.reservationService.getByDate( - dayjs().subtract(7, 'days'), - ) + const reservationsToPerform = + await this.reservationService.getScheduleable() this.loggerService.log( `Found ${reservationsToPerform.length} reservations to perform`, ) diff --git a/src/reservations/service.ts b/src/reservations/service.ts index bbbf6aa..03d2d4a 100644 --- a/src/reservations/service.ts +++ b/src/reservations/service.ts @@ -31,6 +31,20 @@ export class ReservationsService { .getMany() } + /** + * Gets all reservations that have not been scheduled that are within the reservation window + * @returns Reservations that can be scheduled + */ + async getScheduleable() { + return await this.reservationsRepository + .createQueryBuilder() + .where(`DATE(dateRangeStart) <= DATE(:date)`, { + date: dayjs().add(7, 'days'), + }) + .andWhere(`waitListed = false`) + .getMany() + } + async getByDateOnWaitingList(date = dayjs()) { return await this.reservationsRepository .createQueryBuilder()