2023-05-26 15:43:14 -05:00
|
|
|
import { Exclude, Transform, Type } from 'class-transformer'
|
|
|
|
|
import { Dayjs } from 'dayjs'
|
|
|
|
|
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'
|
|
|
|
|
import dayjs from '../common/dayjs'
|
|
|
|
|
|
|
|
|
|
@Entity({ name: 'reservations' })
|
|
|
|
|
export class Reservation {
|
|
|
|
|
@PrimaryGeneratedColumn('uuid')
|
|
|
|
|
id: string
|
|
|
|
|
|
|
|
|
|
@Column('varchar', { length: 64, nullable: false })
|
|
|
|
|
username: string
|
|
|
|
|
|
|
|
|
|
@Transform(({ value, options: { groups = [] } }) =>
|
|
|
|
|
groups.includes('password') ? value : '***',
|
|
|
|
|
)
|
|
|
|
|
@Column('varchar', { length: 255, nullable: false })
|
|
|
|
|
password: string
|
|
|
|
|
|
|
|
|
|
@Column('datetime', { nullable: false })
|
|
|
|
|
@Type(() => Dayjs)
|
|
|
|
|
@Transform(({ value }) => dayjs(value).format())
|
|
|
|
|
dateRangeStart: Dayjs
|
2023-06-27 16:06:19 +02:00
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@Column('datetime', { nullable: false })
|
|
|
|
|
@Type(() => Dayjs)
|
|
|
|
|
@Transform(({ value }) => dayjs(value).format())
|
|
|
|
|
dateRangeEnd: Dayjs
|
|
|
|
|
|
|
|
|
|
@Column('varchar', { length: 32, nullable: false })
|
|
|
|
|
opponentId: string
|
|
|
|
|
|
|
|
|
|
@Column('varchar', { length: 255, nullable: false })
|
|
|
|
|
opponentName: string
|
|
|
|
|
|
|
|
|
|
constructor(partial: Partial<Reservation>) {
|
|
|
|
|
Object.assign(this, partial)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Exclude()
|
|
|
|
|
public createPossibleDates(): Dayjs[] {
|
2023-06-27 16:06:19 +02:00
|
|
|
const possibleDates: Dayjs[] = []
|
2023-05-26 15:43:14 -05:00
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
let possibleDate = dayjs(this.dateRangeStart).second(0).millisecond(0)
|
|
|
|
|
while (possibleDate.isSameOrBefore(this.dateRangeEnd)) {
|
|
|
|
|
possibleDates.push(possibleDate)
|
|
|
|
|
possibleDate = possibleDate.add(15, 'minute')
|
|
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
return possibleDates
|
|
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
|
2023-06-27 16:06:19 +02:00
|
|
|
/**
|
|
|
|
|
* Method to check if a reservation is available for reservation in the system
|
|
|
|
|
* @returns is reservation date within 7 days
|
|
|
|
|
*/
|
2023-05-26 15:43:14 -05:00
|
|
|
@Exclude()
|
2023-06-27 16:06:19 +02:00
|
|
|
public isAvailableForReservation(): boolean {
|
2023-05-26 15:43:14 -05:00
|
|
|
return dayjs().diff(this.dateRangeStart, 'day') <= 7
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
|
|
|
|
|
@Exclude()
|
2023-06-27 16:06:19 +02:00
|
|
|
public getAllowedReservationDate(): Dayjs {
|
2023-05-26 15:43:14 -05:00
|
|
|
return this.dateRangeStart
|
2023-06-27 16:06:19 +02:00
|
|
|
.hour(0)
|
|
|
|
|
.minute(0)
|
|
|
|
|
.second(0)
|
|
|
|
|
.millisecond(0)
|
|
|
|
|
.subtract(7, 'days')
|
|
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
}
|