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>,
|
private reservationsRepository: Repository<Reservation>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
getAll() {
|
async getAll() {
|
||||||
return this.reservationsRepository.find()
|
return await this.reservationsRepository.find()
|
||||||
}
|
}
|
||||||
|
|
||||||
getById(id: string) {
|
async getById(id: string) {
|
||||||
return this.reservationsRepository.findOneBy({ id })
|
return await this.reservationsRepository.findOneBy({ id })
|
||||||
}
|
}
|
||||||
|
|
||||||
getByDate(date = dayjs()) {
|
async getByDate(date = dayjs()) {
|
||||||
return this.reservationsRepository
|
return await this.reservationsRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
||||||
.getMany()
|
.getMany()
|
||||||
}
|
}
|
||||||
|
|
||||||
getByDateOnWaitingList(date = dayjs()) {
|
async getByDateOnWaitingList(date = dayjs()) {
|
||||||
return this.reservationsRepository
|
return await this.reservationsRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
.where(`DATE(dateRangeStart, '-7 day') = DATE(:date)`, { date })
|
||||||
.andWhere('waitListed = true')
|
.andWhere('waitListed = true')
|
||||||
.getMany()
|
.getMany()
|
||||||
}
|
}
|
||||||
|
|
||||||
create(reservation: Reservation) {
|
async create(reservation: Reservation) {
|
||||||
return this.reservationsRepository.save(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) {
|
async deleteById(id: string) {
|
||||||
await this.reservationsRepository.delete({ id })
|
return await this.reservationsRepository.delete({ id })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import {
|
||||||
} from '../runner/baanreserveren/service'
|
} from '../runner/baanreserveren/service'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from './config'
|
import { RESERVATIONS_QUEUE_NAME } from './config'
|
||||||
import { Reservation } from './entity'
|
import { Reservation } from './entity'
|
||||||
|
import { ReservationsService } from './service'
|
||||||
|
|
||||||
@Processor(RESERVATIONS_QUEUE_NAME)
|
@Processor(RESERVATIONS_QUEUE_NAME)
|
||||||
export class ReservationsWorker {
|
export class ReservationsWorker {
|
||||||
|
|
@ -17,6 +18,9 @@ export class ReservationsWorker {
|
||||||
@Inject(BaanReserverenService)
|
@Inject(BaanReserverenService)
|
||||||
private readonly brService: BaanReserverenService,
|
private readonly brService: BaanReserverenService,
|
||||||
|
|
||||||
|
@Inject(ReservationsService)
|
||||||
|
private readonly reservationsService: ReservationsService,
|
||||||
|
|
||||||
@Inject(LoggerService)
|
@Inject(LoggerService)
|
||||||
private readonly loggerService: LoggerService,
|
private readonly loggerService: LoggerService,
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -38,8 +42,11 @@ export class ReservationsWorker {
|
||||||
) {
|
) {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case error instanceof NoCourtAvailableError: {
|
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)
|
await this.addReservationToWaitList(reservation)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
@ -53,6 +60,7 @@ export class ReservationsWorker {
|
||||||
async performReservation(reservation: Reservation) {
|
async performReservation(reservation: Reservation) {
|
||||||
try {
|
try {
|
||||||
await this.brService.performReservation(reservation)
|
await this.brService.performReservation(reservation)
|
||||||
|
await this.reservationsService.deleteById(reservation.id)
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
await this.handleReservationErrors(error, reservation)
|
await this.handleReservationErrors(error, reservation)
|
||||||
}
|
}
|
||||||
|
|
@ -60,5 +68,6 @@ export class ReservationsWorker {
|
||||||
|
|
||||||
async addReservationToWaitList(reservation: Reservation) {
|
async addReservationToWaitList(reservation: Reservation) {
|
||||||
await this.brService.addReservationToWaitList(reservation)
|
await this.brService.addReservationToWaitList(reservation)
|
||||||
|
await this.reservationsService.update(reservation.id, { waitListed: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue