diff --git a/src/reservations/config.ts b/src/reservations/config.ts index 44395e4..7e3bda6 100644 --- a/src/reservations/config.ts +++ b/src/reservations/config.ts @@ -1,7 +1,13 @@ -import type { Queue } from 'bull' +import type { Job, Queue } from 'bull' import type { Reservation } from './entity' export const RESERVATIONS_QUEUE_NAME = 'reservations' -export type ReservationsQueue = Queue +interface ReservationsQueueJobPayload { + reservation: Reservation + speedyMode: boolean +} + +export type ReservationsQueue = Queue +export type ReservationsJob = Job diff --git a/src/reservations/controller.ts b/src/reservations/controller.ts index a04ab32..069d1e5 100644 --- a/src/reservations/controller.ts +++ b/src/reservations/controller.ts @@ -120,7 +120,7 @@ export class ReservationsController { return 'Reservation saved' } this.loggerService.debug('Reservation is available for reservation') - await this.reservationsQueue.add(reservation) + await this.reservationsQueue.add({ reservation, speedyMode: true }) return 'Reservation queued' } @@ -132,7 +132,7 @@ export class ReservationsController { if (!reservation.isAvailableForReservation()) throw new HttpException('Not available', 400) - await this.reservationsQueue.add(reservation) + await this.reservationsQueue.add({ reservation, speedyMode: true }) return 'Reservation queued' } diff --git a/src/reservations/cron.ts b/src/reservations/cron.ts index 6c0286c..dbbb57e 100644 --- a/src/reservations/cron.ts +++ b/src/reservations/cron.ts @@ -73,7 +73,7 @@ export class ReservationsCronService { this.loggerService.debug(`It's go-time`) await this.reservationsQueue.addBulk( reservationsToPerform.map((res) => ({ - data: res, + data: { reservation: res, speedyMode: true }, opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, })), ) diff --git a/src/reservations/worker.ts b/src/reservations/worker.ts index ff76f77..0c336e5 100644 --- a/src/reservations/worker.ts +++ b/src/reservations/worker.ts @@ -9,7 +9,7 @@ import { BaanReserverenService, NoCourtAvailableError, } from '../runner/baanreserveren/service' -import { RESERVATIONS_QUEUE_NAME } from './config' +import { RESERVATIONS_QUEUE_NAME, ReservationsJob } from './config' import { DAILY_RESERVATIONS_ATTEMPTS } from './cron' import { Reservation } from './entity' import { ReservationsService } from './service' @@ -31,8 +31,8 @@ export class ReservationsWorker { ) {} @Process() - async handleReservationJob(job: Job) { - const reservation = plainToInstance(Reservation, job.data, { + async handleReservationJob(job: ReservationsJob) { + const reservation = plainToInstance(Reservation, job.data.reservation, { groups: ['password'], }) this.loggerService.log('Handling reservation', { @@ -43,7 +43,12 @@ export class ReservationsWorker { reservation.dateRangeStart, reservation.dateRangeEnd, ) - await this.performReservation(reservation, job.attemptsMade, true, true) + await this.performReservation( + reservation, + job.attemptsMade, + true, + job.data.speedyMode, + ) } private async handleReservationErrors( diff --git a/src/waitingList/service.ts b/src/waitingList/service.ts index 96cf6b6..fad565a 100644 --- a/src/waitingList/service.ts +++ b/src/waitingList/service.ts @@ -95,7 +95,10 @@ export class WaitingListService { ) await this.reservationsQueue.addBulk( - reservations.map((r) => ({ data: r, opts: { attempts: 1 } })), + reservations.map((r) => ({ + data: { reservation: r, speedyMode: false }, + opts: { attempts: 1 }, + })), ) } diff --git a/test/unit/reservations/cron.spec.ts b/test/unit/reservations/cron.spec.ts index 24410ca..5fa97fa 100644 --- a/test/unit/reservations/cron.spec.ts +++ b/test/unit/reservations/cron.spec.ts @@ -119,7 +119,7 @@ describe('reservations.cron', () => { it('should perform reservations', () => { expect(reservationsQueueSpy).toHaveBeenCalledWith([ { - data: stubbedReservation, + data: { reservation: stubbedReservation, speedyMode: true }, opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, }, ])