Fix fixture date detection: use first match of fixture to determine date, not any match

parent 12aafd6f
...@@ -132,7 +132,13 @@ class GamesThread(ThreadedComponent): ...@@ -132,7 +132,13 @@ class GamesThread(ThreadedComponent):
return False return False
def _is_fixture_from_yesterday(self, fixture_id: str, session) -> bool: def _is_fixture_from_yesterday(self, fixture_id: str, session) -> bool:
"""Check if the specified fixture has any matches from yesterday""" """Check if the specified fixture is from yesterday.
A fixture is considered 'from yesterday' if the FIRST match of the fixture
(determined by match_number) has a start_time from yesterday.
The fixture date is determined by the first match, regardless of whether
that match is completed or not.
"""
try: try:
if not fixture_id: if not fixture_id:
return False return False
...@@ -140,29 +146,36 @@ class GamesThread(ThreadedComponent): ...@@ -140,29 +146,36 @@ class GamesThread(ThreadedComponent):
# Get today's date in venue timezone # Get today's date in venue timezone
today = self._get_today_venue_date() today = self._get_today_venue_date()
# Get all active matches for this fixture # Get the FIRST match of the fixture (by match_number)
matches = session.query(MatchModel).filter( # This determines the fixture date, regardless of completion status
first_match = session.query(MatchModel).filter(
MatchModel.fixture_id == fixture_id, MatchModel.fixture_id == fixture_id,
MatchModel.active_status == True MatchModel.active_status == True
).all() ).order_by(MatchModel.match_number.asc()).first()
for match in matches: if not first_match:
if match.start_time: logger.debug(f"Fixture {fixture_id} has no matches")
# Convert UTC start_time to venue timezone for date comparison return False
from ..utils.timezone_utils import utc_to_venue_datetime
venue_start_time = utc_to_venue_datetime(match.start_time, self.db_manager)
match_date = venue_start_time.date()
# Check if the match date is yesterday if not first_match.start_time:
if (today - match_date).days == 1: logger.debug(f"First match of fixture {fixture_id} has no start_time")
logger.debug(f"Fixture {fixture_id} has match from yesterday: {match_date}, today: {today}") return False
return True
logger.debug(f"Fixture {fixture_id} has no matches from yesterday") # Convert UTC start_time to venue timezone for date comparison
return False from ..utils.timezone_utils import utc_to_venue_datetime
venue_start_time = utc_to_venue_datetime(first_match.start_time, self.db_manager)
match_date = venue_start_time.date()
# Check if the first match date is yesterday
days_diff = (today - match_date).days
is_yesterday = days_diff == 1
logger.info(f"📅 Fixture {fixture_id} first match #{first_match.match_number} date: {match_date}, today: {today}, days_diff: {days_diff}, is_yesterday: {is_yesterday}")
return is_yesterday
except Exception as e: except Exception as e:
logger.error(f"Failed to check if fixture {fixture_id} has matches from yesterday: {e}") logger.error(f"Failed to check if fixture {fixture_id} is from yesterday: {e}")
return False return False
def _create_new_fixture_for_continuation(self) -> Optional[str]: def _create_new_fixture_for_continuation(self) -> Optional[str]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment