Deleting/waitlisting reservations on result
This commit is contained in:
parent
d1faef35d2
commit
89948e79f4
2 changed files with 26 additions and 13 deletions
|
|
@ -12,34 +12,38 @@ export class ReservationsService {
|
|||
private reservationsRepository: Repository<Reservation>,
|
||||
) {}
|
||||
|
||||
getAll() {
|
||||
return this.reservationsRepository.find()
|
||||
async getAll() {
|
||||
return await this.reservationsRepository.find()
|
||||
}
|
||||
|
||||
getById(id: string) {
|
||||
return this.reservationsRepository.findOneBy({ id })
|
||||
async getById(id: string) {
|
||||
return await this.reservationsRepository.findOneBy({ id })
|
||||
}
|
||||
|
||||
getByDate(date = dayjs()) {
|
||||
return this.reservationsRepository
|
||||
async getByDate(date = dayjs()) {
|
||||
return await this.reservationsRepository
|
||||
.createQueryBuilder()
|
||||
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
||||
.getMany()
|
||||
}
|
||||
|
||||
getByDateOnWaitingList(date = dayjs()) {
|
||||
return this.reservationsRepository
|
||||
async getByDateOnWaitingList(date = dayjs()) {
|
||||
return await this.reservationsRepository
|
||||
.createQueryBuilder()
|
||||
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
||||
.andWhere('waitListed = true')
|
||||
.getMany()
|
||||
}
|
||||
|
||||
create(reservation: Reservation) {
|
||||
return this.reservationsRepository.save(reservation)
|
||||
async create(reservation: Reservation) {
|
||||
return await this.reservationsRepository.save(reservation)
|
||||
}
|
||||
|
||||
async update(reservationId: string, update: Partial<Reservation>) {
|
||||
return await this.reservationsRepository.update(reservationId, update)
|
||||
}
|
||||
|
||||
async deleteById(id: string) {
|
||||
await this.reservationsRepository.delete({ id })
|
||||
return await this.reservationsRepository.delete({ id })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import {
|
|||
} from '../runner/baanreserveren/service'
|
||||
import { RESERVATIONS_QUEUE_NAME } from './config'
|
||||
import { Reservation } from './entity'
|
||||
import { ReservationsService } from './service'
|
||||
|
||||
@Processor(RESERVATIONS_QUEUE_NAME)
|
||||
export class ReservationsWorker {
|
||||
|
|
@ -17,6 +18,9 @@ export class ReservationsWorker {
|
|||
@Inject(BaanReserverenService)
|
||||
private readonly brService: BaanReserverenService,
|
||||
|
||||
@Inject(ReservationsService)
|
||||
private readonly reservationsService: ReservationsService,
|
||||
|
||||
@Inject(LoggerService)
|
||||
private readonly loggerService: LoggerService,
|
||||
) {}
|
||||
|
|
@ -38,8 +42,11 @@ export class ReservationsWorker {
|
|||
) {
|
||||
switch (true) {
|
||||
case error instanceof NoCourtAvailableError: {
|
||||
this.loggerService.warn('No court available, adding to waiting list')
|
||||
this.loggerService.warn('No court available')
|
||||
if (!reservation.waitListed) {
|
||||
this.loggerService.log('Adding reservation to waiting list')
|
||||
await this.addReservationToWaitList(reservation)
|
||||
}
|
||||
return
|
||||
}
|
||||
default:
|
||||
|
|
@ -53,6 +60,7 @@ export class ReservationsWorker {
|
|||
async performReservation(reservation: Reservation) {
|
||||
try {
|
||||
await this.brService.performReservation(reservation)
|
||||
await this.reservationsService.deleteById(reservation.id)
|
||||
} catch (error: unknown) {
|
||||
await this.handleReservationErrors(error, reservation)
|
||||
}
|
||||
|
|
@ -60,5 +68,6 @@ export class ReservationsWorker {
|
|||
|
||||
async addReservationToWaitList(reservation: Reservation) {
|
||||
await this.brService.addReservationToWaitList(reservation)
|
||||
await this.reservationsService.update(reservation.id, { waitListed: true })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue