From bf0852d541716faab1e47811b3f8a36949abc92a Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Sat, 21 Jan 2023 15:17:21 +0100 Subject: [PATCH] Updating reservation to use UUIDv4 as an id --- src/common/database/sql.ts | 2 +- src/common/reservation.ts | 66 +++++++++++++++++-- .../__snapshots__/scheduler.test.ts.snap | 2 + tests/unit/common/scheduler.test.ts | 6 +- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/common/database/sql.ts b/src/common/database/sql.ts index 6e19552..c9e43b5 100644 --- a/src/common/database/sql.ts +++ b/src/common/database/sql.ts @@ -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, diff --git a/src/common/reservation.ts b/src/common/reservation.ts index 9dcc63a..8884cf0 100644 --- a/src/common/reservation.ts +++ b/src/common/reservation.ts @@ -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 { + public static async delete(res: Reservation) { + await query( + ` + DELETE FROM reservations + WHERE id = $ + `, + [ + res.id, + ] + ) + } + + public static async fetchById(id: string): Promise { const response = await query( ` 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 { + const response = await query( + ` + 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 diff --git a/tests/unit/common/__snapshots__/scheduler.test.ts.snap b/tests/unit/common/__snapshots__/scheduler.test.ts.snap index 1b6a655..70f303c 100644 --- a/tests/unit/common/__snapshots__/scheduler.test.ts.snap +++ b/tests/unit/common/__snapshots__/scheduler.test.ts.snap @@ -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, "opponent": { "id": "123", "name": "collin", diff --git a/tests/unit/common/scheduler.test.ts b/tests/unit/common/scheduler.test.ts index 1d11f7f..0007049 100644 --- a/tests/unit/common/scheduler.test.ts +++ b/tests/unit/common/scheduler.test.ts @@ -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')