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