Skip to content

Commit 1d8ece3

Browse files
committed
Revert "Remove is_past from database and dynamically calculate value form current time"
This reverts commit fd713d2.
1 parent fd713d2 commit 1d8ece3

8 files changed

Lines changed: 94 additions & 149 deletions

File tree

worker/migrations/0001_remove_is_past.sql

Lines changed: 0 additions & 6 deletions
This file was deleted.

worker/migrations/README.md

Lines changed: 0 additions & 72 deletions
This file was deleted.

worker/schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS fixtures (
1919
home_team TEXT NOT NULL,
2020
away_team TEXT NOT NULL,
2121
venue TEXT,
22+
is_past INTEGER DEFAULT 0,
2223
created_at INTEGER NOT NULL,
2324
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
2425
);

worker/src/database.integration.test.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,28 @@ describe('DatabaseService Integration Tests', () => {
9999
expect(fixture.venue).toBe('Test Venue');
100100
});
101101

102-
it('should create a fixture with all required fields', async () => {
103-
const fixture = await db.createFixture(
102+
it('should mark past fixtures correctly', async () => {
103+
const pastFixture = await db.createFixture(
104+
'team-123',
105+
'2020-01-01',
106+
'Jan 1 Wed 19:00',
107+
'Home Team',
108+
'Away Team'
109+
);
110+
111+
expect(pastFixture.is_past).toBe(1);
112+
});
113+
114+
it('should mark future fixtures correctly', async () => {
115+
const futureFixture = await db.createFixture(
104116
'team-123',
105117
'2030-12-31',
106118
'Dec 31 Wed 19:00',
107119
'Home Team',
108-
'Away Team',
109-
'Test Venue'
120+
'Away Team'
110121
);
111122

112-
expect(fixture.id).toBeDefined();
113-
expect(fixture.team_id).toBe('team-123');
114-
expect(fixture.match_date).toBe('2030-12-31');
115-
expect(fixture.day_time).toBe('Dec 31 Wed 19:00');
116-
expect(fixture.home_team).toBe('Home Team');
117-
expect(fixture.away_team).toBe('Away Team');
118-
expect(fixture.venue).toBe('Test Venue');
119-
expect(fixture.created_at).toBeDefined();
123+
expect(futureFixture.is_past).toBe(0);
120124
});
121125

122126
it('should get fixtures for a team', async () => {
@@ -129,6 +133,7 @@ describe('DatabaseService Integration Tests', () => {
129133
home_team: 'Home Team 1',
130134
away_team: 'Away Team 1',
131135
venue: null,
136+
is_past: 0,
132137
created_at: Date.now()
133138
},
134139
{
@@ -139,6 +144,7 @@ describe('DatabaseService Integration Tests', () => {
139144
home_team: 'Home Team 2',
140145
away_team: 'Away Team 2',
141146
venue: 'Test Venue',
147+
is_past: 0,
142148
created_at: Date.now()
143149
}
144150
];
@@ -364,7 +370,7 @@ describe('DatabaseService Integration Tests', () => {
364370
);
365371

366372
expect(fixture.id).toBeDefined();
367-
expect(fixture.match_date).toBe('2026-01-15');
373+
expect(fixture.is_past).toBe(0);
368374

369375
// Create availability
370376
await db.createAvailability(fixture.id, player1.id, true);
@@ -390,6 +396,7 @@ describe('DatabaseService Integration Tests', () => {
390396
home_team: 'Home United',
391397
away_team: 'Away City',
392398
venue: 'Test Venue',
399+
is_past: 0,
393400
created_at: Date.now()
394401
};
395402

@@ -426,11 +433,12 @@ describe('DatabaseService Integration Tests', () => {
426433
mockD1.prepare = () => ({
427434
bind: (...params: any[]) => {
428435
// Verify update was called with correct parameters
429-
if (params.length === 3) {
436+
if (params.length === 4) {
430437
updateCalled = true;
431438
expect(params[0]).toBe('2026-04-20'); // match_date
432439
expect(params[1]).toBe('Apr 20 19:00'); // day_time
433-
expect(params[2]).toBe(fixtureId);
440+
expect(params[2]).toBe(0); // is_past (future date)
441+
expect(params[3]).toBe(fixtureId);
434442
}
435443
return {
436444
run: async () => ({ success: true })
@@ -443,6 +451,26 @@ describe('DatabaseService Integration Tests', () => {
443451
expect(updateCalled).toBe(true);
444452
});
445453

454+
it('should mark fixture as past when updating to past date', async () => {
455+
const fixtureId = 'fixture-123';
456+
let isPastValue: number | null = null;
457+
458+
mockD1.prepare = () => ({
459+
bind: (...params: any[]) => {
460+
if (params.length === 4) {
461+
isPastValue = params[2]; // is_past parameter
462+
}
463+
return {
464+
run: async () => ({ success: true })
465+
};
466+
}
467+
});
468+
469+
await db.updateFixtureDate(fixtureId, '2025-01-01', 'Jan 1 20:00');
470+
471+
expect(isPastValue).toBe(1);
472+
});
473+
446474
it('should clear availability for fixture', async () => {
447475
const fixtureId = 'fixture-123';
448476
let deleteCalled = false;
@@ -477,6 +505,7 @@ describe('DatabaseService Integration Tests', () => {
477505
home_team: 'Home Team',
478506
away_team: 'Away Team',
479507
venue: null,
508+
is_past: 0,
480509
created_at: Date.now()
481510
};
482511

worker/src/database.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Env, Team, FixtureRow, Player, Availability, FinalSelection } from './types';
2-
import { generateUUID, now } from './utils';
1+
import type { Env, Team, Fixture, Player, Availability, FinalSelection } from './types';
2+
import { generateUUID, now, isPastDate } from './utils';
33

44
/**
55
* Database service for D1 operations
@@ -52,16 +52,17 @@ export class DatabaseService {
5252
homeTeam: string,
5353
awayTeam: string,
5454
venue?: string
55-
): Promise<FixtureRow> {
55+
): Promise<Fixture> {
5656
const id = generateUUID();
5757
const timestamp = now();
58+
const isPast = isPastDate(matchDate) ? 1 : 0;
5859

5960
await this.db
6061
.prepare(`
61-
INSERT INTO fixtures (id, team_id, match_date, day_time, home_team, away_team, venue, created_at)
62-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
62+
INSERT INTO fixtures (id, team_id, match_date, day_time, home_team, away_team, venue, is_past, created_at)
63+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
6364
`)
64-
.bind(id, teamId, matchDate, dayTime, homeTeam, awayTeam, venue || null, timestamp)
65+
.bind(id, teamId, matchDate, dayTime, homeTeam, awayTeam, venue || null, isPast, timestamp)
6566
.run();
6667

6768
return {
@@ -72,41 +73,44 @@ export class DatabaseService {
7273
home_team: homeTeam,
7374
away_team: awayTeam,
7475
venue: venue || null,
76+
is_past: isPast,
7577
created_at: timestamp
7678
};
7779
}
7880

79-
async getFixtures(teamId: string): Promise<FixtureRow[]> {
81+
async getFixtures(teamId: string): Promise<Fixture[]> {
8082
const result = await this.db
8183
.prepare('SELECT * FROM fixtures WHERE team_id = ? ORDER BY match_date ASC')
8284
.bind(teamId)
83-
.all<FixtureRow>();
85+
.all<Fixture>();
8486

8587
return result.results || [];
8688
}
8789

88-
async getFixture(fixtureId: string): Promise<FixtureRow | null> {
90+
async getFixture(fixtureId: string): Promise<Fixture | null> {
8991
const result = await this.db
9092
.prepare('SELECT * FROM fixtures WHERE id = ?')
9193
.bind(fixtureId)
92-
.first<FixtureRow>();
94+
.first<Fixture>();
9395

9496
return result;
9597
}
9698

97-
async getFixtureByTeams(teamId: string, homeTeam: string, awayTeam: string): Promise<FixtureRow | null> {
99+
async getFixtureByTeams(teamId: string, homeTeam: string, awayTeam: string): Promise<Fixture | null> {
98100
const result = await this.db
99101
.prepare('SELECT * FROM fixtures WHERE team_id = ? AND home_team = ? AND away_team = ?')
100102
.bind(teamId, homeTeam, awayTeam)
101-
.first<FixtureRow>();
103+
.first<Fixture>();
102104

103105
return result;
104106
}
105107

106108
async updateFixtureDate(fixtureId: string, matchDate: string, dayTime: string): Promise<void> {
109+
const isPast = isPastDate(matchDate) ? 1 : 0;
110+
107111
await this.db
108-
.prepare('UPDATE fixtures SET match_date = ?, day_time = ? WHERE id = ?')
109-
.bind(matchDate, dayTime, fixtureId)
112+
.prepare('UPDATE fixtures SET match_date = ?, day_time = ?, is_past = ? WHERE id = ?')
113+
.bind(matchDate, dayTime, isPast, fixtureId)
110114
.run();
111115
}
112116

@@ -235,13 +239,14 @@ export class DatabaseService {
235239
dayTime: string,
236240
playerIds: string[]
237241
): Promise<void> {
242+
const isPast = isPastDate(matchDate) ? 1 : 0;
238243
const timestamp = now();
239244

240245
// Build batch of statements
241246
const statements = [
242247
// Update fixture date
243-
this.db.prepare('UPDATE fixtures SET match_date = ?, day_time = ? WHERE id = ?')
244-
.bind(matchDate, dayTime, fixtureId),
248+
this.db.prepare('UPDATE fixtures SET match_date = ?, day_time = ?, is_past = ? WHERE id = ?')
249+
.bind(matchDate, dayTime, isPast, fixtureId),
245250
// Clear availability
246251
this.db.prepare('DELETE FROM availability WHERE fixture_id = ?')
247252
.bind(fixtureId),
@@ -271,17 +276,18 @@ export class DatabaseService {
271276
awayTeam: string,
272277
venue: string | undefined,
273278
playerIds: string[]
274-
): Promise<FixtureRow> {
279+
): Promise<Fixture> {
275280
const fixtureId = generateUUID();
276281
const timestamp = now();
282+
const isPast = isPastDate(matchDate) ? 1 : 0;
277283

278284
// Build batch of statements
279285
const statements = [
280286
// Create fixture
281287
this.db.prepare(`
282-
INSERT INTO fixtures (id, team_id, match_date, day_time, home_team, away_team, venue, created_at)
283-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
284-
`).bind(fixtureId, teamId, matchDate, dayTime, homeTeam, awayTeam, venue || null, timestamp),
288+
INSERT INTO fixtures (id, team_id, match_date, day_time, home_team, away_team, venue, is_past, created_at)
289+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
290+
`).bind(fixtureId, teamId, matchDate, dayTime, homeTeam, awayTeam, venue || null, isPast, timestamp),
285291
];
286292

287293
// Add availability inserts for each player
@@ -304,6 +310,7 @@ export class DatabaseService {
304310
home_team: homeTeam,
305311
away_team: awayTeam,
306312
venue: venue || null,
313+
is_past: isPast,
307314
created_at: timestamp
308315
};
309316
}

0 commit comments

Comments
 (0)