2021-11-24 00:00:22 +01:00
|
|
|
import dayjs, { Dayjs } from 'dayjs'
|
|
|
|
|
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
|
|
|
|
|
dayjs.extend(isSameOrBefore)
|
|
|
|
|
|
|
|
|
|
export interface Opponent {
|
|
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DateRange {
|
|
|
|
|
start: dayjs.Dayjs
|
|
|
|
|
end: dayjs.Dayjs
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 13:11:09 +01:00
|
|
|
const RESERVATION_AVAILABLE_WITHIN_DAYS = 7
|
|
|
|
|
|
2021-11-24 00:00:22 +01:00
|
|
|
export class Reservation {
|
|
|
|
|
public readonly dateRange: DateRange
|
|
|
|
|
public readonly opponent: Opponent
|
|
|
|
|
public readonly possibleDates: Dayjs[]
|
|
|
|
|
public booked = false
|
|
|
|
|
|
|
|
|
|
constructor(dateRange: DateRange, opponent: Opponent) {
|
|
|
|
|
this.dateRange = dateRange
|
|
|
|
|
this.opponent = opponent
|
|
|
|
|
this.possibleDates = this.createPossibleDates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createPossibleDates(): Dayjs[] {
|
|
|
|
|
const possibleDates: Dayjs[] = []
|
|
|
|
|
|
|
|
|
|
const { start, end } = this.dateRange
|
|
|
|
|
|
2021-11-28 13:06:52 +01:00
|
|
|
let possibleDate = dayjs(start).second(0).millisecond(0)
|
2021-11-24 00:00:22 +01:00
|
|
|
while (possibleDate.isSameOrBefore(end)) {
|
|
|
|
|
possibleDates.push(possibleDate)
|
|
|
|
|
possibleDate = possibleDate.add(15, 'minute')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return possibleDates
|
|
|
|
|
}
|
2021-11-27 13:11:09 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to check if a reservation is available for reservation in the system
|
|
|
|
|
* @returns is reservation date within 7 days
|
|
|
|
|
*/
|
|
|
|
|
public isAvailableForReservation(): boolean {
|
2021-11-28 13:06:52 +01:00
|
|
|
return (
|
|
|
|
|
Math.ceil(this.dateRange.start.diff(dayjs(), 'days', true)) <=
|
|
|
|
|
RESERVATION_AVAILABLE_WITHIN_DAYS
|
|
|
|
|
)
|
2021-11-27 13:11:09 +01:00
|
|
|
}
|
2022-03-29 22:41:44 +02:00
|
|
|
|
|
|
|
|
public format(): unknown {
|
|
|
|
|
return {
|
|
|
|
|
opponent: this.opponent,
|
|
|
|
|
booked: this.booked,
|
|
|
|
|
possibleDates: this.possibleDates.map((date) => date.format()),
|
|
|
|
|
dateRange: {
|
|
|
|
|
start: this.dateRange.start.format(),
|
|
|
|
|
end: this.dateRange.end.format(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-24 00:00:22 +01:00
|
|
|
}
|