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

parent 12aafd6f
......@@ -132,7 +132,13 @@ class GamesThread(ThreadedComponent):
return False
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:
if not fixture_id:
return False
......@@ -140,29 +146,36 @@ class GamesThread(ThreadedComponent):
# Get today's date in venue timezone
today = self._get_today_venue_date()
# Get all active matches for this fixture
matches = session.query(MatchModel).filter(
# Get the FIRST match of the fixture (by match_number)
# This determines the fixture date, regardless of completion status
first_match = session.query(MatchModel).filter(
MatchModel.fixture_id == fixture_id,
MatchModel.active_status == True
).all()
).order_by(MatchModel.match_number.asc()).first()
for match in matches:
if match.start_time:
# Convert UTC start_time to venue timezone for date comparison
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()
if not first_match:
logger.debug(f"Fixture {fixture_id} has no matches")
return False
# Check if the match date is yesterday
if (today - match_date).days == 1:
logger.debug(f"Fixture {fixture_id} has match from yesterday: {match_date}, today: {today}")
return True
if not first_match.start_time:
logger.debug(f"First match of fixture {fixture_id} has no start_time")
return False
logger.debug(f"Fixture {fixture_id} has no matches from yesterday")
return False
# Convert UTC start_time to venue timezone for date comparison
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:
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
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