2023-05-26 15:43:14 -05:00
|
|
|
import { InjectQueue } from '@nestjs/bull'
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common'
|
|
|
|
|
import { Cron, CronExpression } from '@nestjs/schedule'
|
|
|
|
|
import { Queue } from 'bull'
|
2023-06-29 10:32:09 +02:00
|
|
|
|
2023-07-29 16:30:06 +02:00
|
|
|
import dayjs from '../common/dayjs'
|
2023-06-29 10:32:09 +02:00
|
|
|
import { LoggerService } from '../logger/service'
|
2023-05-26 15:43:14 -05:00
|
|
|
import { RESERVATIONS_QUEUE_NAME } from './config'
|
|
|
|
|
import { ReservationsService } from './service'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ReservationsCronService {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(ReservationsService)
|
|
|
|
|
private readonly reservationService: ReservationsService,
|
2023-06-27 16:06:19 +02:00
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@InjectQueue(RESERVATIONS_QUEUE_NAME)
|
|
|
|
|
private readonly reservationsQueue: Queue,
|
|
|
|
|
|
|
|
|
|
@Inject(LoggerService)
|
2023-07-29 14:58:48 +02:00
|
|
|
private readonly loggerService: LoggerService,
|
2023-05-26 15:43:14 -05:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_7AM, {
|
|
|
|
|
name: 'handleDailyReservations',
|
|
|
|
|
timeZone: 'Europe/Amsterdam',
|
|
|
|
|
})
|
|
|
|
|
async handleDailyReservations() {
|
2023-07-29 16:30:06 +02:00
|
|
|
const reservationsToPerform = await this.reservationService.getByDate(
|
|
|
|
|
dayjs().subtract(7, 'days'),
|
|
|
|
|
)
|
2023-07-29 14:58:48 +02:00
|
|
|
this.loggerService.log(
|
2023-06-27 16:06:19 +02:00
|
|
|
`Found ${reservationsToPerform.length} reservations to perform`,
|
|
|
|
|
)
|
|
|
|
|
await this.reservationsQueue.addBulk(
|
|
|
|
|
reservationsToPerform.map((r) => ({ data: r })),
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-07-29 15:23:13 +02:00
|
|
|
|
|
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_11PM, {
|
|
|
|
|
name: 'cleanUpExpiredReservations',
|
|
|
|
|
timeZone: 'Europe/Amsterdam',
|
|
|
|
|
})
|
|
|
|
|
async cleanUpExpiredReservations() {
|
|
|
|
|
const reservations = await this.reservationService.getByDate()
|
|
|
|
|
this.loggerService.log(
|
|
|
|
|
`Found ${reservations.length} reservations to delete`,
|
|
|
|
|
)
|
|
|
|
|
for (const reservation of reservations) {
|
|
|
|
|
await this.reservationService.deleteById(reservation.id)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
}
|