Updating reservation to use UUIDv4 as an id

This commit is contained in:
Collin Duncan 2023-01-21 15:17:21 +01:00
parent 6b197addfb
commit bf0852d541
No known key found for this signature in database
4 changed files with 70 additions and 6 deletions

View file

@ -1,6 +1,6 @@
export const TABLE_reservations = `
CREATE TABLE IF NOT EXISTS reservations (
id INT unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
id VARCHAR(36) NOT NULL PRIMARY KEY,
username VARCHAR(64) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
date_range_start DATETIME NOT NULL,

View file

@ -1,4 +1,5 @@
import { Dayjs } from 'dayjs'
import { v4 } from 'uuid'
import dayjs from './dayjs'
import { query } from './database'
@ -20,6 +21,7 @@ export interface DateRange {
}
export class Reservation {
public readonly id: string
public readonly user: User
public readonly dateRange: DateRange
public readonly opponent: Opponent
@ -30,8 +32,10 @@ export class Reservation {
user: User,
dateRange: DateRange,
opponent: Opponent,
possibleDates?: Dayjs[]
possibleDates?: Dayjs[],
id = v4()
) {
this.id = id
this.user = user
this.dateRange = dateRange
this.opponent = opponent
@ -127,6 +131,7 @@ export class Reservation {
`
INSERT INTO reservations
(
id
username,
password,
date_range_start,
@ -141,10 +146,12 @@ export class Reservation {
?,
?,
?,
?,
?
)
`,
[
res.id,
res.user.username,
res.user.password,
res.dateRange.start,
@ -155,7 +162,19 @@ export class Reservation {
)
}
public static async fetchById(id: number): Promise<Reservation | null> {
public static async delete(res: Reservation) {
await query(
`
DELETE FROM reservations
WHERE id = $
`,
[
res.id,
]
)
}
public static async fetchById(id: string): Promise<Reservation | null> {
const response = await query<SqlReservation>(
`
SELECT *
@ -176,7 +195,9 @@ export class Reservation {
start: dayjs(sqlReservation.date_range_start),
end: dayjs(sqlReservation.date_range_end),
},
{ id: sqlReservation.opponent_id, name: sqlReservation.opponent_name }
{ id: sqlReservation.opponent_id, name: sqlReservation.opponent_name },
undefined,
sqlReservation.id,
)
return res
}
@ -205,13 +226,49 @@ export class Reservation {
start: dayjs(sqlReservation.date_range_start),
end: dayjs(sqlReservation.date_range_end),
},
{ id: sqlReservation.opponent_id, name: sqlReservation.opponent_name }
{ id: sqlReservation.opponent_id, name: sqlReservation.opponent_name },
undefined,
sqlReservation.id,
)
return res
}
return null
}
public static async fetchByDate(date: Dayjs, limit = 20): Promise<Reservation[]> {
const response = await query<SqlReservation>(
`
SELECT *
FROM reservations
WHERE DATE_FORMAT(DATE_SUB(date_range_start, INTERVAL 7 DAY), '%Y-%m-%d) = ?
ORDER BY date_range_start DESC
LIMIT ?;
`,
[
date.format('YYYY-MM-DD'),
limit,
]
)
if (response.results.length > 0) {
return response.results.map((sqlReservation) => new Reservation(
{
username: sqlReservation.username,
password: sqlReservation.password,
},
{
start: dayjs(sqlReservation.date_range_start),
end: dayjs(sqlReservation.date_range_end),
},
{ id: sqlReservation.opponent_id, name: sqlReservation.opponent_name },
undefined,
sqlReservation.id,
))
}
return []
}
}
export interface SerializedDateRange {
@ -228,6 +285,7 @@ export interface SerializedReservation {
}
export interface SqlReservation {
id: string
username: string
password: string
date_range_start: string

View file

@ -41,6 +41,7 @@ exports[`scheduler should handle valid requests outside of reservation window 1`
"$y": 2022,
},
},
"id": "1234",
"opponent": {
"id": "123",
"name": "collin",
@ -145,6 +146,7 @@ exports[`scheduler should handle valid requests within reservation window 1`] =
"$y": 2022,
},
},
"id": Any<String>,
"opponent": {
"id": "123",
"name": "collin",

View file

@ -8,6 +8,7 @@ import { schedule, SchedulerInput } from '../../../src/common/scheduler'
jest.mock('../../../src/common/logger')
jest.mock('../../../src/common/reserver')
jest.mock('uuid', () => ({ v4: () => '1234' }))
jest.useFakeTimers().setSystemTime(new Date('2022-01-01'))
describe('scheduler', () => {
@ -26,6 +27,7 @@ describe('scheduler', () => {
expect(await schedule(payload)).toMatchSnapshot({
scheduledReservation: {
reservation: {
id: expect.any(String),
user: {
username: 'collin',
password: expect.any(String),
@ -52,7 +54,9 @@ describe('scheduler', () => {
reservation: new Reservation(
{ username: 'collin', password: expect.any(String) },
{ start, end },
{ id: '123', name: 'collin' }
{ id: '123', name: 'collin' },
undefined,
'1234',
),
scheduledFor: start
.subtract(7, 'days')