2023-05-26 15:43:14 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common'
|
2023-06-29 10:32:09 +02:00
|
|
|
import { instanceToPlain } from 'class-transformer'
|
2023-01-20 15:31:04 +01:00
|
|
|
import { Dayjs } from 'dayjs'
|
2023-05-26 15:43:14 -05:00
|
|
|
import { ElementHandle, Page } from 'puppeteer'
|
2023-08-29 10:44:12 +02:00
|
|
|
import { LoggerService } from 'src/logger/service.logger'
|
2023-06-29 10:32:09 +02:00
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
import dayjs from '../../common/dayjs'
|
|
|
|
|
import { Reservation } from '../../reservations/entity'
|
2023-06-29 10:32:09 +02:00
|
|
|
import { EmptyPage } from '../pages/empty'
|
2023-05-26 15:43:14 -05:00
|
|
|
|
2023-07-31 15:18:08 +02:00
|
|
|
const BAAN_RESERVEREN_ROOT_URL = 'https://squashcity.baanreserveren.nl'
|
2023-05-26 15:43:14 -05:00
|
|
|
|
|
|
|
|
export enum BaanReserverenUrls {
|
2023-06-27 16:06:19 +02:00
|
|
|
Reservations = '/reservations',
|
|
|
|
|
Logout = '/auth/logout',
|
2023-07-28 19:50:04 +02:00
|
|
|
WaitingList = '/waitinglist',
|
|
|
|
|
WaitingListAdd = '/waitinglist/add',
|
2023-05-26 15:43:14 -05:00
|
|
|
}
|
2021-11-15 11:28:39 +01:00
|
|
|
|
2023-02-10 12:24:27 +01:00
|
|
|
enum SessionAction {
|
2023-06-27 16:06:19 +02:00
|
|
|
NoAction,
|
|
|
|
|
Logout,
|
|
|
|
|
Login,
|
2023-02-10 12:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
interface BaanReserverenSession {
|
2023-06-27 16:06:19 +02:00
|
|
|
username: string
|
|
|
|
|
startedAt: Dayjs
|
2023-02-10 12:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
const MIN_TYPING_DELAY_MS = 10
|
|
|
|
|
const MAX_TYPING_DELAY_MS = 30
|
|
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@Injectable()
|
|
|
|
|
export class BaanReserverenService {
|
2023-06-27 16:06:19 +02:00
|
|
|
private session: BaanReserverenSession | null = null
|
|
|
|
|
|
|
|
|
|
constructor(
|
2023-06-28 09:28:24 +02:00
|
|
|
@Inject(LoggerService)
|
|
|
|
|
private readonly loggerService: LoggerService,
|
2023-06-27 16:06:19 +02:00
|
|
|
|
|
|
|
|
@Inject(EmptyPage)
|
|
|
|
|
private readonly page: Page,
|
|
|
|
|
) {}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
// tryna be sneaky
|
|
|
|
|
private getTypingDelay() {
|
|
|
|
|
return (
|
|
|
|
|
(MAX_TYPING_DELAY_MS - MIN_TYPING_DELAY_MS) * Math.random() +
|
|
|
|
|
MIN_TYPING_DELAY_MS
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 15:18:08 +02:00
|
|
|
private async checkSession(username: string) {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Checking session', {
|
|
|
|
|
username,
|
|
|
|
|
session: this.session,
|
|
|
|
|
})
|
2023-07-31 15:18:08 +02:00
|
|
|
if (this.page.url().includes(BAAN_RESERVEREN_ROOT_URL)) {
|
2023-08-28 14:23:08 +02:00
|
|
|
// Check session by going to /reservations to see if we are still logged in via cookies
|
|
|
|
|
await this.navigateToReservations()
|
2023-07-31 15:18:08 +02:00
|
|
|
if (this.page.url().includes('?reason=LOGGED_IN')) {
|
|
|
|
|
return SessionAction.Login
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
return this.session?.username !== username
|
|
|
|
|
? SessionAction.Logout
|
|
|
|
|
: SessionAction.NoAction
|
|
|
|
|
}
|
|
|
|
|
return SessionAction.Login
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private startSession(username: string) {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Starting session', { username })
|
2023-06-27 16:06:19 +02:00
|
|
|
if (this.session && this.session.username !== username) {
|
2023-08-31 11:30:15 +02:00
|
|
|
throw new SessionStartError('Session already started')
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.session?.username === username) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.session = {
|
|
|
|
|
username,
|
|
|
|
|
startedAt: dayjs(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private endSession() {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Ending session', { session: this.session })
|
2023-06-27 16:06:19 +02:00
|
|
|
this.session = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async login(username: string, password: string) {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Logging in', { username })
|
2023-06-27 16:06:19 +02:00
|
|
|
await this.page
|
|
|
|
|
.waitForSelector('input[name=username]')
|
2023-07-28 19:50:04 +02:00
|
|
|
.then((i) => i?.type(username, { delay: this.getTypingDelay() }))
|
2023-06-27 16:06:19 +02:00
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:53:28 +02:00
|
|
|
throw new RunnerLoginUsernameInputError(e)
|
2023-06-27 16:06:19 +02:00
|
|
|
})
|
|
|
|
|
await this.page
|
|
|
|
|
.$('input[name=password]')
|
2023-07-28 19:50:04 +02:00
|
|
|
.then((i) => i?.type(password, { delay: this.getTypingDelay() }))
|
2023-06-27 16:06:19 +02:00
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:53:28 +02:00
|
|
|
throw new RunnerLoginPasswordInputError(e)
|
2023-06-27 16:06:19 +02:00
|
|
|
})
|
|
|
|
|
await this.page
|
|
|
|
|
.$('button')
|
|
|
|
|
.then((b) => b?.click())
|
|
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:53:28 +02:00
|
|
|
throw new RunnerLoginSubmitError(e)
|
2023-06-27 16:06:19 +02:00
|
|
|
})
|
2023-08-31 17:39:03 +02:00
|
|
|
await this.page.waitForNetworkIdle()
|
2023-06-27 16:06:19 +02:00
|
|
|
this.startSession(username)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async logout() {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Logging out')
|
2023-07-31 15:18:08 +02:00
|
|
|
await this.page.goto(
|
2023-08-10 13:35:32 +02:00
|
|
|
`${BAAN_RESERVEREN_ROOT_URL}/${BaanReserverenUrls.Logout}`,
|
2023-07-31 15:18:08 +02:00
|
|
|
)
|
2023-08-31 17:39:03 +02:00
|
|
|
await this.page.waitForNetworkIdle()
|
2023-06-27 16:06:19 +02:00
|
|
|
this.endSession()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async init(reservation: Reservation) {
|
2023-06-28 21:40:29 +02:00
|
|
|
this.loggerService.debug('Initializing', {
|
|
|
|
|
reservation: instanceToPlain(reservation),
|
|
|
|
|
})
|
2023-07-31 15:18:08 +02:00
|
|
|
await this.page.goto(BAAN_RESERVEREN_ROOT_URL)
|
|
|
|
|
const action = await this.checkSession(reservation.username)
|
2023-06-27 16:06:19 +02:00
|
|
|
switch (action) {
|
|
|
|
|
case SessionAction.Logout:
|
|
|
|
|
await this.logout()
|
|
|
|
|
await this.login(reservation.username, reservation.password)
|
|
|
|
|
break
|
|
|
|
|
case SessionAction.Login:
|
|
|
|
|
await this.login(reservation.username, reservation.password)
|
|
|
|
|
break
|
|
|
|
|
case SessionAction.NoAction:
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getLastVisibleDay(): Dayjs {
|
|
|
|
|
const lastDayOfMonth = dayjs().add(1, 'month').set('date', 0)
|
|
|
|
|
let daysToAdd = 0
|
|
|
|
|
switch (lastDayOfMonth.day()) {
|
|
|
|
|
case 0:
|
|
|
|
|
daysToAdd = 0
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
daysToAdd = 7 - lastDayOfMonth.day()
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
return lastDayOfMonth.add(daysToAdd, 'day')
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async navigateToDay(date: Dayjs) {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Navigating to day', { date })
|
2023-06-27 16:06:19 +02:00
|
|
|
if (this.getLastVisibleDay().isBefore(date)) {
|
|
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.waitForSelector('td.month.next')
|
2023-06-27 16:06:19 +02:00
|
|
|
.then((d) => d?.click())
|
|
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:25:40 +02:00
|
|
|
this.loggerService.error('Failed to switch months', { error: e })
|
2023-06-27 16:06:19 +02:00
|
|
|
throw new RunnerNavigationMonthError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.waitForSelector(
|
2023-06-27 16:06:19 +02:00
|
|
|
`td#cal_${date.get('year')}_${date.get('month') + 1}_${date.get(
|
|
|
|
|
'date',
|
|
|
|
|
)}`,
|
|
|
|
|
)
|
|
|
|
|
.then((d) => d?.click())
|
|
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:25:40 +02:00
|
|
|
this.loggerService.error('Failed to select day', { error: e })
|
2023-06-27 16:06:19 +02:00
|
|
|
throw new RunnerNavigationDayError(e)
|
|
|
|
|
})
|
|
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.waitForSelector(
|
2023-06-27 16:06:19 +02:00
|
|
|
`td#cal_${date.get('year')}_${date.get('month') + 1}_${date.get(
|
|
|
|
|
'date',
|
|
|
|
|
)}.selected`,
|
|
|
|
|
)
|
|
|
|
|
.catch((e: Error) => {
|
2023-06-28 16:25:40 +02:00
|
|
|
this.loggerService.error('Failed to wait for selected day', {
|
|
|
|
|
error: e,
|
|
|
|
|
})
|
2023-06-27 16:06:19 +02:00
|
|
|
throw new RunnerNavigationSelectionError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async navigateToWaitingList() {
|
2023-07-31 15:18:08 +02:00
|
|
|
this.loggerService.debug('Navigating to waiting list')
|
|
|
|
|
await this.page
|
2023-08-10 13:35:32 +02:00
|
|
|
.goto(`${BAAN_RESERVEREN_ROOT_URL}/${BaanReserverenUrls.WaitingList}`)
|
2023-07-31 15:18:08 +02:00
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListNavigationError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 14:23:08 +02:00
|
|
|
private async navigateToReservations() {
|
2023-08-31 17:39:03 +02:00
|
|
|
this.loggerService.debug('Navigating to reservations')
|
2023-08-28 14:23:08 +02:00
|
|
|
await this.page
|
|
|
|
|
.goto(`${BAAN_RESERVEREN_ROOT_URL}/${BaanReserverenUrls.Reservations}`)
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListNavigationError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 11:30:15 +02:00
|
|
|
private async recordWaitingListEntries(): Promise<number[]> {
|
|
|
|
|
const waitingListEntriesElements = await this.page.$$(
|
|
|
|
|
'#content tbody tr td:nth-child(1)',
|
|
|
|
|
)
|
|
|
|
|
if (!waitingListEntriesElements) return []
|
|
|
|
|
|
|
|
|
|
const waitingListIds = (
|
|
|
|
|
await Promise.all(
|
|
|
|
|
waitingListEntriesElements.map(async (e) => {
|
|
|
|
|
const elementTextContent = await (
|
|
|
|
|
await e.getProperty('textContent')
|
|
|
|
|
).jsonValue()
|
|
|
|
|
|
|
|
|
|
if (elementTextContent) {
|
|
|
|
|
return Number.parseInt(elementTextContent)
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
).filter((id): id is number => id != null && typeof id === 'number')
|
|
|
|
|
|
|
|
|
|
return waitingListIds
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private findNewWaitingListEntryId(
|
|
|
|
|
previous: number[],
|
|
|
|
|
current: number[],
|
|
|
|
|
): number | null {
|
|
|
|
|
const previousSet = new Set(previous)
|
|
|
|
|
for (const c of current) {
|
|
|
|
|
if (!previousSet.has(c)) return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async deleteWaitingListEntryRowById(id: number) {
|
|
|
|
|
const rows = await this.page.$x(`//td[text()="${id}"]/parent::tr`)
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
throw new WaitingListEntryDeletionError(
|
|
|
|
|
'Cannot find waiting list entry to delete',
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 17:39:03 +02:00
|
|
|
const acceptedDialogPromise = new Promise<void>((res, rej) => {
|
|
|
|
|
this.page.on('dialog', async (dialog) => {
|
|
|
|
|
await dialog.accept().catch(rej)
|
|
|
|
|
res()
|
|
|
|
|
})
|
|
|
|
|
setTimeout(rej, 10000)
|
|
|
|
|
})
|
|
|
|
|
|
2023-08-31 11:30:15 +02:00
|
|
|
const deleteButton = await rows[0].$('a.wl-delete')
|
|
|
|
|
await deleteButton?.click()
|
2023-08-31 17:39:03 +02:00
|
|
|
await acceptedDialogPromise
|
2023-08-31 11:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async openWaitingListDialog() {
|
2023-07-31 15:18:08 +02:00
|
|
|
this.loggerService.debug('Opening waiting list dialog')
|
2023-08-10 13:35:32 +02:00
|
|
|
await this.page.waitForNetworkIdle()
|
|
|
|
|
await this.page.goto(
|
|
|
|
|
`${BAAN_RESERVEREN_ROOT_URL}/${BaanReserverenUrls.WaitingListAdd}`,
|
|
|
|
|
)
|
2023-07-28 19:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async selectAvailableTime(reservation: Reservation) {
|
2023-06-28 21:40:29 +02:00
|
|
|
this.loggerService.debug('Selecting available time', {
|
|
|
|
|
reservation: instanceToPlain(reservation),
|
|
|
|
|
})
|
2023-06-27 16:06:19 +02:00
|
|
|
let freeCourt: ElementHandle | null | undefined
|
|
|
|
|
let i = 0
|
|
|
|
|
const possibleDates = reservation.createPossibleDates()
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Possible dates', { possibleDates })
|
2023-06-27 16:06:19 +02:00
|
|
|
while (i < possibleDates.length && !freeCourt) {
|
|
|
|
|
const possibleDate = possibleDates[i]
|
|
|
|
|
const timeString = possibleDate.format('HH:mm')
|
|
|
|
|
const selector =
|
|
|
|
|
`tr[data-time='${timeString}']` + `> td.free[rowspan='3'][type='free']`
|
2023-08-31 11:30:15 +02:00
|
|
|
freeCourt = await this.page.$(selector)
|
2023-06-27 16:06:19 +02:00
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!freeCourt) {
|
2023-08-10 13:35:32 +02:00
|
|
|
throw new NoCourtAvailableError('No court available for reservation')
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-28 21:40:29 +02:00
|
|
|
this.loggerService.debug('Free court found')
|
2023-06-28 09:28:24 +02:00
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
await freeCourt.click().catch((e: Error) => {
|
|
|
|
|
throw new RunnerCourtSelectionError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async selectOpponent(id: string, name: string) {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Selecting opponent', { id, name })
|
2023-06-27 16:06:19 +02:00
|
|
|
const player2Search = await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.waitForSelector('input:has(~ select[name="players[2]"])')
|
2023-06-27 16:06:19 +02:00
|
|
|
.catch((e: Error) => {
|
|
|
|
|
throw new RunnerOpponentSearchError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
await player2Search
|
|
|
|
|
?.type(name, { delay: this.getTypingDelay() })
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
throw new RunnerOpponentSearchInputError(e)
|
|
|
|
|
})
|
2023-08-31 11:30:15 +02:00
|
|
|
await this.page.waitForNetworkIdle().catch((e: Error) => {
|
2023-06-27 16:06:19 +02:00
|
|
|
throw new RunnerOpponentSearchNetworkError(e)
|
|
|
|
|
})
|
|
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.$('select.br-user-select[name="players[2]"]')
|
2023-06-27 16:06:19 +02:00
|
|
|
.then((d) => d?.select(id))
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
throw new RunnerOpponentSearchSelectionError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async confirmReservation() {
|
2023-06-28 09:28:24 +02:00
|
|
|
this.loggerService.debug('Confirming reservation')
|
2023-06-27 16:06:19 +02:00
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.$('input#__make_submit')
|
2023-06-27 16:06:19 +02:00
|
|
|
.then((b) => b?.click())
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
throw new RunnerReservationConfirmButtonError(e)
|
|
|
|
|
})
|
|
|
|
|
await this.page
|
2023-08-31 11:30:15 +02:00
|
|
|
.waitForSelector('input#__make_submit2')
|
2023-06-27 16:06:19 +02:00
|
|
|
.then((b) => b?.click())
|
|
|
|
|
.catch((e: Error) => {
|
|
|
|
|
throw new RunnerReservationConfirmSubmitError(e)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 19:50:04 +02:00
|
|
|
private async inputWaitingListDetails(reservation: Reservation) {
|
2023-07-31 15:18:08 +02:00
|
|
|
this.loggerService.debug('Inputting waiting list details')
|
2023-08-31 11:30:15 +02:00
|
|
|
const startDateInput = await this.page.$('input[name="start_date"]')
|
2023-07-31 15:18:08 +02:00
|
|
|
// Click 3 times to select all existing text
|
|
|
|
|
await startDateInput?.click({ count: 3, delay: 10 }).catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListInputError(e)
|
|
|
|
|
})
|
|
|
|
|
await startDateInput
|
|
|
|
|
?.type(reservation.dateRangeStart.format('DD-MM-YYYY'), {
|
|
|
|
|
delay: this.getTypingDelay(),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListInputError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
|
2023-08-31 11:30:15 +02:00
|
|
|
const endDateInput = await this.page.$('input[name="end_date"]')
|
2023-07-31 15:18:08 +02:00
|
|
|
await endDateInput
|
|
|
|
|
?.type(reservation.dateRangeEnd.format('DD-MM-YYYY'), {
|
|
|
|
|
delay: this.getTypingDelay(),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListInputError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
|
2023-08-31 11:30:15 +02:00
|
|
|
const startTimeInput = await this.page.$('input[name="start_time"]')
|
2023-08-10 13:35:32 +02:00
|
|
|
await startTimeInput
|
2023-07-31 15:18:08 +02:00
|
|
|
?.type(reservation.dateRangeStart.format('HH:mm'), {
|
|
|
|
|
delay: this.getTypingDelay(),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListInputError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
|
|
|
|
|
// Use the same time for start and end so that the waiting list only notifies for start time
|
2023-08-31 11:30:15 +02:00
|
|
|
const endTimeInput = await this.page.$('input[name="end_time"]')
|
2023-08-10 13:35:32 +02:00
|
|
|
await endTimeInput
|
|
|
|
|
?.type(reservation.dateRangeStart.add(1, 'minutes').format('HH:mm'), {
|
2023-07-31 15:18:08 +02:00
|
|
|
delay: this.getTypingDelay(),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListInputError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async confirmWaitingListDetails() {
|
2023-07-31 15:18:08 +02:00
|
|
|
this.loggerService.debug('Confirming waiting list details')
|
2023-08-31 17:39:03 +02:00
|
|
|
const saveButton = await this.page.$('input[type="submit"]')
|
2023-07-31 15:18:08 +02:00
|
|
|
await saveButton?.click().catch((e) => {
|
|
|
|
|
throw new RunnerWaitingListConfirmError(e)
|
|
|
|
|
})
|
2023-07-28 19:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
public async performReservation(reservation: Reservation) {
|
|
|
|
|
await this.init(reservation)
|
|
|
|
|
await this.navigateToDay(reservation.dateRangeStart)
|
|
|
|
|
await this.selectAvailableTime(reservation)
|
|
|
|
|
await this.selectOpponent(reservation.opponentId, reservation.opponentName)
|
2023-06-28 09:28:24 +02:00
|
|
|
await this.confirmReservation()
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
2023-07-28 19:50:04 +02:00
|
|
|
|
|
|
|
|
public async addReservationToWaitList(reservation: Reservation) {
|
|
|
|
|
await this.init(reservation)
|
|
|
|
|
await this.navigateToWaitingList()
|
2023-08-31 11:30:15 +02:00
|
|
|
const previousWaitingListIds = await this.recordWaitingListEntries()
|
2023-07-28 19:50:04 +02:00
|
|
|
await this.openWaitingListDialog()
|
|
|
|
|
await this.inputWaitingListDetails(reservation)
|
|
|
|
|
await this.confirmWaitingListDetails()
|
2023-08-31 11:30:15 +02:00
|
|
|
const currentWaitingListIds = await this.recordWaitingListEntries()
|
|
|
|
|
const waitingListId = this.findNewWaitingListEntryId(
|
|
|
|
|
previousWaitingListIds,
|
|
|
|
|
currentWaitingListIds,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (waitingListId == null) {
|
|
|
|
|
throw new WaitingListSubmissionError(
|
|
|
|
|
'Failed to find new waiting list entry',
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return waitingListId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async removeReservationFromWaitList(reservation: Reservation) {
|
|
|
|
|
if (!reservation.waitListed || !reservation.waitingListId) return
|
|
|
|
|
|
|
|
|
|
await this.init(reservation)
|
|
|
|
|
await this.navigateToWaitingList()
|
|
|
|
|
await this.deleteWaitingListEntryRowById(reservation.waitingListId)
|
2023-07-28 19:50:04 +02:00
|
|
|
}
|
2021-11-17 14:17:25 +01:00
|
|
|
}
|
2023-01-30 12:38:42 +01:00
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
export class RunnerError extends Error {
|
2023-08-10 13:35:32 +02:00
|
|
|
constructor(error: Error, name?: string) {
|
2023-06-27 16:06:19 +02:00
|
|
|
super(error.message)
|
|
|
|
|
this.stack = error.stack
|
2023-08-10 13:35:32 +02:00
|
|
|
this.name = name ?? 'RunnerError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class PuppeteerError extends RunnerError {
|
|
|
|
|
constructor(error: Error, name?: string) {
|
|
|
|
|
super(error, name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class PuppeteerBrowserLaunchError extends PuppeteerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'PuppeteerBrowserLaunchError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class PuppeteerNewPageError extends PuppeteerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'PuppeteerNewPageError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class RunnerNewSessionError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerNewSessionError')
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
2023-01-29 14:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerLogoutError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerLogoutError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-20 11:08:56 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerLoginNavigationError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerLoginNavigationError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerLoginUsernameInputError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerLoginUsernameInputError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerLoginPasswordInputError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerLoginPasswordInputError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerLoginSubmitError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerLoginSubmitError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-10 12:24:27 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerNavigationMonthError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerNavigationMonthError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerNavigationDayError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerNavigationDayError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerNavigationSelectionError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerNavigationSelectionError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-29 14:34:39 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerWaitingListNavigationError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerWaitingListNavigationError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerWaitingListNavigationMenuError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerWaitingListNavigationMenuError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerWaitingListNavigationAddError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerWaitingListNavigationAddError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-29 14:34:39 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerWaitingListInputError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerWaitingListInputError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 15:18:08 +02:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerWaitingListConfirmError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerWaitingListConfirmError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 15:18:08 +02:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerCourtSelectionError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerCourtSelectionError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 15:18:08 +02:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class NoCourtAvailableError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'NoCourtAvailableError'
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-29 14:34:39 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerOpponentSearchError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerOpponentSearchError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerOpponentSearchInputError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerOpponentSearchInputError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerOpponentSearchNetworkError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerOpponentSearchNetworkError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerOpponentSearchSelectionError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerOpponentSearchSelectionError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-29 14:34:39 +01:00
|
|
|
|
2023-08-10 13:35:32 +02:00
|
|
|
export class RunnerReservationConfirmButtonError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerReservationConfirmButtonError')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export class RunnerReservationConfirmSubmitError extends RunnerError {
|
|
|
|
|
constructor(error: Error) {
|
|
|
|
|
super(error, 'RunnerReservationConfirmSubmitError')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-31 11:30:15 +02:00
|
|
|
|
|
|
|
|
export class SessionStartError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'SessionStartError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class WaitingListSubmissionError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'WaitingListSubmissionError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class WaitingListEntryDeletionError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'WaitingListEntryDeletionError'
|
|
|
|
|
}
|
|
|
|
|
}
|