def _get_client_business_hours(self, client_id: str) -> Optional[Dict[str, Any]]: """ Get client's business hours configuration from Airtable Args: client_id: The client's Airtable record ID Returns: Client data with timezone and opening hours, or None if not found """ if not airtable_service.is_configured(): logger.error("Airtable service not configured") return None try: # Get client record client_table = Table(Config.AIRTABLE_API_KEY, Config.AIRTABLE_BASE_ID, 'tblkyQzhGKVv6H03U') client_record = client_table.get(client_id) if not client_record: logger.warning(f"Client record not found: {client_id}") return None client_fields = client_record['fields'] timezone = client_fields.get('timezone_text') # Use the formula field instead of linked field opening_hours_ids = client_fields.get('opening_hours', []) if not timezone: logger.warning(f"No timezone configured for client: {client_id}") return None if not opening_hours_ids: logger.warning(f"No opening hours configured for client: {client_id}") return None # Get opening hours records opening_hours_table = Table(Config.AIRTABLE_API_KEY, Config.AIRTABLE_BASE_ID, 'tblHNQIWJn1QzEj6r') opening_hours_records = [] for hours_id in opening_hours_ids: try: hours_record = opening_hours_table.get(hours_id) if hours_record: fields = hours_record['fields'] logger.info(f"Opening hours record {hours_id}: {fields}") opening_hours_records.append(fields) except Exception as e: logger.warning(f"Error getting opening hours record {hours_id}: {e}") continue return { 'timezone': timezone, 'opening_hours': opening_hours_records } except Exception as e: logger.error(f"Error getting client business hours: {e}") return None def _check_business_hours(self, opening_hours: List[Dict[str, Any]], current_weekday: str, current_time_str: str) -> bool: """ Check if current time is within business hours Args: opening_hours: List of opening hours records from Airtable current_weekday: Current weekday in lowercase (e.g., 'monday') current_time_str: Current time in HH:MM format (e.g., '14:30') Returns: True if within business hours, False otherwise """ try: # Find the opening hours record for the current weekday current_day_hours = None # Handle both scenarios: single record with list of days, or multiple records with individual days for hours_record in opening_hours: day_field = hours_record.get('day', '') # If day is a list (single record with multiple days) if isinstance(day_field, list): days = [str(d).lower() for d in day_field] if current_weekday in days: current_day_hours = hours_record break # If day is a string (individual day record) elif isinstance(day_field, str): day = day_field.lower() if day == current_weekday: current_day_hours = hours_record break if not current_day_hours: logger.info(f"No opening hours configured for {current_weekday}") return False start_time = current_day_hours.get('start_time', '') end_time = current_day_hours.get('end_time', '') if not start_time or not end_time: logger.warning(f"Incomplete opening hours for {current_weekday}: start={start_time}, end={end_time}") return False logger.info(f"Business hours for {current_weekday}: {start_time} - {end_time}") # Compare times (HH:MM format allows direct string comparison) is_within_hours = start_time <= current_time_str <= end_time logger.info(f"Current time {current_time_str} within hours {start_time}-{end_time}: {is_within_hours}") return is_within_hours except Exception as e: logger.error(f"Error checking business hours: {e}") return False