Updating reservation to use UUIDv4 as an id
This commit is contained in:
parent
6b197addfb
commit
bf0852d541
4 changed files with 70 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
export const TABLE_reservations = `
|
export const TABLE_reservations = `
|
||||||
CREATE TABLE IF NOT EXISTS 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,
|
username VARCHAR(64) NOT NULL UNIQUE,
|
||||||
password VARCHAR(255) NOT NULL,
|
password VARCHAR(255) NOT NULL,
|
||||||
date_range_start DATETIME NOT NULL,
|
date_range_start DATETIME NOT NULL,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { Dayjs } from 'dayjs'
|
import { Dayjs } from 'dayjs'
|
||||||
|
import { v4 } from 'uuid'
|
||||||
import dayjs from './dayjs'
|
import dayjs from './dayjs'
|
||||||
import { query } from './database'
|
import { query } from './database'
|
||||||
|
|
||||||
|
|
@ -20,6 +21,7 @@ export interface DateRange {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Reservation {
|
export class Reservation {
|
||||||
|
public readonly id: string
|
||||||
public readonly user: User
|
public readonly user: User
|
||||||
public readonly dateRange: DateRange
|
public readonly dateRange: DateRange
|
||||||
public readonly opponent: Opponent
|
public readonly opponent: Opponent
|
||||||
|
|
@ -30,8 +32,10 @@ export class Reservation {
|
||||||
user: User,
|
user: User,
|
||||||
dateRange: DateRange,
|
dateRange: DateRange,
|
||||||
opponent: Opponent,
|
opponent: Opponent,
|
||||||
possibleDates?: Dayjs[]
|
possibleDates?: Dayjs[],
|
||||||
|
id = v4()
|
||||||
) {
|
) {
|
||||||
|
this.id = id
|
||||||
this.user = user
|
this.user = user
|
||||||
this.dateRange = dateRange
|
this.dateRange = dateRange
|
||||||
this.opponent = opponent
|
this.opponent = opponent
|
||||||
|
|
@ -127,6 +131,7 @@ export class Reservation {
|
||||||
`
|
`
|
||||||
INSERT INTO reservations
|
INSERT INTO reservations
|
||||||
(
|
(
|
||||||
|
id
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
date_range_start,
|
date_range_start,
|
||||||
|
|
@ -141,10 +146,12 @@ export class Reservation {
|
||||||
?,
|
?,
|
||||||
?,
|
?,
|
||||||
?,
|
?,
|
||||||
|
?,
|
||||||
?
|
?
|
||||||
)
|
)
|
||||||
`,
|
`,
|
||||||
[
|
[
|
||||||
|
res.id,
|
||||||
res.user.username,
|
res.user.username,
|
||||||
res.user.password,
|
res.user.password,
|
||||||
res.dateRange.start,
|
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>(
|
const response = await query<SqlReservation>(
|
||||||
`
|
`
|
||||||
SELECT *
|
SELECT *
|
||||||
|
|
@ -176,7 +195,9 @@ export class Reservation {
|
||||||
start: dayjs(sqlReservation.date_range_start),
|
start: dayjs(sqlReservation.date_range_start),
|
||||||
end: dayjs(sqlReservation.date_range_end),
|
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 res
|
||||||
}
|
}
|
||||||
|
|
@ -205,13 +226,49 @@ export class Reservation {
|
||||||
start: dayjs(sqlReservation.date_range_start),
|
start: dayjs(sqlReservation.date_range_start),
|
||||||
end: dayjs(sqlReservation.date_range_end),
|
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 res
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
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 {
|
export interface SerializedDateRange {
|
||||||
|
|
@ -228,6 +285,7 @@ export interface SerializedReservation {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SqlReservation {
|
export interface SqlReservation {
|
||||||
|
id: string
|
||||||
username: string
|
username: string
|
||||||
password: string
|
password: string
|
||||||
date_range_start: string
|
date_range_start: string
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ exports[`scheduler should handle valid requests outside of reservation window 1`
|
||||||
"$y": 2022,
|
"$y": 2022,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"id": "1234",
|
||||||
"opponent": {
|
"opponent": {
|
||||||
"id": "123",
|
"id": "123",
|
||||||
"name": "collin",
|
"name": "collin",
|
||||||
|
|
@ -145,6 +146,7 @@ exports[`scheduler should handle valid requests within reservation window 1`] =
|
||||||
"$y": 2022,
|
"$y": 2022,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"id": Any<String>,
|
||||||
"opponent": {
|
"opponent": {
|
||||||
"id": "123",
|
"id": "123",
|
||||||
"name": "collin",
|
"name": "collin",
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import { schedule, SchedulerInput } from '../../../src/common/scheduler'
|
||||||
|
|
||||||
jest.mock('../../../src/common/logger')
|
jest.mock('../../../src/common/logger')
|
||||||
jest.mock('../../../src/common/reserver')
|
jest.mock('../../../src/common/reserver')
|
||||||
|
jest.mock('uuid', () => ({ v4: () => '1234' }))
|
||||||
jest.useFakeTimers().setSystemTime(new Date('2022-01-01'))
|
jest.useFakeTimers().setSystemTime(new Date('2022-01-01'))
|
||||||
|
|
||||||
describe('scheduler', () => {
|
describe('scheduler', () => {
|
||||||
|
|
@ -26,6 +27,7 @@ describe('scheduler', () => {
|
||||||
expect(await schedule(payload)).toMatchSnapshot({
|
expect(await schedule(payload)).toMatchSnapshot({
|
||||||
scheduledReservation: {
|
scheduledReservation: {
|
||||||
reservation: {
|
reservation: {
|
||||||
|
id: expect.any(String),
|
||||||
user: {
|
user: {
|
||||||
username: 'collin',
|
username: 'collin',
|
||||||
password: expect.any(String),
|
password: expect.any(String),
|
||||||
|
|
@ -52,7 +54,9 @@ describe('scheduler', () => {
|
||||||
reservation: new Reservation(
|
reservation: new Reservation(
|
||||||
{ username: 'collin', password: expect.any(String) },
|
{ username: 'collin', password: expect.any(String) },
|
||||||
{ start, end },
|
{ start, end },
|
||||||
{ id: '123', name: 'collin' }
|
{ id: '123', name: 'collin' },
|
||||||
|
undefined,
|
||||||
|
'1234',
|
||||||
),
|
),
|
||||||
scheduledFor: start
|
scheduledFor: start
|
||||||
.subtract(7, 'days')
|
.subtract(7, 'days')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue